#include <bits/stdc++.h>
const long double PI(acosl(-1.0));
long double eps = 1e-9;
using namespace std;
const int N = 8 * 1e5;
struct tree {
  tree* l;
  tree* r;
  long long mx;
  tree() : l{NULL}, r{NULL}, mx{0} {}
  tree(long long x) : mx{x} {}
  tree(tree* a) {
    l = a->l;
    r = a->r;
    mx = a->mx;
  }
  void calc() {
    if (this == NULL) return;
    mx = 0;
    if (l != NULL) mx += l->mx;
    if (r != NULL) mx += r->mx;
  }
};
tree* build(tree* v, long long l, long long r) {
  v = new tree();
  if (l == r) {
    return v;
  }
  v->l = build(v->l, l, (l + r) / 2);
  v->r = build(v->r, (l + r) / 2 + 1, r);
  v->calc();
  return v;
}
tree* modi(tree* v, long long l, long long r, long long x, long long o) {
  tree* nv = new tree(v);
  if (x < l || x > r) return nv;
  if (l == r) {
    nv->mx += o;
  } else {
    nv->l = modi(nv->l, l, (l + r) / 2, x, o);
    nv->r = modi(nv->r, (l + r) / 2 + 1, r, x, o);
    nv->calc();
  }
  return nv;
}
long long find(tree* v, long long l, long long r, long long l1, long long r1) {
  if (v == NULL) return 0;
  if (l > r1 || r < l1) return 0;
  if (l >= l1 && r <= r1) return v->mx;
  return (find(v->l, l, (l + r) / 2, l1, r1) +
          find(v->r, (l + r) / 2 + 1, r, l1, r1));
}
struct zap {
  int st, fn, k, y;
};
vector<vector<int>> g(N), pr(N);
int n, m, nm;
int tin[N];
int tout[N];
int tm1[N];
zap inp[N];
tree* t[N];
int curT;
void dfs(int v, int pred) {
  pr[v].resize(20);
  pr[v][0] = pred;
  for (int i(1); i < 20; i++) {
    int to = pr[v][i - 1];
    if (to == -1)
      pr[v][i] = -1;
    else
      pr[v][i] = pr[to][i - 1];
  }
  tin[v] = curT++;
  int x = tm1[v];
  if (x == 0) x = nm;
  t[v] = modi(t[v], 1, nm, x, 1);
  for (int i(0); i < g[v].size(); i++) {
    int to = g[v][i];
    t[to] = new tree(t[v]);
    dfs(to, v);
  }
  tout[v] = curT++;
}
bool check(int x, int y) {
  if (x == -1) return true;
  if (tin[x] <= tin[y] && tout[x] >= tout[y]) return true;
  return false;
}
int lca(int x, int y) {
  if (check(x, y)) return x;
  if (check(y, x)) return y;
  while (true)
    for (int i(0); i < 20; i++) {
      int to = pr[x][i];
      if (i == 0 && check(to, y)) return to;
      if (check(to, y)) {
        x = pr[x][i - 1];
        break;
      }
    }
}
int lcaCnt(int v, int fn) {
  if (v == fn) return 0;
  int ans = 0;
  while (true)
    for (int i(0); i < 20; i++) {
      int to = pr[v][i];
      if (i == 0 && to == fn) return ans;
      if (check(to, fn)) {
        v = pr[v][i - 1];
        ans += 1 << (i - 1);
        break;
      }
    }
}
int cntGood(int v, int l, int r) {
  if (v == -1) return 0;
  int x = find(t[v], 1, nm, 1, l - 1) + find(t[v], 1, nm, r + 1, nm);
  return x;
}
int give(int v, int k, int l, int r) {
  int sm = cntGood(v, l, r);
  for (int i(19); i >= 0; i--)
    if (sm - cntGood(pr[v][i], l, r) < k) v = pr[v][i];
  return v;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(unsigned(time(NULL)));
  cin >> n;
  int king = -1;
  for (int i(1); i <= n; i++) {
    int x;
    cin >> x;
    if (x != 0)
      g[x].push_back(i);
    else
      king = i;
  }
  curT = 1;
  cin >> m;
  nm = 1;
  while (nm <= m + 3) nm *= 2;
  t[king] = build(t[king], 1, nm);
  for (int i(1); i <= m; i++) {
    int c;
    cin >> c;
    if (c == 1) {
      int x;
      cin >> x;
      tm1[x] = i;
      inp[i].st = -1;
    } else
      cin >> inp[i].st >> inp[i].fn >> inp[i].k >> inp[i].y;
  }
  dfs(king, -1);
  for (int i(1); i <= m; i++) {
    if (inp[i].st == -1) continue;
    int a = inp[i].st, b = inp[i].fn;
    int k = inp[i].k, tl = inp[i].y;
    int lc = lca(a, b);
    int x = cntGood(a, tl + 1, i - 1);
    int y = cntGood(b, tl + 1, i - 1);
    int z = cntGood(lc, tl + 1, i - 1);
    x = x - z;
    y = y - z;
    if (tm1[lc] <= tl || tm1[lc] >= i) x++;
    if (tm1[a] <= tl || tm1[a] >= i) k++;
    if (x + y < k) {
      cout << "-1\n";
      continue;
    }
    if (x + y == k && (tm1[b] <= tl || tm1[b] >= i)) {
      cout << "-1\n";
      continue;
    }
    if (k <= x)
      cout << give(a, k, tl + 1, i - 1) << "\n";
    else {
      k = k - x;
      k = y - k + 1;
      cout << give(b, k, tl + 1, i - 1) << "\n";
    }
  }
}
