#include <bits/stdc++.h>
using namespace std;
struct TreeUtility {
  vector<int> *g;
  int n;
  int LN;
  int root;
  int pool = 0;
  int *st;
  int *en;
  int *dep;
  int **pa;
  TreeUtility(vector<int> g[], int n, int root = 1) : g(g), n(n), root(root) {
    LN = ceil(log2(n + 1));
    st = (int *)malloc(sizeof(int) * (n + 1));
    en = (int *)malloc(sizeof(int) * (n + 1));
    dep = (int *)malloc(sizeof(int) * (n + 1));
    pa = (int **)malloc(sizeof(int *) * LN);
    for (int i = 0; i < LN; ++i) {
      pa[i] = (int *)malloc(sizeof(int) * (n + 1));
    }
    init();
  }
  void init() {
    dfs(root, 0);
    pa[0][0] = 0;
    for (int j = 1; j < LN; ++j) {
      for (int i = 0; i <= n; ++i) {
        pa[j][i] = pa[j - 1][pa[j - 1][i]];
      }
    }
  }
  void dfs(int u, int p) {
    st[u] = ++pool;
    dep[u] = dep[p] + 1;
    pa[0][u] = p;
    for (auto v : g[u]) {
      if (v != p) {
        dfs(v, u);
      }
    }
    en[u] = ++pool;
  }
  int lca(int u, int v) {
    if (dep[u] < dep[v]) {
      swap(u, v);
    }
    int d = dep[u] - dep[v];
    for (int i = LN - 1; i >= 0; --i) {
      if (d & (1 << i)) {
        u = pa[i][u];
      }
    }
    if (u == v) {
      return u;
    }
    for (int i = LN - 1; i >= 0; --i) {
      if (pa[i][u] != pa[i][v]) {
        u = pa[i][u];
        v = pa[i][v];
      }
    }
    return pa[0][u];
  }
  int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
  int kthAncestor(int u, int k) {
    for (int i = LN - 1; i >= 0; --i) {
      if (k & (1 << i)) {
        u = pa[i][u];
      }
    }
    return u;
  }
};
const int MAXN = 100005;
typedef struct Node *pnode;
struct Node {
  int sum;
  pnode l, r;
};
pnode null;
void initPST() {
  null = new Node();
  null->l = null->r = null;
  null->sum = 0;
}
void update(pnode &u, pnode p, int x, int v, int l = 1, int r = MAXN * 2) {
  u = new Node();
  u->l = p->l;
  u->r = p->r;
  u->sum = p->sum + v;
  if (l == r) {
    return;
  } else {
    int m = (l + r) / 2;
    if (x <= m) {
      update(u->l, p->l, x, v, l, m);
    } else {
      update(u->r, p->r, x, v, m + 1, r);
    }
  }
}
int query(pnode u, pnode p, int x, int y, int l = 1, int r = MAXN * 2) {
  if (x > r or y < l) {
    return 0;
  } else if (x <= l and r <= y) {
    return u->sum - p->sum;
  } else {
    int m = (l + r) / 2;
    return query(u->l, p->l, x, y, l, m) + query(u->r, p->r, x, y, m + 1, r);
  }
}
pnode root[MAXN];
vector<int> g[MAXN];
TreeUtility *treeUtility;
int **pa, *st, *dep, *en;
int kthAncestor(int u, int k) { return treeUtility->kthAncestor(u, k); }
int lca(int u, int v) { return treeUtility->lca(u, v); }
int sz = 0;
void attackCity(int u, int t) {
  ++sz;
  update(root[sz] = null, root[sz - 1], st[u], 1);
  update(root[sz], root[sz], en[u], -1);
}
int getSafeCount(int u, int l, int y, int n) {
  if (dep[u] < dep[l]) {
    return 0;
  }
  assert(st[l] <= st[u]);
  int total = dep[u] - dep[l] + 1;
  return total - query(root[sz], root[y], st[l], st[u]);
}
int getKthSafeUp(int u, int l, int k, int y, int n) {
  if (k <= 0) {
    return -1;
  }
  if (dep[u] < dep[l]) {
    return -1;
  }
  int low = 1;
  int high = dep[u] - dep[l] + 1;
  int poss = -1;
  while (high >= low) {
    int mid = (high + low) / 2;
    int ql = kthAncestor(u, mid - 1);
    if (getSafeCount(u, ql, y, n) < k) {
      low = mid + 1;
    } else {
      poss = ql;
      high = mid - 1;
    }
  }
  return poss;
}
int main() {
  initPST();
  ::root[0] = null;
  int n, m, root;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int p;
    scanf("%d", &p);
    g[p].push_back(i);
    if (p == 0) {
      root = i;
    }
  }
  treeUtility = new TreeUtility(g, n, root);
  pa = treeUtility->pa;
  st = treeUtility->st;
  en = treeUtility->en;
  dep = treeUtility->dep;
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int c;
      scanf("%d", &c);
      attackCity(c, i);
    } else {
      ::root[sz + 1] = ::root[sz];
      ++sz;
      int a, b, k, y;
      scanf("%d %d %d %d", &a, &b, &k, &y);
      int l = lca(a, b);
      if (l == a) {
        int d = dep[b] - dep[l];
        int bl = kthAncestor(b, d - 1);
        int safeDown = getSafeCount(pa[0][b], bl, y, n);
        printf("%d\n", getKthSafeUp(pa[0][b], bl, safeDown - k + 1, y, n));
      } else if (l == b) {
        int d = dep[a] - dep[l];
        int al = kthAncestor(a, d - 1);
        printf("%d\n", getKthSafeUp(pa[0][a], al, k, y, n));
      } else {
        int safeUp = getSafeCount(pa[0][a], l, y, n);
        if (safeUp >= k) {
          printf("%d\n", getKthSafeUp(pa[0][a], l, k, y, n));
        } else {
          k -= safeUp;
          int d = dep[b] - dep[l];
          int bl = kthAncestor(b, d - 1);
          int safeDown = getSafeCount(pa[0][b], bl, y, n);
          printf("%d\n", getKthSafeUp(pa[0][b], bl, safeDown - k + 1, y, n));
        }
      }
    }
  }
}
