#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int LOG = 20;
vector<int> sum, L, R, adj[N];
int LVL[N], P[N][LOG];
int heavy[N], chainHead[N], toST[N], fromST[N], treeRoot;
int root[N], n;
int copy(int i = -1) {
  sum.push_back(i < 0 ? 0 : sum[i]);
  L.push_back(i < 0 ? 0 : L[i]);
  R.push_back(i < 0 ? 0 : R[i]);
  return (int)L.size() - 1;
}
int update(int p, int index, int l = 1, int r = n) {
  p = copy(p);
  sum[p]++;
  if (l == r) return p;
  int mid = (l + r) >> 1;
  if (index <= mid) {
    int temp = update(L[p], index, l, mid);
    L[p] = temp;
  } else {
    int temp = update(R[p], index, mid + 1, r);
    R[p] = temp;
  }
  return p;
}
int sumQuery(int p1, int p2, int a, int b, int l = 1, int r = n) {
  if (b < l || r < a) return 0;
  if (a <= l && r <= b) return r - l + 1 - (sum[p2] - sum[p1]);
  int mid = (l + r) >> 1;
  return sumQuery(L[p1], L[p2], a, b, l, mid) +
         sumQuery(R[p1], R[p2], a, b, mid + 1, r);
}
int BSQuery(int p1, int p2, int &k, int a, int b, int l = 1, int r = n) {
  if (b < l || r < a) return -1;
  int mid = (l + r) >> 1;
  if (a <= l && r <= b) {
    int ret = r - l + 1 - (sum[p2] - sum[p1]);
    if (ret < k) {
      k -= ret;
      return -1;
    }
    if (l == r) return l;
    ret = r - mid - (sum[R[p2]] - sum[R[p1]]);
    if (ret < k) {
      k -= ret;
      return BSQuery(L[p1], L[p2], k, a, b, l, mid);
    } else
      return BSQuery(R[p1], R[p2], k, a, b, mid + 1, r);
  }
  int ret = BSQuery(R[p1], R[p2], k, a, b, mid + 1, r);
  if (ret == -1) ret = BSQuery(L[p1], L[p2], k, a, b, l, mid);
  return ret;
}
int pre_dfs(int i = treeRoot, int p = 0) {
  int W = 1, maxChild = -1;
  LVL[i] = LVL[p] + 1;
  P[i][0] = p;
  for (int j = 1; j < 18; j++) P[i][j] = P[P[i][j - 1]][j - 1];
  for (auto x : adj[i]) {
    int child = pre_dfs(x, i);
    if (child > maxChild) maxChild = child, heavy[i] = x;
    W += child;
  }
  return W;
}
void buildHLD() {
  for (int i = 1; i <= n; i++) heavy[i] = -1;
  pre_dfs();
  int cnt = 1;
  for (int i = 1; i <= n; i++)
    if (P[i][0] == 0 || heavy[P[i][0]] != i)
      for (int j = i; j != -1; j = heavy[j])
        chainHead[j] = i, toST[j] = cnt, fromST[cnt] = j, cnt++;
  copy();
}
int LCA(int u, int v) {
  while (chainHead[u] != chainHead[v]) {
    if (LVL[chainHead[u]] < LVL[chainHead[v]]) swap(u, v);
    u = P[chainHead[u]][0];
  }
  if (LVL[u] < LVL[v]) swap(u, v);
  return v;
}
int getPar(int u, int k) {
  for (int i = 0; i < 18; i++)
    if (k >> i & 1) u = P[u][i];
  return u;
}
int sumPathQuery(int u, int v, int p1, int p2) {
  int sumRes = 0;
  while (chainHead[u] != chainHead[v]) {
    sumRes += sumQuery(p1, p2, toST[chainHead[u]], toST[u]);
    u = P[chainHead[u]][0];
  }
  sumRes += sumQuery(p1, p2, toST[v], toST[u]);
  return sumRes;
}
int getKth(int u, int v, int k, int p1, int p2) {
  while (chainHead[u] != chainHead[v]) {
    int sumRet = sumQuery(p1, p2, toST[chainHead[u]], toST[u]);
    if (sumRet < k) {
      k -= sumRet;
      u = P[chainHead[u]][0];
      continue;
    }
    return BSQuery(p1, p2, k, toST[chainHead[u]], toST[u]);
  }
  assert(sumQuery(p1, p2, toST[v], toST[u]) >= k);
  return BSQuery(p1, p2, k, toST[v], toST[u]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    if (p)
      adj[p].push_back(i);
    else
      treeRoot = i;
  }
  buildHLD();
  int curRoot = 0;
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      int c;
      cin >> c;
      curRoot = update(curRoot, toST[c]);
    } else {
      int a, b, k, y;
      cin >> a >> b >> k >> y;
      int p1 = root[y], p2 = curRoot;
      int lca = LCA(a, b);
      int ans = -1;
      if (a != lca && (P[a][0] != lca || lca != b)) {
        int t = lca;
        if (lca == b) t = getPar(a, LVL[a] - LVL[lca] - 1);
        int sumA = sumPathQuery(P[a][0], t, p1, p2);
        if (sumA < k)
          k -= sumA;
        else
          ans = getKth(P[a][0], t, k, p1, p2);
      }
      if (ans == -1 && b != lca && P[b][0] != lca) {
        int t = getPar(b, LVL[b] - LVL[lca] - 1);
        int sumB = sumPathQuery(P[b][0], t, p1, p2);
        if (sumB >= k) ans = getKth(P[b][0], t, sumB - k + 1, p1, p2);
      }
      if (ans == -1)
        cout << "-1" << endl;
      else
        cout << fromST[ans] << endl;
    }
    root[i] = curRoot;
  }
  return 0;
}
