#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   const int K = 20;   int go[N][K];   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[u][0] = p;     tin[u] = timer++;     for (int i = 1; i < K; ++i) {       go[u][i] = go[go[u][i - 1]][i - 1];     }     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;     for (int k = K - 1; k >= 0; --k) {       if (!anc(go[a][k], b)) {         a = go[a][k];       }     }     return go[a][0];   }   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 l, int r, int v, pair<int, int> &pth) {     if (l == r) {       pth = merge(pth, t[v]);       if (pth == null) return l - 1;       return l;     }     int m = (l + r) >> 1;     auto cur = merge(pth, t[2 * v + 1]);     if (cur.first != -1) {       return get(m + 1, r, 2 * v + 2, cur);     }     return get(l, m, 2 * v + 1, pth);   }   int mex() {     pair<int, int> cur = empt;     return get(0, n - 1, 0, cur) + 1;   }   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();   }