#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
using namespace std;
const int N = 2e5 + 7;
const int K = 20;
int go[K][N];
int tin[N], tout[N];
vector<int> g[N];
int timer = 0;
int perm[N];
int where[N];
pair<int, int> t[4 * N];
int n;
void dfs(int u, int p = 0) {
  go[0][u] = p;
  tin[u] = timer++;
  for (int i = 1; i < K; ++i) {
    go[i][u] = go[i - 1][go[i - 1][u]];
  }
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  tout[u] = timer++;
}
bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }
int lca(int a, int b) {
  if (anc(a, b)) return a;
  if (anc(b, a)) return b;
  for (int k = K - 1; k >= 0; --k) {
    if (!anc(go[k][a], b)) {
      a = go[k][a];
    }
  }
  return go[0][a];
}
pair<int, int> null = {-1, -1};
pair<int, int> empt = {-2, -2};
int d;
bool on_path(int a, int b, int c) {
  return anc(d, c) && (anc(c, a) || anc(c, b));
}
bool check(int l, int r, const pair<int, int> &a, const pair<int, int> &b) {
  d = lca(l, r);
  return on_path(l, r, a.first) && on_path(l, r, a.second) &&
         on_path(l, r, b.first) && on_path(l, r, b.second);
}
pair<int, int> merge(const pair<int, int> &a, const pair<int, int> &b) {
  if (a == null || b == null) return null;
  if (a == empt) return b;
  if (b == empt) return a;
  if (check(a.first, a.second, a, b)) return a;
  if (check(b.first, b.second, a, b)) return b;
  if (check(a.first, b.first, a, b)) return {a.first, b.first};
  if (check(a.second, b.second, a, b)) return {a.second, b.second};
  if (check(a.first, b.second, a, b)) return {a.first, b.second};
  if (check(a.second, b.first, a, b)) return {a.second, b.first};
  return null;
}
void build(int l, int r, int v) {
  if (l == r) {
    t[v] = {where[l], where[l]};
  } else {
    int m = (l + r) >> 1;
    build(l, m, 2 * v + 1);
    build(m + 1, r, 2 * v + 2);
    t[v] = merge(t[2 * v + 1], t[2 * v + 2]);
  }
}
void modify(int ind, int l, int r, int v) {
  if (l == r) {
    t[v] = {where[ind], where[ind]};
  } else {
    int m = (l + r) >> 1;
    if (ind <= m)
      modify(ind, l, m, 2 * v + 1);
    else
      modify(ind, m + 1, r, 2 * v + 2);
    t[v] = merge(t[2 * v + 1], t[2 * v + 2]);
  }
}
void update(int a, int b) {
  int pa = perm[a];
  int pb = perm[b];
  swap(where[perm[a]], where[perm[b]]);
  swap(perm[a], perm[b]);
  modify(pa, 0, n - 1, 0);
  modify(pb, 0, n - 1, 0);
}
int get(int v, int l, int r, pair<int, int> cur) {
  if (l == r) {
    return l;
  }
  int m = (l + r) >> 1;
  auto mem = merge(t[2 * v + 1], cur);
  if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem);
  return get(2 * v + 1, l, m, cur);
}
int mex() {
  if (t[0].first != -1) return n;
  return get(0, 0, n - 1, {where[0], where[0]});
}
void init() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> perm[i];
    where[perm[i]] = i;
  }
  for (int i = 1; i < n; ++i) {
    int p;
    cin >> p;
    --p;
    g[p].push_back(i);
  }
  dfs(0);
  build(0, n - 1, 0);
}
void solve() {
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      update(a, b);
    } else {
      cout << mex() << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  init();
  solve();
}

### Prompt

Please formulate a cpp solution to the following problem:

Once Grisha found a tree (connected graph without cycles) with a root in node 1.

But this tree was not just a tree. A permutation p of integers from 0 to n - 1 is written in nodes, a number p_i is written in node i.

As Grisha likes to invent some strange and interesting problems for himself, but not always can solve them, you need to help him deal with two types of queries on this tree.

Let's define a function MEX(S), where S is a set of non-negative integers, as a smallest non-negative integer that is not included in this set.

Let l be a simple path in this tree. So let's define indices of nodes which lie on l as u_1, u_2, …, u_k. 

Define V(l) as a set {p_{u_1}, p_{u_2}, … , p_{u_k}}. 

Then queries are: 

  1. For two nodes i and j, swap p_i and p_j. 
  2. Find the maximum value of MEX(V(l)) in all possible l. 

Input

The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of nodes of a tree.

The second line contains n integers — p_1, p_2, …, p_n (0≤ p_i < n) — the permutation p, it's guaranteed that all numbers are different .

The third line contains n - 1 integers — d_2, d_3, …, d_n (1 ≤ d_i < i), where d_i is a direct ancestor of node i in a tree.

The fourth line contains a single integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of queries.

The following q lines contain the description of queries:

At the beginning of each of next q lines, there is a single integer t (1 or 2) — the type of a query: 

  1. If t = 1, the line also contains two integers i and j (1 ≤ i, j ≤ n) — the indices of nodes, where values of the permutation should be swapped. 
  2. If t = 2, you need to find the maximum value of MEX(V(l)) in all possible l. 

