#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; void solve() { int n; cin >> n; n += 2; vector a(n, INF), b(n, INF); FOR(i, 1, n - 1) cin >> a[i]; FOR(i, 1, n - 1) cin >> b[i]; REP(i, n) { if (a[i] > b[i]) { cout << "No\n"; return; } } map>> seg; for (int i = 1; i < n - 1;) { int j = i + 1; while (j < n - 1 && b[i] == b[j]) ++j; seg[-b[i]].emplace_back(i, j - 1); i = j; } vector> que(n); for (const auto [num_, v] : seg) { const int num = -num_; for (const auto [l, r] : v) { if (b[l - 1] > num && que[l - 1].count(num) == 1) { if (b[r + 1] < num) { int mx = -INF; for (int i = r; i >= l; --i) { if (chmax(mx, a[i]) && mx < num) que[r + 1].emplace(mx); } for (auto it = que[l - 1].begin(); it != que[l - 1].end() && *it <= mx; it = que[l - 1].erase(it)) {} while (!que[l - 1].empty() && *que[l - 1].rbegin() >= num) { que[l - 1].erase(prev(que[l - 1].end())); } if (que[l - 1].size() > que[r + 1].size()) swap(que[l - 1], que[r + 1]); copy(ALL(que[l - 1]), inserter(que[r + 1], que[r + 1].end())); } } else if (b[r + 1] > num && que[r + 1].count(num) == 1) { if (b[l - 1] < num) { int mx = -INF; for (int i = l; i <= r; ++i) { if (chmax(mx, a[i]) && mx < num) que[l - 1].emplace(mx); } for (auto it = que[r + 1].begin(); it != que[r + 1].end() && *it <= mx; it = que[r + 1].erase(it)) {} while (!que[r + 1].empty() && *que[r + 1].rbegin() >= num) { que[r + 1].erase(prev(que[r + 1].end())); } if (que[r + 1].size() > que[l - 1].size()) swap(que[r + 1], que[l - 1]); copy(ALL(que[r + 1]), inserter(que[l - 1], que[l - 1].end())); } } else if (count(a.begin() + l, a.begin() + (r + 1), num) > 0) { FOR(i, l, r + 1) { if (a[i] == num) { int mx = -INF; FOR(j, l, i) { if (chmax(mx, a[j]) && mx < num) que[l - 1].emplace(mx); } break; } } for (int i = r; i >= l; --i) { if (a[i] == num) { int mx = -INF; for (int j = r; j > i; --j) { if (chmax(mx, a[j]) && mx < num) que[r + 1].emplace(mx); } break; } } } else { cout << "No\n"; return; } } } cout << "Yes\n"; } int main() { int t; cin >> t; while (t--) solve(); return 0; }