#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, root, tot, t, a, b, c, y, Y, cnt, u, v, A, B, now;
int fa[21][100011], dep[100011], S[100011], E[100011], ent[100011];
int Left[4000011], Right[4000011], stop[4000011];
vector<int> e[100011];
int lca(int a, int b) {
  if (dep[a] > dep[b]) swap(a, b);
  while (dep[a] != dep[b]) b = fa[(int)(log(dep[b] - dep[a]) / log(2))][b];
  if (a == b) return a;
  int i = 18;
  while (fa[0][a] != fa[0][b]) {
    while (fa[i][a] == fa[i][b]) i--;
    a = fa[i][a], b = fa[i][b];
  }
  return fa[0][a];
}
void dfs(int now) {
  S[now] = ++tot;
  int i = 0;
  while (fa[i][fa[i][now]]) {
    fa[i + 1][now] = fa[i][fa[i][now]];
    i++;
  }
  for (i = 0; i < e[now].size(); i++) {
    dep[e[now][i]] = dep[now] + 1;
    fa[0][e[now][i]] = now;
    dfs(e[now][i]);
  }
  E[now] = tot;
}
void build(int s, int e, int now) {
  if (s == e) return;
  int mid = (s + e) / 2;
  Left[now] = ++tot;
  build(s, mid, tot);
  Right[now] = ++tot;
  build(mid + 1, e, tot);
}
void ins(int s, int e, int l, int r, int now, int last) {
  stop[now] = stop[last];
  if (s == l && e == r) {
    stop[now]++;
    Left[now] = Left[last];
    Right[now] = Right[last];
    return;
  }
  int mid = (l + r) / 2;
  if (e <= mid)
    Left[now] = ++tot, Right[now] = Right[last],
    ins(s, e, l, mid, Left[now], Left[last]);
  else if (s > mid)
    Left[now] = Left[last], Right[now] = ++tot,
    ins(s, e, mid + 1, r, Right[now], Right[last]);
  else
    Left[now] = ++tot, Right[now] = ++tot,
    ins(s, mid, l, mid, Left[now], Left[last]),
    ins(mid + 1, e, mid + 1, r, Right[now], Right[last]);
}
int query(int x, int now, int last) {
  if (x == 0) return 0;
  int ans = 0, l = 1, r = n, mid;
  while (1) {
    ans += stop[now] - stop[last];
    if (l == r) return ans;
    mid = (l + r) / 2;
    if (x <= mid)
      r = mid, now = Left[now], last = Left[last];
    else
      l = mid + 1, now = Right[now], last = Right[last];
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    if (k)
      e[k].push_back(i);
    else
      root = i;
  }
  dep[root] = 1;
  dfs(root);
  tot = 0;
  build(1, n, 0);
  scanf("%d", &m);
  for (Y = 1; Y <= m; Y++) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d", &c);
      ent[Y] = ++tot, ins(S[c], E[c], 1, n, tot, ent[Y - 1]);
    } else {
      ent[Y] = ent[Y - 1];
      scanf("%d%d%d%d", &a, &b, &k, &y);
      if (fa[0][a] == b || fa[0][b] == a) {
        printf("-1\n");
        continue;
      }
      c = lca(a, b);
      cnt = dep[a] + dep[b] - dep[c] * 2 + 1;
      u = query(S[a], ent[Y], ent[y]) - query(S[fa[0][a]], ent[Y], ent[y]),
      u = 1 - u;
      v = query(S[b], ent[Y], ent[y]) - query(S[fa[0][b]], ent[Y], ent[y]),
      v = 1 - v;
      A = query(S[a], ent[Y], ent[y]) - query(S[c], ent[Y], ent[y]);
      B = query(S[b], ent[Y], ent[y]) - query(S[fa[0][c]], ent[Y], ent[y]);
      cnt -= A + B;
      if (cnt - u - v < k) {
        printf("-1\n");
        continue;
      }
      if (dep[a] - dep[c] - A - u >= k) {
        i = 18;
        now = a;
        while (1) {
          while (i >= 0 &&
                 dep[a] - dep[fa[0][fa[i][now]]] -
                         (query(S[a], ent[Y], ent[y]) -
                          query(S[fa[0][fa[i][now]]], ent[Y], ent[y])) -
                         u >=
                     k)
            i--;
          if (i < 0) break;
          now = fa[i][now];
        }
        printf("%d\n", fa[0][now]);
      } else {
        i = 18;
        now = b;
        if (a != c) k -= dep[a] - dep[c] - A - u;
        k = dep[b] - dep[c] -
            (query(S[b], ent[Y], ent[y]) - query(S[c], ent[Y], ent[y])) - k;
        if (a != c &&
            query(S[c], ent[Y], ent[y]) - query(S[fa[0][c]], ent[Y], ent[y]) ==
                0)
          k++;
        while (1) {
          while (i >= 0 &&
                 dep[b] - dep[fa[0][fa[i][now]]] -
                         (query(S[b], ent[Y], ent[y]) -
                          query(S[fa[0][fa[i][now]]], ent[Y], ent[y])) >
                     k)
            i--;
          if (i < 0) break;
          now = fa[i][now];
        }
        printf("%d\n", fa[0][now]);
      }
    }
  }
  return 0;
}
