#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10, mod = (int)1e9 + 7;
int n, m, ones[N << 5], tree[N << 2], T, root[N], king, pos, num, posarr[N],
    id[N], head[N], sz[N], p[21][N], h[N], le[N << 5], ri[N << 5], ver[N];
vector<int> adj[N];
void pre(int u, int pa, int l) {
  p[0][u] = pa;
  sz[u] = 1;
  h[u] = l;
  for (int v : adj[u]) {
    pre(v, u, l + 1);
    sz[u] += sz[v];
  }
}
void hld(int u) {
  if (!head[num]) {
    head[num] = u;
  }
  int big = -1, child = -1;
  id[u] = num;
  posarr[u] = pos++;
  ver[pos - 1] = u;
  for (int v : adj[u]) {
    if (sz[v] > big) {
      big = sz[v];
      child = v;
    }
  }
  if (child != -1) hld(child);
  for (int v : adj[u]) {
    if (v == child) continue;
    num++;
    hld(v);
  }
}
int lca(int a, int b) {
  if (h[a] > h[b]) swap(a, b);
  for (int j = 20; j >= 0; j--) {
    if (h[b] - (1 << j) >= h[a]) b = p[j][b];
  }
  if (a == b) return a;
  for (int j = 20; j >= 0; j--) {
    if (p[j][a] != p[j][b]) {
      a = p[j][a];
      b = p[j][b];
    }
  }
  return p[0][a];
}
int build(int x, int y, int l = 0, int r = pos - 1) {
  if (l == r) {
    tree[x] = 1;
    return y;
  }
  int mid = (l + r) >> 1;
  le[y] = build(x << 1, T++, l, mid);
  ri[y] = build(x << 1 | 1, T++, mid + 1, r);
  tree[x] = tree[x << 1] + tree[x << 1 | 1];
  ones[y] = ones[le[y]] + ones[ri[y]];
  return y;
}
int upd(int x, int y, int i, int l = 0, int r = pos - 1) {
  int Y = T++;
  if (l == r) {
    tree[x] = 0;
    ones[Y] = 1;
    return Y;
  }
  le[Y] = le[y], ri[Y] = ri[y];
  int mid = (l + r) >> 1;
  if (i <= mid)
    le[Y] = upd(x << 1, le[Y], i, l, mid);
  else
    ri[Y] = upd(x << 1 | 1, ri[Y], i, mid + 1, r);
  tree[x] = tree[x << 1] + tree[x << 1 | 1];
  ones[Y] = ones[le[Y]] + ones[ri[Y]];
  return Y;
}
int query(int x, int y, int i, int j, int l = 0, int r = pos - 1) {
  if (l == r) {
    if (l >= i && l <= j) return tree[x] + ones[y];
    return 0;
  }
  if (i > r || l > j) return 0;
  if (l >= i && r <= j) return tree[x] + ones[y];
  int mid = (l + r) >> 1;
  return query(x << 1, le[y], i, j, l, mid) +
         query(x << 1 | 1, ri[y], i, j, mid + 1, r);
}
int k, y, ans;
void queryup(int u, int v) {
  int uchain, vchain = id[v];
  while (1) {
    uchain = id[u];
    int cur;
    if (uchain == vchain)
      cur = query(1, root[y], posarr[v], posarr[u]);
    else
      cur = query(1, root[y], posarr[head[uchain]], posarr[u]);
    if (cur < k)
      k -= cur;
    else {
      int l = (uchain == vchain ? posarr[v] : posarr[head[uchain]]),
          r = posarr[u], mid;
      while (l <= r) {
        mid = (l + r) >> 1;
        int sum = query(1, root[y], mid, posarr[u]);
        if (sum < k)
          r = mid - 1;
        else {
          if (sum == k) ans = ver[mid];
          l = mid + 1;
        }
      }
      k = 0;
      break;
    }
    if (uchain == vchain) break;
    u = head[uchain];
    u = p[0][u];
  }
}
void querydown(int u, int v) {
  int uchain, vchain = id[v];
  vector<pair<int, int>> segs;
  while (1) {
    uchain = id[u];
    if (uchain == vchain) {
      segs.push_back({posarr[v], posarr[u]});
      break;
    } else
      segs.push_back({posarr[head[uchain]], posarr[u]});
    u = head[uchain];
    u = p[0][u];
  }
  reverse(segs.begin(), segs.end());
  for (int i = 0; i < segs.size(); i++) {
    int l = segs[i].first, r = segs[i].second, mid;
    int cur = query(1, root[y], l, r);
    if (cur < k)
      k -= cur;
    else {
      while (l <= r) {
        mid = (l + r) >> 1;
        int sum = query(1, root[y], segs[i].first, mid);
        if (sum < k)
          l = mid + 1;
        else {
          if (sum == k) ans = ver[mid];
          r = mid - 1;
        }
      }
      k = 0;
      break;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int p;
    scanf("%d", &p);
    if (p)
      adj[p].push_back(i);
    else
      king = i;
  }
  pre(king, king, 0);
  for (int j = 1; j < 21; j++) {
    for (int i = 1; i <= n; i++) p[j][i] = p[j - 1][p[j - 1][i]];
  }
  hld(king);
  root[0] = build(1, T++);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    root[i] = root[i - 1];
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int c;
      scanf("%d", &c);
      root[i] = upd(1, root[i], posarr[c]);
    } else {
      int u, v;
      scanf("%d%d%d%d", &u, &v, &k, &y);
      int LCA = lca(u, v);
      k += query(1, root[y], posarr[u], posarr[u]);
      queryup(u, LCA);
      if (!k && ans != u && ans != v)
        printf("%d\n", ans);
      else {
        k += query(1, root[y], posarr[LCA], posarr[LCA]);
        querydown(v, LCA);
        if (!k)
          printf("%d\n", (ans == v ? -1 : ans));
        else
          printf("-1\n");
      }
    }
  }
  return 0;
}
