#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int lg = 20;
int L[maxn * lg * 6], R[maxn * lg * 6], root[maxn * 6], s[maxn * lg * 6];
int n, q;
int par[maxn][lg];
vector<int> v[maxn];
int cnt = 0;
int st[maxn], fn[maxn];
int IR = 0;
int NXT = 1;
int TIME = 1;
int h[maxn];
void dfs(int x) {
  h[x] = h[par[x][0]] + 1;
  st[x] = cnt++;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != par[x][0]) dfs(v[x][i]);
  fn[x] = cnt;
}
void make_tree(int x = IR, int l = 0, int r = n) {
  if (r - l < 2) return;
  int mid = (l + r) / 2;
  L[x] = NXT++;
  R[x] = NXT++;
  make_tree(L[x], l, mid);
  make_tree(R[x], mid, r);
}
int modify(int ql, int qr, int x, int l = 0, int r = n) {
  int jad = NXT++;
  s[jad] = s[x];
  L[jad] = L[x];
  R[jad] = R[x];
  if (ql <= l && r <= qr) {
    s[jad]++;
    return jad;
  }
  int mid = (l + r) / 2;
  if (ql < mid) L[jad] = modify(ql, qr, L[x], l, mid);
  if (qr > mid) R[jad] = modify(ql, qr, R[x], mid, r);
  return jad;
}
int get(int p, int x, int l = 0, int r = n) {
  if (r - l < 2) return s[x];
  int mid = (l + r) / 2;
  if (p < mid)
    return s[x] + get(p, L[x], l, mid);
  else
    return s[x] + get(p, R[x], mid, r);
}
int salem(int a, int b, int y) {
  int aa = get(st[a], root[TIME]) - get(st[a], root[y]);
  int bb = get(st[b], root[TIME]) - get(st[b], root[y]);
  return h[a] - h[b] - (aa - bb);
}
int lca(int a, int b) {
  if (h[a] < h[b]) swap(a, b);
  for (int i = lg - 1; i >= 0; i--)
    if (h[par[a][i]] >= h[b]) a = par[a][i];
  if (a == b) return a;
  for (int i = lg - 1; i >= 0; i--)
    if (par[a][i] != par[b][i]) {
      a = par[a][i];
      b = par[b][i];
    }
  return par[a][0];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    par[i][0] = x;
    v[x].push_back(i);
  }
  dfs(0);
  for (int i = 1; i < lg; i++)
    for (int j = 0; j <= n; j++) par[j][i] = par[par[j][i - 1]][i - 1];
  n++;
  make_tree();
  root[1] = root[0] = IR;
  cin >> q;
  for (TIME = 1; TIME <= q; TIME++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      cin >> x;
      root[TIME] = modify(st[x], fn[x], root[TIME - 1]);
    }
    if (t == 2) {
      root[TIME] = root[TIME - 1];
      int a, b, k, y;
      cin >> a >> b >> k >> y;
      int kuft = k;
      if (salem(a, par[a][0], y)) k++;
      int isb = 0;
      if (salem(b, par[b][0], y)) isb++;
      int mid = lca(a, b);
      if (salem(a, par[mid][0], y) + salem(b, mid, y) - isb < k) {
        cout << -1 << endl;
        continue;
      }
      if (salem(a, par[mid][0], y) >= k) {
        k = salem(a, par[mid][0], y) - k + 1;
        int ans = a, pp = par[mid][0];
        for (int i = lg - 1; i >= 0; i--)
          if (h[par[ans][i]] > h[pp] && salem(par[ans][i], pp, y) >= k)
            ans = par[ans][i];
        cout << ans << endl;
      } else {
        k -= salem(a, par[mid][0], y);
        int ans = b;
        for (int i = lg - 1; i >= 0; i--)
          if (h[par[ans][i]] >= h[mid] && salem(par[ans][i], mid, y) >= k)
            ans = par[ans][i];
        cout << ans << endl;
      }
    }
  }
}
