#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : ~x + 1;
}
inline void print(int x) {
  if (x < 0) putchar('-'), x = ~x + 1;
  if (x >= 10) print(x / 10);
  putchar((x % 10) | 48);
}
int n;
vector<int> vec[200010];
int in[200010], pos[22][400010];
int dep[200010], minn[22][400010];
int idx;
int logt[400010];
int a[200010], P[200010];
void dfs(int x, int fa) {
  in[x] = ++idx;
  pos[0][idx] = x;
  dep[x] = dep[fa] + 1;
  for (int v : vec[x]) {
    dfs(v, x);
    pos[0][++idx] = x;
  }
}
void ST() {
  for (int i = 1; i <= idx; ++i) minn[0][i] = dep[pos[0][i]];
  for (int i = 1; i <= 18; ++i) {
    for (int j = 1; j + (1 << i) - 1 <= idx; ++j) {
      if (minn[i - 1][j] > minn[i - 1][j + (1 << i - 1)]) {
        minn[i][j] = minn[i - 1][j + (1 << i - 1)];
        pos[i][j] = pos[i - 1][j + (1 << i - 1)];
      } else {
        minn[i][j] = minn[i - 1][j];
        pos[i][j] = pos[i - 1][j];
      }
    }
  }
}
int q(int l, int r) {
  if (l > r) swap(l, r);
  int k = logt[r - l + 1];
  if (minn[k][l] <= minn[k][r - (1 << k) + 1])
    return pos[k][l];
  else
    return pos[k][r - (1 << k) + 1];
}
int LCA(int x, int y) { return q(in[x], in[y]); }
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }
struct segment {
  struct node {
    int x, y;
  } tre[800010];
  node hb(node x, int y) {
    if (x.x < 0 || y < 0) return node{-1, -1};
    int xx = x.x, xy = x.y;
    int A = dis(xx, xy);
    int B = dis(xx, y);
    int C = dis(xy, y);
    if (A + B == C) return node{xy, y};
    if (B + C == A) return x;
    if (A + C == B) return node{xx, y};
    return node{-1, -1};
  }
  node merge(node x, node y) {
    if (x.x < 0 || y.x < 0) return node{-1, -1};
    node tmp = hb(x, y.x);
    if (tmp.x < 0)
      return node{-1, -1};
    else
      return hb(tmp, y.y);
  }
  void pushup(int p) { tre[p] = merge(tre[p << 1], tre[p << 1 | 1]); }
  void build(int p, int l, int r) {
    if (l == r) {
      tre[p] = node{P[l], P[l]};
      return;
    }
    int mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
  }
  void change(int p, int l, int r, int pos, int k) {
    if (l == r) {
      tre[p] = node{k, k};
      return;
    }
    int mid = l + r >> 1;
    if (pos <= mid)
      change(p << 1, l, mid, pos, k);
    else
      change(p << 1 | 1, mid + 1, r, pos, k);
    pushup(p);
  }
  int query(int p, int l, int r, node &lf) {
    if (tre[p].x > 0) {
      if (lf.x == -1) {
        lf = tre[p];
        return r + 1;
      }
      node tmp = merge(tre[p], lf);
      if (tmp.x > 0) {
        lf = tmp;
        return r + 1;
      }
    }
    if (l == r) return l;
    int mid = l + r >> 1;
    int x = query(p << 1, l, mid, lf);
    if (x <= mid) return x;
    return query(p << 1 | 1, mid + 1, r, lf);
  }
} T;
int main() {
  n = read();
  for (int i = 2; i <= 2 * n; ++i) logt[i] = logt[i >> 1] + 1;
  for (int i = 1; i <= n; ++i) a[i] = read() + 1, P[a[i]] = i;
  for (int i = 1; i < n; ++i) {
    int fa = read();
    vec[fa].push_back(i + 1);
  }
  dfs(1, 0);
  ST();
  T.build(1, 1, n);
  int q = read();
  while (q--) {
    int op = read();
    if (op == 2) {
      segment::node x;
      x.x = -1, x.y = -1;
      printf("%d\n", T.query(1, 1, n, x) - 1);
      continue;
    }
    int x = read(), y = read();
    swap(a[x], a[y]);
    swap(P[a[x]], P[a[y]]);
    T.change(1, 1, n, a[x], x);
    T.change(1, 1, n, a[y], y);
  }
}

### Prompt

