#include <bits/stdc++.h>
using namespace std;
const int tt = 100005;
struct edge {
  edge *nxt;
  int des;
  void maker(edge *&a, int b) {
    nxt = a, des = b;
    a = this;
  }
} w[tt], *sta[tt], *tot;
struct treap {
  treap *l, *r;
  int key, sz, ran;
} * seg[tt * 4], buf[2000000], *pos;
int bg[tt], ed[tt], fa[tt][17], h[tt], ft[tt], now, A, B, Y, n;
void dfs(int i) {
  bg[i] = ++now;
  for (edge *e = sta[i]; e; e = e->nxt) {
    h[e->des] = h[i] + 1;
    dfs(e->des);
  }
  ed[i] = now;
}
void lr(treap *&a) {
  treap *b = a->r;
  a->r = b->l;
  b->l = a;
  b->sz = a->sz;
  a->sz = a->l->sz + a->r->sz + 1;
  a = b;
}
void rr(treap *&a) {
  treap *b = a->l;
  a->l = b->r;
  b->r = a;
  b->sz = a->sz;
  a->sz = a->l->sz + a->r->sz + 1;
  a = b;
}
void ins(treap *&a) {
  if (a == buf) {
    a = ++pos;
    a->l = a->r = buf, a->key = Y, a->sz = 1, a->ran = rand() * 32767 + rand();
  } else {
    a->sz++;
    if (Y > a->key) {
      ins(a->r);
      if (a->r->ran > a->ran) lr(a);
    } else {
      ins(a->l);
      if (a->l->ran > a->ran) rr(a);
    }
  }
}
void ins(int now, int l, int r) {
  if (A <= l && r <= B)
    ins(seg[now]);
  else {
    if (A <= ((l + r) >> 1)) ins(now << 1, l, ((l + r) >> 1));
    if (B > ((l + r) >> 1)) ins(now * 2 + 1, ((l + r) >> 1) + 1, r);
  }
}
void build(int now, int l, int r) {
  seg[now] = buf;
  if (l != r)
    build(now << 1, l, ((l + r) >> 1)),
        build(now * 2 + 1, ((l + r) >> 1) + 1, r);
}
int lca(int a, int b) {
  if (h[a] > h[b]) swap(a, b);
  for (int i = 16; i >= 0; i--)
    if (h[fa[b][i]] >= h[a]) b = fa[b][i];
  if (a == b) return a;
  for (int i = 16; i >= 0; i--)
    if (fa[b][i] != fa[a][i]) b = fa[b][i], a = fa[a][i];
  return fa[a][0];
}
int qry(treap *a) {
  int ans = 0;
  for (; a != buf;)
    if (Y < a->key) {
      ans += a->r->sz + 1;
      a = a->l;
    } else
      a = a->r;
  return ans;
}
int qry(int now, int l, int r, int a) {
  int ans = qry(seg[now]);
  if (l != r)
    ans += a <= ((l + r) >> 1) ? qry(now << 1, l, ((l + r) >> 1), a)
                               : qry(now * 2 + 1, ((l + r) >> 1) + 1, r, a);
  return ans;
}
int qry(int a) { return h[a] + 1 - qry(1, 1, n, bg[a]); }
int getqry(int a, int k, int t) {
  for (int i = 16; i >= 0; i--) {
    int b = qry(fa[a][i]);
    if (t - b < k) {
      k -= t - b, a = fa[a][i];
      t = b;
    }
  }
  return a;
}
int main() {
  int i, j, rt, m, t, a, b, c, k, cnt1, cnt2, cnt3;
  scanf("%d", &n);
  for (now = 0, i = 1; i <= n; i++) sta[i] = NULL, ft[i] = 0;
  for (tot = w, i = 1; i <= n; i++)
    if (scanf("%d", &fa[i][0]), !fa[i][0])
      h[fa[i][0] = rt = i] = 0;
    else
      (tot++)->maker(sta[fa[i][0]], i);
  dfs(rt);
  for (i = 1; i <= 16; i++)
    for (j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
  build(1, 1, n), buf->sz = 0, pos = buf;
  for (scanf("%d", &m), i = 1; i <= m; i++) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d", &a);
      A = bg[a], B = ed[a], ft[a] = Y = i;
      ins(1, 1, n);
    } else {
      scanf("%d%d%d%d", &a, &b, &k, &Y);
      c = lca(a, b);
      cnt1 = qry(a), cnt2 = qry(c), cnt3 = qry(b);
      if (cnt1 + cnt3 - 2 * cnt2 + (ft[(c)] <= Y) - (ft[(a)] <= Y) -
              (ft[(b)] <= Y) <
          k) {
        puts("-1");
        continue;
      }
      if (cnt1 - cnt2 - (ft[(a)] <= Y) >= k)
        k = getqry(a, k, cnt1 - (ft[(a)] <= Y));
      else {
        k = getqry(b,
                   cnt1 + cnt3 - 2 * cnt2 + (ft[(c)] <= Y) - (ft[(a)] <= Y) -
                       (ft[(b)] <= Y) - k + 1,
                   cnt3 - (ft[(b)] <= Y));
      }
      printf("%d\n", k);
    }
  }
  return 0;
}
