#include <bits/stdc++.h>
int lst[100005], to[200005], pre[200005], tot;
int st[100005], en[100005], t;
int fa[100005][25], dep[100005], num[100005];
int rt[100005], lson[3000005], rson[3000005], sum[3000005], t_cnt;
inline void add_edge(int u, int v) {
  to[tot] = v;
  pre[tot] = lst[u];
  lst[u] = tot++;
}
void dfs(int u, int f = -1) {
  fa[u][0] = f;
  for (int i = 1; i < 20; i++)
    fa[u][i] = fa[u][i - 1] == -1 ? -1 : fa[fa[u][i - 1]][i - 1];
  st[u] = t++;
  for (int i = lst[u]; ~i; i = pre[i]) {
    dep[to[i]] = dep[u] + 1;
    dfs(to[i], u);
  }
  en[u] = t - 1;
}
inline int lca(int u, int v) {
  if (dep[u] > dep[v]) std::swap(u, v);
  for (int i = 0; i < 20; i++) {
    if (dep[v] - dep[u] & (1 << i)) v = fa[v][i];
  }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (fa[u][i] != fa[v][i]) {
      u = fa[u][i];
      v = fa[v][i];
    }
  }
  return fa[u][0];
}
void modify(int v, int &u, int l, int r, int L, int R, int x) {
  u = ++t_cnt;
  lson[u] = lson[v];
  rson[u] = rson[v];
  sum[u] = sum[v];
  if (L <= l && r <= R) {
    sum[u] += x;
    return;
  }
  int m = l + r >> 1;
  if (L <= m) modify(lson[v], lson[u], l, m, L, R, x);
  if (m < R) modify(rson[v], rson[u], m + 1, r, L, R, x);
}
int query(int u, int l, int r, int x) {
  if (!u) return 0;
  if (l == r) return sum[u];
  int m = l + r >> 1;
  if (x <= m) return query(lson[u], l, m, x) + sum[u];
  return query(rson[u], m + 1, r, x) + sum[u];
}
int calc(int x, int y, int u, int v) {
  int res = query(rt[x], 0, t - 1, st[u]) - query(rt[y], 0, t - 1, st[u]);
  if (~v) res -= query(rt[x], 0, t - 1, st[v]) - query(rt[y], 0, t - 1, st[v]);
  return res;
}
int main() {
  memset(lst, -1, sizeof(lst));
  int n, tree_rt;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (!x)
      tree_rt = i;
    else
      add_edge(--x, i);
  }
  dfs(tree_rt);
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int tp;
    scanf("%d", &tp);
    if (tp == 1) {
      int u;
      scanf("%d", &u);
      num[--u] = i;
      modify(rt[i - 1], rt[i], 0, t - 1, st[u], en[u], 1);
    } else {
      rt[i] = rt[i - 1];
      int u, v, k, y;
      scanf("%d%d%d%d", &u, &v, &k, &y);
      int x = lca(--u, --v);
      k += num[u] <= y;
      int ou = u, ov = v;
      int ux = dep[u] - dep[x] + 1 - calc(i, y, u, fa[x][0]),
          xv = dep[v] - dep[x] - calc(i, y, v, x);
      if (ux + xv < k) {
        puts("-1");
        continue;
      }
      if (ux < k) {
        u = v;
        k = ux + xv - k + 1;
      }
      for (int j = 19; j >= 0; j--) {
        v = fa[u][j];
        if (-1 == v) continue;
        int res = dep[u] - dep[v] - calc(i, y, u, v);
        if (res < k) {
          k -= res;
          u = v;
        }
      }
      if (u == ou || u == ov)
        puts("-1");
      else
        printf("%d\n", u + 1);
    }
  }
  return 0;
}
