#include <bits/stdc++.h>

using namespace std;

class DisjointSet {
 public:
  explicit DisjointSet(const std::size_t n) : nodes(n, Node(n, 1)) {}

  std::size_t size() const { return nodes.size(); }

  std::size_t size(const std::size_t x) { return nodes[find(x)].size; }

  std::size_t find(const std::size_t x) {
    if (nodes[x].parent == size()) return x;
    return nodes[x].parent = find(nodes[x].parent);
  }

  bool is_same(const std::size_t x, const std::size_t y) { return find(x) == find(y); }

  bool unite(const std::size_t x, const std::size_t y) {
    std::size_t rx = find(x), ry = find(y);
    if (rx == ry) return false;
    if (nodes[rx].size < nodes[ry].size) std::swap(rx, ry);
    nodes[rx].size += nodes[ry].size;
    nodes[ry].parent = rx;
    return true;
  }

 private:
  class Node {
   private:
    friend DisjointSet;

    Node(const std::size_t parent, const std::size_t size) : parent(parent), size(size) {}

    std::size_t parent;
    std::size_t size;
  };

  std::vector<Node> nodes;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  auto solve = [&]() {
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for (auto &&e : A) {
      cin >> e;
    }
    for (auto &&e : B) {
      cin >> e;
    }
    DisjointSet ds(N);
    for (int k = 2; k < N; k++) {
      for (int i = 1; k * (i + 1) - 1 < N; i++) {
        ds.unite(k * i - 1, k * (i + 1) - 1);
      }
    }
    map<int, multiset<int>> mp;
    for (int i = 0; i < N; i++) {
      mp[ds.find(i)].insert(B[i]);
    }
    bool ok = true;
    for (int i = 0; i < N; i++) {
      // for (const auto &[key, value] : mp) {
      //   cerr << "[+]" << ' ' << key << endl;
      //   for (const auto &e : value) {
      //     cerr << e << '\n';
      //   }
      // }
      // cerr << "--------" << endl;
      if (mp[ds.find(i)].find(A[i]) == mp[ds.find(i)].end()) {
        ok = false;
        break;
      }
      mp[ds.find(i)].erase(mp[ds.find(i)].find(A[i]));
    }
    cout << (ok ? "Yes" : "No") << '\n';
  };
  for (int i = 0; i < T; i++) {
    solve();
  }
  return 0;
}