#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const long long INF = 1e18 + 5;   const int MAXN = 1e6;   const int base = 317;   const long long bs = 1e5 + 1;   const long long mod = 998244353;   struct node {     node *left, *right;     int val;     node(node* l = NULL, node* r = NULL, int v = 0) {       left = l;       right = r;       val = v;     }   };   node* version[N];   int n, m, d[N], up[21][N];   int tin[N], tout[N], timer;   vector<int> g[N];   void build(node* v, int L, int R) {     if (L == R) return;     int md = (L + R) >> 1;     v->left = new node();     v->right = new node();     build(v->left, L, md);     build(v->right, md + 1, R);   }   node* upd(node* v, int L, int R, int pos, int val) {     if (L == R) {       return new node(NULL, NULL, val);     }     node* nw = new node();     int md = (L + R) >> 1;     if (md >= pos) {       nw->right = v->right;       nw->left = upd(v->left, L, md, pos, val);     } else {       nw->left = v->left;       nw->right = upd(v->right, md + 1, R, pos, val);     }     nw->val = nw->left->val + nw->right->val;     return nw;   }   void dfs(int v, int p = 0) {     d[v] = d[p] + 1;     up[0][v] = p;     tin[v] = ++timer;     for (int i = 1; i <= 20; i++) {       up[i][v] = up[i - 1][up[i - 1][v]];     }     for (int to : g[v]) {       if (to != p) {         dfs(to, v);       }     }     tout[v] = ++timer;   }   int lca(int a, int b) {     if (d[a] > d[b]) {       swap(a, b);     }     for (int i = 20; i >= 0; i--) {       if (d[a] <= d[up[i][b]]) {         b = up[i][b];       }     }     if (a == b) return a;     for (int i = 20; i >= 0; i--) {       if (up[i][a] != up[i][b]) {         a = up[i][a];         b = up[i][b];       }     }     return up[0][a];   }   int get(node* v, int L, int R, int l, int r) {     if (L > r || l > R) return 0;     if (l <= L && R <= r) return v->val;     int md = (L + R) >> 1;     return get(v->left, L, md, l, r) + get(v->right, md + 1, R, l, r);   }   int calc(int u, int v, int y1, int y2) {     int r = tin[u], l = tin[v];     return d[u] - d[v] + 1 -            (get(version[y2], 1, 2 * n, l, r) - get(version[y1], 1, 2 * n, l, r));   }   int getk(int a, int k, int y1, int y2) {     for (int i = 20; i >= 0; i--) {       int count = calc(a, up[i][a], y1, y2) - calc(a, a, y1, y2);       if (count < k) {         k -= count;         a = up[i][a];       }     }     return up[0][a];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       int p;       cin >> p;       g[i].push_back(p);       g[p].push_back(i);     }     dfs(g[0][0], g[0][0]);     version[0] = new node();     build(version[0], 0, n * 2);     cin >> m;     for (int i = 1; i <= m; i++) {       int t;       cin >> t;       if (t == 1) {         int id;         cin >> id;         version[i] = upd(version[i - 1], 1, 2 * n, tin[id], 1);         version[i] = upd(version[i], 1, 2 * n, tout[id], -1);       } else {         int a, b, k, y;         cin >> a >> b >> k >> y;         version[i] = version[i - 1];         int lc = lca(a, b);         int len1 = calc(a, lc, y, i) - calc(a, a, y, i),             len2 = calc(b, lc, y, i) - calc(b, b, y, i);         int count = len1 + len2 - calc(lc, lc, y, i);         if (count < k) {           cout << -1 <<   n ;           continue;         }         if (len1 >= k) {           cout << getk(a, k, y, i) <<   n ;         } else {           cout << getk(b, count - k + 1, y, i) <<   n ;         }       }     }     return 0;   }