#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 500;
int n, m, dep[N], fa[N][20], seed, st[N], ed[N], rs[N * 40], ls[N * 40],
    sum[N * 40], val[N], cnt, rt[N];
vector<int> To[N];
void dfs(int t) {
  for (int i = 1; i <= 19; i++) fa[t][i] = fa[fa[t][i - 1]][i - 1];
  st[t] = ++seed;
  for (int i = 0; i <= (int)To[t].size() - 1; i++) {
    dep[To[t][i]] = dep[t] + 1;
    dfs(To[t][i]);
  }
  ed[t] = ++seed;
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int t = dep[x] - dep[y];
  for (int i = 0; t; t >>= 1, ++i)
    if (t & 1) x = fa[x][i];
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (fa[x][i] ^ fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
void add(int x, int &y, int l, int r, int pos, int dt) {
  y = ++cnt;
  sum[y] = sum[x] + dt;
  if (l == r) return;
  int mi = (l + r) >> 1;
  ls[y] = ls[x], rs[y] = rs[x];
  if (pos <= mi)
    add(ls[x], ls[y], l, mi, pos, dt);
  else
    add(rs[x], rs[y], mi + 1, r, pos, dt);
}
int query(int x, int y, int l, int r, int Lim) {
  if (r <= Lim) return sum[y] - sum[x];
  int mi = (l + r) >> 1;
  if (Lim <= mi)
    return query(ls[x], ls[y], l, mi, Lim);
  else
    return sum[ls[y]] - sum[ls[x]] + query(rs[x], rs[y], mi + 1, r, Lim);
}
void work(int r1, int r2, int x, int y, int K) {
  for (int i = 19; i >= 0; i--)
    if (fa[x][i] && dep[fa[x][i]] >= dep[y]) {
      int t1 = dep[fa[x][i]] - query(r1, r2, 1, n << 1, st[fa[x][i]]) - 1;
      int t2 = dep[x] - query(r1, r2, 1, n << 1, st[x]) - 1;
      if (K > t2 - t1) K -= t2 - t1, x = fa[x][i];
    }
  printf("%d\n", fa[x][0]);
}
int main() {
  scanf("%d", &n);
  int Rt;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &fa[i][0]);
    if (!fa[i][0]) Rt = i;
    To[fa[i][0]].push_back(i);
  }
  dep[Rt] = 1;
  dfs(Rt);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int kd, x, y, K, t;
    scanf("%d%d", &kd, &x);
    if (kd == 1) {
      int tmp;
      val[x] = i;
      add(rt[i - 1], tmp, 1, n << 1, st[x] + 1, 1);
      add(tmp, rt[i], 1, n << 1, ed[x], -1);
    } else {
      scanf("%d%d%d", &y, &K, &t);
      rt[i] = rt[i - 1];
      int sx = dep[x] - query(rt[t], rt[i], 1, n << 1, st[x]) - 1;
      int sy = dep[y] - query(rt[t], rt[i], 1, n << 1, st[y]) - 1;
      int z = lca(x, y);
      int _sz = dep[z] - query(rt[t], rt[i], 1, n << 1, st[z]) - 1;
      if (sx + sy - _sz * 2 - (val[z] <= t) < K) {
        puts("-1");
        continue;
      } else {
        if (sx - _sz < K)
          work(rt[t], rt[i], y, z, sx + sy - _sz * 2 - (val[z] <= t) - K + 1);
        else
          work(rt[t], rt[i], x, z, K);
      }
    }
  }
  return 0;
}
