#include <bits/stdc++.h>
using namespace std;
int n, i, dad[100010][21], root1, tim[100010], root[100010], m, K, cs[100010],
    t, x, t2, t1;
int v[100010], ne[100010], head[100010], l, a[100010], b[100010], k[100010],
    y[100010];
int newpoint, num[5000010], lc[5000010], rc[5000010], mid;
void add(int x, int y) {
  v[++l] = y;
  ne[l] = head[x];
  head[x] = l;
}
int lca(int x, int y) {
  if (cs[x] > cs[y]) x ^= y ^= x ^= y;
  for (t = K; t >= 0; t--)
    if (cs[dad[y][t]] >= cs[x]) y = dad[y][t];
  if (y == x) return x;
  for (t = K; t >= 0; t--)
    if (dad[y][t] != dad[x][t]) y = dad[y][t], x = dad[x][t];
  return dad[x][0];
}
void build0(int x, int &cur, int cur1, int L, int R) {
  cur = ++newpoint;
  num[cur] = num[cur1] + 1;
  lc[cur] = lc[cur1];
  rc[cur] = rc[cur1];
  if (L == R) return;
  mid = (L + R) >> 1;
  if (x <= mid)
    build0(x, lc[cur], lc[cur1], L, mid);
  else
    build0(x, rc[cur], rc[cur1], mid + 1, R);
}
void dfs(int x) {
  if (tim[x])
    build0(tim[x], root[x], root[dad[x][0]], 1, m);
  else
    root[x] = root[dad[x][0]];
  cs[x] = cs[dad[x][0]] + 1;
  for (t = 0; dad[x][t]; t++) dad[x][t + 1] = dad[dad[x][t]][t];
  if (t > K) K = t - 1;
  for (int i = head[x]; i; i = ne[i]) dfs(v[i]);
}
int ask(int x, int y, int cur, int cur1, int L, int R) {
  if (x <= L && y >= R) return num[cur] - num[cur1];
  int mid = (L + R) >> 1, ss = 0;
  if (x <= mid) ss += ask(x, y, lc[cur], lc[cur1], L, mid);
  if (y > mid) ss += ask(x, y, rc[cur], rc[cur1], mid + 1, R);
  return ss;
}
int find(int x, int k) {
  int t, s;
  k--;
  for (t = K; t >= 0; t--)
    if ((s = (cs[x] - cs[dad[x][t]] -
              ask(y[i], i, root[x], root[dad[x][t]], 1, m))) <= k)
      k -= s, x = dad[x][t];
  return x;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &dad[i][0]);
    add(dad[i][0], i), root1 += (!dad[i][0]) * i;
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d", &t);
    if (t - 1)
      scanf("%d%d%d%d", &a[i], &b[i], &k[i], &y[i]), y[i]++;
    else
      scanf("%d", &x), tim[x] = i;
  }
  dfs(root1);
  for (i = 1; i <= m; i++)
    if (a[i]) {
      t = lca(a[i], b[i]);
      t1 = (cs[a[i]] - cs[dad[t][0]] -
            ask(y[i], i, root[a[i]], root[dad[t][0]], 1, m));
      t2 = (cs[b[i]] - cs[dad[t][0]] -
            ask(y[i], i, root[b[i]], root[dad[t][0]], 1, m));
      if (tim[a[i]] < y[i] || tim[a[i]] > i) t1--;
      if (tim[b[i]] < y[i] || tim[b[i]] > i) t2--;
      if (tim[t] < y[i] || tim[t] > i) t2--;
      if (t1 + t2 < k[i])
        printf("-1\n");
      else if (t1 < k[i]) {
        if (tim[b[i]] < y[i] || tim[b[i]] > i) t2++;
        printf("%d\n", find(b[i], t1 + t2 - k[i] + 1));
      } else {
        if (tim[a[i]] < y[i] || tim[a[i]] > i) k[i]++;
        printf("%d\n", find(a[i], k[i]));
      }
    }
}
