#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
inline void read(int &x) {
  char ch = getchar();
  int f = 1;
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
int n, m, dep[100050], id[100050], idcnt, tcnt, top[100050], fa[100050],
    pos[100050], size[100050], son[100050], root[100050], rt;
struct tnode {
  int size, ls, rs;
} t[100050 << 5];
vector<int> adj[100050];
vector<pair<int, int> > itv;
void dfs1(int u) {
  size[u] = 1, dep[u] = dep[fa[u]] + 1;
  for (auto v : adj[u]) {
    fa[v] = u, dfs1(v), size[u] += size[v];
    if (size[v] > size[son[u]]) son[u] = v;
  }
}
void dfs2(int u, int t) {
  pos[id[u] = ++idcnt] = u, top[u] = t;
  if (!son[u]) return;
  dfs2(son[u], t);
  for (auto v : adj[u])
    if (!id[v]) dfs2(v, v);
}
void ins(int &u, int l, int r, int x) {
  int tmp = u;
  u = ++tcnt, t[u] = t[tmp];
  ++t[u].size;
  if (l == r) return;
  if (x <= ((l + r) >> 1))
    ins(t[u].ls, l, ((l + r) >> 1), x);
  else
    ins(t[u].rs, ((l + r) >> 1) + 1, r, x);
}
int getl(int u, int p, int l, int r, int k) {
  if (l == r) return l;
  int lsize = (((l + r) >> 1) - l + 1 - (t[t[u].ls].size - t[t[p].ls].size));
  if (k <= lsize) return getl(t[u].ls, t[p].ls, l, ((l + r) >> 1), k);
  return getl(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, k - lsize);
}
pair<int, int> queryl(int u, int p, int l, int r, int tl, int tr, int k) {
  if (tl > tr) return make_pair(0, 0);
  if (tl <= l && r <= tr) {
    int siz = r - l + 1 - (t[u].size - t[p].size);
    if (siz < k) return make_pair(0, siz);
    return make_pair(1, getl(u, p, l, r, k));
  }
  if (tr <= ((l + r) >> 1))
    return queryl(t[u].ls, t[p].ls, l, ((l + r) >> 1), tl, tr, k);
  if (tl > ((l + r) >> 1))
    return queryl(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, tl, tr, k);
  pair<int, int> res = queryl(t[u].ls, t[p].ls, l, ((l + r) >> 1), tl, tr, k);
  if (res.first) return res;
  pair<int, int> res2 =
      queryl(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, tl, tr, k - res.second);
  if (res2.first) return res2;
  return make_pair(0, res.second + res2.second);
}
int getr(int u, int p, int l, int r, int k) {
  if (l == r) return l;
  int rsize = (r - ((l + r) >> 1) - (t[t[u].rs].size - t[t[p].rs].size));
  if (k <= rsize) return getr(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, k);
  return getr(t[u].ls, t[p].ls, l, ((l + r) >> 1), k - rsize);
}
pair<int, int> queryr(int u, int p, int l, int r, int tl, int tr, int k) {
  if (tl > tr) return make_pair(0, 0);
  if (tl <= l && r <= tr) {
    int siz = r - l + 1 - (t[u].size - t[p].size);
    if (siz < k) return make_pair(0, siz);
    return make_pair(1, getr(u, p, l, r, k));
  }
  if (tr <= ((l + r) >> 1))
    return queryr(t[u].ls, t[p].ls, l, ((l + r) >> 1), tl, tr, k);
  if (tl > ((l + r) >> 1))
    return queryr(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, tl, tr, k);
  pair<int, int> res =
      queryr(t[u].rs, t[p].rs, ((l + r) >> 1) + 1, r, tl, tr, k);
  if (res.first) return res;
  pair<int, int> res2 =
      queryr(t[u].ls, t[p].ls, l, ((l + r) >> 1), tl, tr, k - res.second);
  if (res2.first) return res2;
  return make_pair(0, res.second + res2.second);
}
int _lca(int u, int v) {
  while (top[u] != top[v]) {
    if (dep[top[u]] < dep[top[v]]) swap(u, v);
    u = fa[top[u]];
  }
  return id[u] > id[v] ? v : u;
}
int Query(int u, int v, int k, int y1, int y2) {
  int lca = _lca(u, v);
  pair<int, int> res;
  itv.clear();
  while (top[u] != top[lca])
    itv.push_back(make_pair(id[top[u]], id[u])), u = fa[top[u]];
  itv.push_back(make_pair(id[lca], id[u]));
  for (auto cur : itv) {
    res = queryr(root[y1], root[y2], 1, n, cur.first, cur.second, k);
    if (res.first)
      return res.second;
    else
      k -= res.second;
  }
  itv.clear();
  while (top[v] != top[lca])
    itv.push_back(make_pair(id[top[v]], id[v])), v = fa[top[v]];
  if (v != lca) itv.push_back(make_pair(id[lca] + 1, id[v]));
  reverse(itv.begin(), itv.end());
  for (auto cur : itv) {
    res = queryl(root[y1], root[y2], 1, n, cur.first, cur.second, k);
    if (res.first)
      return res.second;
    else
      k -= res.second;
  }
  return -1;
}
int main() {
  int opt, u, v, k, y;
  read(n);
  for (register int i = 1; i <= n; ++i)
    read(u), u == 0 ? void(rt = i) : adj[u].push_back(i);
  dfs1(rt), dfs2(rt, rt);
  read(m);
  for (register int i = 1; i <= m; ++i) {
    read(opt);
    root[i] = root[i - 1];
    if (opt == 1) {
      read(u), ins(root[i], 1, n, id[u]);
    } else {
      read(u), read(v), read(k), read(y);
      if (queryl(root[i], root[y], 1, n, id[u], id[u], 1).first) ++k;
      int res = Query(u, v, k, i, y);
      if (res == -1 || pos[res] == v)
        printf("-1\n");
      else
        printf("%d\n", pos[res]);
    }
  }
  return 0;
}