Output

For each type 2 query print a single integer — the answer for this query.

Examples

Input


6
2 5 0 3 1 4
1 1 3 3 3
3
2
1 6 3
2


Output


3
2


Input


6
5 2 1 4 3 0
1 1 1 3 3
9
2
1 5 3
2
1 6 1
2
1 4 2
2
1 1 6
2


Output


3
2
4
4
2

Note

Number written in brackets is a permutation value of a node. 

<image> In the first example, for the first query, optimal path is a path from node 1 to node 5. For it, set of values is \{0, 1, 2\} and MEX is 3.  <image> For the third query, optimal path is a path from node 5 to node 6. For it, set of values is \{0, 1, 4\} and MEX is 2.  <image> In the second example, for the first query, optimal path is a path from node 2 to node 6. For it, set of values is \{0, 1, 2, 5\} and MEX is 3.  <image> For the third query, optimal path is a path from node 5 to node 6. For it, set of values is \{0, 1, 3\} and MEX is 2.  <image> For the fifth query, optimal path is a path from node 5 to node 2. For it, set of values is \{0, 1, 2, 3\} and MEX is 4.  <image> For the seventh query, optimal path is a path from node 5 to node 4. For it, set of values is \{0, 1, 2, 3\} and MEX is 4.  <image> For the ninth query, optimal path is a path from node 6 to node 5. For it, set of values is \{0, 1, 3\} and MEX is 2. 

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
using namespace std;
const int N = 2e5 + 7;
const int K = 20;
int go[K][N];
int tin[N], tout[N];
vector<int> g[N];
int timer = 0;
int perm[N];
int where[N];
pair<int, int> t[4 * N];
int n;
void dfs(int u, int p = 0) {
  go[0][u] = p;
  tin[u] = timer++;
  for (int i = 1; i < K; ++i) {
    go[i][u] = go[i - 1][go[i - 1][u]];
  }
  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  tout[u] = timer++;
}
bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; }
int lca(int a, int b) {
  if (anc(a, b)) return a;
  if (anc(b, a)) return b;
  for (int k = K - 1; k >= 0; --k) {
    if (!anc(go[k][a], b)) {
      a = go[k][a];
    }
  }
  return go[0][a];
}
pair<int, int> null = {-1, -1};
pair<int, int> empt = {-2, -2};
int d;
bool on_path(int a, int b, int c) {
  return anc(d, c) && (anc(c, a) || anc(c, b));
}
bool check(int l, int r, const pair<int, int> &a, const pair<int, int> &b) {
  d = lca(l, r);
  return on_path(l, r, a.first) && on_path(l, r, a.second) &&
         on_path(l, r, b.first) && on_path(l, r, b.second);
}
pair<int, int> merge(const pair<int, int> &a, const pair<int, int> &b) {
  if (a == null || b == null) return null;
  if (a == empt) return b;
  if (b == empt) return a;
  if (check(a.first, a.second, a, b)) return a;
  if (check(b.first, b.second, a, b)) return b;
  if (check(a.first, b.first, a, b)) return {a.first, b.first};
  if (check(a.second, b.second, a, b)) return {a.second, b.second};
  if (check(a.first, b.second, a, b)) return {a.first, b.second};
  if (check(a.second, b.first, a, b)) return {a.second, b.first};
  return null;
}
void build(int l, int r, int v) {
  if (l == r) {
    t[v] = {where[l], where[l]};
  } else {
    int m = (l + r) >> 1;
    build(l, m, 2 * v + 1);
    build(m + 1, r, 2 * v + 2);
    t[v] = merge(t[2 * v + 1], t[2 * v + 2]);
  }
}
void modify(int ind, int l, int r, int v) {
  if (l == r) {
    t[v] = {where[ind], where[ind]};
  } else {
    int m = (l + r) >> 1;
    if (ind <= m)
      modify(ind, l, m, 2 * v + 1);
    else
      modify(ind, m + 1, r, 2 * v + 2);
    t[v] = merge(t[2 * v + 1], t[2 * v + 2]);
  }
}
void update(int a, int b) {
  int pa = perm[a];
  int pb = perm[b];
  swap(where[perm[a]], where[perm[b]]);
  swap(perm[a], perm[b]);
  modify(pa, 0, n - 1, 0);
  modify(pb, 0, n - 1, 0);
}
int get(int v, int l, int r, pair<int, int> cur) {
  if (l == r) {
    return l;
  }
  int m = (l + r) >> 1;
  auto mem = merge(t[2 * v + 1], cur);
  if (mem.first != -1) return get(2 * v + 2, m + 1, r, mem);
  return get(2 * v + 1, l, m, cur);
}
int mex() {
  if (t[0].first != -1) return n;
  return get(0, 0, n - 1, {where[0], where[0]});
}
void init() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> perm[i];
    where[perm[i]] = i;
  }
  for (int i = 1; i < n; ++i) {
    int p;
    cin >> p;
    --p;
    g[p].push_back(i);
  }
  dfs(0);
  build(0, n - 1, 0);
}
void solve() {
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      update(a, b);
    } else {
      cout << mex() << '\n';
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  init();
  solve();
}

```