#include <bits/stdc++.h>
using namespace std;
const int N = 100004;
inline void gi(int &x) {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  for (x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + (c ^ 48);
}
int n, m, x, y, k, year, opt, ans;
vector<int> E[N];
int tot, rt[N];
struct node {
  int lc, rc, sum;
} t[N * 30];
int f[N][17], dep[N], son[N], sz[N], fa[N], top[N], dfn[N], dfnn, rk[N];
void dfs(int u, int father) {
  sz[u] = 1;
  fa[u] = father;
  f[u][0] = fa[u];
  for (int i = 1; i < 17; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (auto v : E[u])
    if (v != father) {
      dep[v] = dep[u] + 1;
      dfs(v, u);
      sz[u] += sz[v];
      if (sz[v] > sz[son[u]]) son[u] = v;
    }
}
void pull(int u, int Top) {
  top[u] = Top;
  dfn[u] = ++dfnn;
  rk[dfnn] = u;
  if (son[u]) pull(son[u], Top);
  for (auto v : E[u])
    if (v != fa[u] && v != son[u]) pull(v, v);
}
inline 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 dep[u] < dep[v] ? u : v;
}
inline int jump(int u, int x) {
  for (int i = 0; x; x >>= 1, i++)
    if (x & 1) u = f[u][i];
  return u;
}
void build(int &u, int l, int r) {
  if (!u) u = ++tot;
  if (l == r) {
    t[u].sum = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(t[u].lc, l, mid);
  build(t[u].rc, mid + 1, r);
  t[u].sum = t[t[u].lc].sum + t[t[u].rc].sum;
}
void upd(int &u, int l, int r, int x, int i) {
  t[++tot] = t[u];
  u = tot;
  t[u].sum += i;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (mid >= x)
    upd(t[u].lc, l, mid, x, i);
  else
    upd(t[u].rc, mid + 1, r, x, i);
}
int que(int u, int v, int l, int r, int L, int R) {
  if (l > R || r < L) return 0;
  if (l >= L && r <= R) return r - l + 1 - (t[v].sum - t[u].sum);
  int mid = (l + r) >> 1;
  return que(t[u].lc, t[v].lc, l, mid, L, R) +
         que(t[u].rc, t[v].rc, mid + 1, r, L, R);
}
int kth(int u, int v, int l, int r, int k) {
  if (l == r) return l;
  int mid = (l + r) >> 1;
  int f = mid - l + 1 - (t[t[v].lc].sum - t[t[u].lc].sum);
  if (f >= k)
    return kth(t[u].lc, t[v].lc, l, mid, k);
  else
    return kth(t[u].rc, t[v].rc, mid + 1, r, k - f);
}
vector<pair<int, int> > seqA, seqB;
void find(int u, int v, int k, int no1, int no2) {
  seqA.clear();
  seqB.clear();
  while (top[u] != top[v]) {
    if (dep[top[u]] >= dep[top[v]]) {
      seqA.push_back(make_pair(dfn[top[u]], dfn[u]));
      u = fa[top[u]];
    } else {
      seqB.push_back(make_pair(dfn[top[v]], dfn[v]));
      v = fa[top[v]];
    }
  }
  if (dep[u] >= dep[v])
    seqA.push_back(make_pair(dfn[v], dfn[u]));
  else
    seqB.push_back(make_pair(dfn[u], dfn[v]));
  reverse(seqB.begin(), seqB.end());
  int cnt = 0;
  for (auto q : seqA) {
    int tmp = que(rt[no1], rt[no2], 1, n, q.first, q.second);
    if (cnt + tmp >= k) {
      int i = cnt + tmp - k + 1 + que(rt[no1], rt[no2], 1, n, 1, q.first - 1);
      printf("%d\n", rk[kth(rt[no1], rt[no2], 1, n, i)]);
      return;
    }
    cnt += tmp;
  }
  for (auto q : seqB) {
    int tmp = que(rt[no1], rt[no2], 1, n, q.first, q.second);
    if (cnt + tmp >= k) {
      int i = k - cnt + que(rt[no1], rt[no2], 1, n, 1, q.first - 1);
      printf("%d\n", rk[kth(rt[no1], rt[no2], 1, n, i)]);
      return;
    }
    cnt += tmp;
  }
  puts("-1");
}
int main() {
  gi(n);
  for (int i = 1; i <= n; i++) {
    gi(x);
    if (x) {
      E[x].push_back(i);
      E[i].push_back(x);
    }
  }
  dep[1] = 1;
  dfs(1, 0);
  pull(1, 1);
  gi(m);
  for (int i = 1; i <= m; i++) {
    rt[i] = rt[i - 1];
    gi(opt);
    gi(x);
    if (opt == 1) {
      upd(rt[i], 1, n, dfn[x], 1);
    } else {
      gi(y);
      gi(k);
      gi(year);
      int lca = LCA(x, y);
      if (dep[x] + dep[y] - 2 * dep[lca] <= 1)
        puts("-1");
      else {
        int u, v;
        if (lca == x) {
          u = jump(y, dep[y] - dep[x] - 1);
          v = fa[y];
        } else if (lca == y) {
          u = fa[x];
          v = jump(x, dep[x] - dep[y] - 1);
        } else {
          u = fa[x];
          v = fa[y];
        }
        find(u, v, k, year, i);
      }
    }
  }
  return 0;
}
