#include <bits/stdc++.h>   using namespace std;   int p[200005], q[200005];   int fa[200005], top[200005], sz[200005], dep[200005];   vector<int> E[200005];   void dfs1(int root);   void dfs2(int root, int toproot);   int lca(int node1, int node2);   int dis(int node1, int node2);   pair<int, int> merge(int node1, int node2, int node3);   pair<int, int> merge(pair<int, int> path1, pair<int, int> path2);   void modify(int index, int lboundary, int rboundary, int position,               pair<int, int> path);   int query(int index, int lboundary, int rboundary, pair<int, int> path);   pair<int, int> tr[4 * 200005];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , p + i), q[p[i]] = i;     for (int i = 2; i <= n; i++) scanf( %d , fa + i);     for (int i = 2; i <= n; i++) {       E[i].push_back(fa[i]);       E[fa[i]].push_back(i);     }     dfs1(1);     dfs2(1, 1);     int k;     scanf( %d , &k);     for (int i = 1; i <= n; i++) {       modify(1, 0, n - 1, p[i], make_pair(q[p[i]], q[p[i]]));     }     while (k--) {       int t, u, v;       scanf( %d , &t);       if (t == 1) {         scanf( %d %d , &u, &v);         swap(p[u], p[v]);         swap(q[p[u]], q[p[v]]);         modify(1, 0, n - 1, p[u], make_pair(q[p[u]], q[p[u]]));         modify(1, 0, n - 1, p[v], make_pair(q[p[v]], q[p[v]]));       } else if (t == 2) {         if (tr[1].first)           cout << n << endl;         else           cout << query(1, 0, n - 1, make_pair(q[0], q[0])) << endl;       }     }   }   void dfs1(int r) {     sz[r] = 1;     for (int u : E[r])       if (u != fa[r]) {         dfs1(u);         sz[r] += sz[u];       }   }   void dfs2(int r, int t) {     top[r] = t;     dep[r] = dep[fa[r]] + 1;     int tid = -1, msz = 0;     for (int u : E[r])       if (u != fa[r] && sz[u] > msz) {         msz = sz[u];         tid = u;       }     if (tid == -1) return;     dfs2(tid, t);     for (int u : E[r])       if (u != fa[r] && u != tid) dfs2(u, u);   }   int lca(int a, int b) {     while (top[a] != top[b]) {       if (dep[top[a]] < dep[top[b]]) swap(a, b);       a = fa[top[a]];     }     return dep[a] < dep[b] ? a : b;   }   int dis(int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; }   pair<int, int> merge(int a, int b, int c) {     int ab = dis(a, b), ac = dis(a, c), bc = dis(b, c);     if (ab == bc + ac)       return make_pair(a, b);     else if (ac == ab + bc)       return make_pair(a, c);     else if (bc == ab + ac)       return make_pair(b, c);     else       return make_pair(0, 0);   }   pair<int, int> merge(pair<int, int> a, pair<int, int> b) {     if (!a.first || !b.first) return make_pair(0, 0);     pair<int, int> tmp = merge(a.first, a.second, b.first);     if (tmp.first) tmp = merge(tmp.first, tmp.second, b.second);     return tmp;   }   void modify(int o, int l, int r, int p, pair<int, int> v) {     if (l == r) {       tr[o] = v;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       modify((o << 1), l, mid, p, v);     else       modify((o << 1 | 1), mid + 1, r, p, v);     tr[o] = merge(tr[(o << 1)], tr[(o << 1 | 1)]);   }   int query(int o, int l, int r, pair<int, int> p) {     if (l == r) return l;     int mid = (l + r) >> 1;     pair<int, int> q = merge(p, tr[(o << 1)]);     if (!q.first)       return query((o << 1), l, mid, p);     else       return query((o << 1 | 1), mid + 1, r, q);   }   