#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int cur[MAXN];
vector<int> tree[4 * MAXN];
void build() {}
void add(int i, int l, int r, int x, int y, int val) {
  if (l > y || r < x) return;
  if (l >= x && r <= y) {
    tree[i].push_back(val);
    return;
  }
  int md = (l + r) / 2;
  add(i * 2 + 1, l, md, x, y, val);
  add(i * 2 + 2, md + 1, r, x, y, val);
}
int getMore(int i, int l, int r, int x, int val) {
  if (l > x || r < x) return 0;
  int res = tree[i].size() - (upper_bound(tree[i].begin(), tree[i].end(), val) -
                              tree[i].begin());
  if (l == r) {
    return res;
  }
  res += getMore(i * 2 + 1, l, (l + r) / 2, x, val);
  res += getMore(i * 2 + 2, (l + r) / 2 + 1, r, x, val);
  return res;
}
int n;
int in[MAXN];
int out[MAXN];
vector<int> tr[MAXN];
const int LG = 18;
int p[MAXN][LG];
int L[MAXN];
int LCA(int x, int y) {
  if (L[x] < L[y]) swap(x, y);
  for (int i = LG - 1; i >= 0; i--)
    if (L[x] - (1 << i) >= L[y]) x = p[x][i];
  if (x == y) return x;
  for (int i = LG - 1; i >= 0; i--)
    if (p[x][i] != -1 && p[x][i] != p[y][i]) {
      x = p[x][i];
      y = p[y][i];
    }
  return p[x][0];
}
int T;
void dfs(int i, int par, int d) {
  in[i] = ++T;
  L[i] = d;
  p[i][0] = par;
  for (int j = 1; j < LG; j++) {
    if (p[i][j - 1] == -1) break;
    p[i][j] = p[p[i][j - 1]][j - 1];
  }
  for (int j = 0; j < tr[i].size(); j++) {
    if (tr[i][j] == par) continue;
    dfs(tr[i][j], i, d + 1);
  }
  out[i] = T;
}
void init() {
  T = -1;
  dfs(0, -1, 0);
  build();
  for (int i = 0; i < n; i++) {
    cur[i] = -i - 1;
  }
}
void destroy(int x, int t) {
  cur[x] = t;
  add(0, 0, n - 1, in[x], out[x], cur[x]);
}
int rootToNode(int x, int y) {
  if (x < -1) return 0;
  return L[x] + 1 - getMore(0, 0, n - 1, in[x], y);
}
int getKthParent(int x, int k) {
  int cur = x;
  for (int i = LG - 1; i >= 0; i--) {
    if ((1 << i) <= k) {
      cur = p[cur][i];
      k -= (1 << i);
    }
  }
  return cur;
}
int lca, lcaVal, uVal;
int check(int u, int v, int kth, int y) {
  if (kth < L[u] - L[lca]) {
    return uVal - rootToNode(getKthParent(u, kth + 1), y);
  } else {
    int res = 0;
    if (cur[lca] <= y && lca != u && lca != v) res++;
    if (u != lca) {
      res += uVal;
      res -= 2 * lcaVal;
    } else
      res -= rootToNode(lca, y);
    int nl = L[lca] + kth - (L[u] - L[lca]);
    res += rootToNode(getKthParent(v, L[v] - nl), y);
    return res;
  }
}
int solve(int u, int v, int y, int kth) {
  lca = LCA(u, v);
  uVal = rootToNode(p[u][0], y);
  lcaVal = rootToNode(lca, y);
  int dis = L[u] + L[v] - 2 * L[lca];
  int lo = 1;
  int hi = dis - 1;
  int md;
  while (lo <= hi) {
    md = (lo + hi) / 2;
    if (check(u, v, md, y) < kth)
      lo = md + 1;
    else
      hi = md - 1;
  }
  if (lo >= dis) return -1;
  if (lo < L[u] - L[lca])
    return getKthParent(u, lo) + 1;
  else {
    int nl = L[lca] + lo - (L[u] - L[lca]);
    return getKthParent(v, L[v] - nl) + 1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    if (x < 0) continue;
    tr[i].push_back(x);
    tr[x].push_back(i);
  }
  init();
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int c;
      scanf("%d", &c);
      destroy(c - 1, i + 1);
    } else {
      int a, b, k, y;
      scanf("%d %d %d %d", &a, &b, &k, &y);
      a--;
      b--;
      printf("%d\n", solve(a, b, y, k));
    }
  }
}