Generate 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>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : ~x + 1;
}
inline void print(int x) {
  if (x < 0) putchar('-'), x = ~x + 1;
  if (x >= 10) print(x / 10);
  putchar((x % 10) | 48);
}
int n;
vector<int> vec[200010];
int in[200010], pos[22][400010];
int dep[200010], minn[22][400010];
int idx;
int logt[400010];
int a[200010], P[200010];
void dfs(int x, int fa) {
  in[x] = ++idx;
  pos[0][idx] = x;
  dep[x] = dep[fa] + 1;
  for (int v : vec[x]) {
    dfs(v, x);
    pos[0][++idx] = x;
  }
}
void ST() {
  for (int i = 1; i <= idx; ++i) minn[0][i] = dep[pos[0][i]];
  for (int i = 1; i <= 18; ++i) {
    for (int j = 1; j + (1 << i) - 1 <= idx; ++j) {
      if (minn[i - 1][j] > minn[i - 1][j + (1 << i - 1)]) {
        minn[i][j] = minn[i - 1][j + (1 << i - 1)];
        pos[i][j] = pos[i - 1][j + (1 << i - 1)];
      } else {
        minn[i][j] = minn[i - 1][j];
        pos[i][j] = pos[i - 1][j];
      }
    }
  }
}
int q(int l, int r) {
  if (l > r) swap(l, r);
  int k = logt[r - l + 1];
  if (minn[k][l] <= minn[k][r - (1 << k) + 1])
    return pos[k][l];
  else
    return pos[k][r - (1 << k) + 1];
}
int LCA(int x, int y) { return q(in[x], in[y]); }
int dis(int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }
struct segment {
  struct node {
    int x, y;
  } tre[800010];
  node hb(node x, int y) {
    if (x.x < 0 || y < 0) return node{-1, -1};
    int xx = x.x, xy = x.y;
    int A = dis(xx, xy);
    int B = dis(xx, y);
    int C = dis(xy, y);
    if (A + B == C) return node{xy, y};
    if (B + C == A) return x;
    if (A + C == B) return node{xx, y};
    return node{-1, -1};
  }
  node merge(node x, node y) {
    if (x.x < 0 || y.x < 0) return node{-1, -1};
    node tmp = hb(x, y.x);
    if (tmp.x < 0)
      return node{-1, -1};
    else
      return hb(tmp, y.y);
  }
  void pushup(int p) { tre[p] = merge(tre[p << 1], tre[p << 1 | 1]); }
  void build(int p, int l, int r) {
    if (l == r) {
      tre[p] = node{P[l], P[l]};
      return;
    }
    int mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
  }
  void change(int p, int l, int r, int pos, int k) {
    if (l == r) {
      tre[p] = node{k, k};
      return;
    }
    int mid = l + r >> 1;
    if (pos <= mid)
      change(p << 1, l, mid, pos, k);
    else
      change(p << 1 | 1, mid + 1, r, pos, k);
    pushup(p);
  }
  int query(int p, int l, int r, node &lf) {
    if (tre[p].x > 0) {
      if (lf.x == -1) {
        lf = tre[p];
        return r + 1;
      }
      node tmp = merge(tre[p], lf);
      if (tmp.x > 0) {
        lf = tmp;
        return r + 1;
      }
    }
    if (l == r) return l;
    int mid = l + r >> 1;
    int x = query(p << 1, l, mid, lf);
    if (x <= mid) return x;
    return query(p << 1 | 1, mid + 1, r, lf);
  }
} T;
int main() {
  n = read();
  for (int i = 2; i <= 2 * n; ++i) logt[i] = logt[i >> 1] + 1;
  for (int i = 1; i <= n; ++i) a[i] = read() + 1, P[a[i]] = i;
  for (int i = 1; i < n; ++i) {
    int fa = read();
    vec[fa].push_back(i + 1);
  }
  dfs(1, 0);
  ST();
  T.build(1, 1, n);
  int q = read();
  while (q--) {
    int op = read();
    if (op == 2) {
      segment::node x;
      x.x = -1, x.y = -1;
      printf("%d\n", T.query(1, 1, n, x) - 1);
      continue;
    }
    int x = read(), y = read();
    swap(a[x], a[y]);
    swap(P[a[x]], P[a[y]]);
    T.change(1, 1, n, a[x], x);
    T.change(1, 1, n, a[y], y);
  }
}

```