#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
  return y <= x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x <= y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int MAXN = 400005;
const int MAXM = 4005;
const int MAXP = 7000000;
const int MX = 20;
const int G = 3;
const int mods = 1e9 + 7;
const int Gi = (mods + 1) / G;
const int SZ = 10;
const int inv2 = (mods + 1) >> 1;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       (c == ' ' || c == '\0' || c == '\n') && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       c != ' ' && c != '\0' && c != '\n';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
inline void putstr(string st) {
  for (int i = 0; i < (int)st.size(); ++i) putc(st[i]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::getc;
using FastIO ::print;
using FastIO ::putc;
using FastIO ::putstr;
using FastIO ::read;
using FastIO ::reads;
pair<int, int> s[MAXN << 2];
vector<int> e[MAXN];
int a[MAXN], ans, n;
int dep[MAXN], fa[MAXN], sz[MAXN], mx[MAXN], top[MAXN], id[MAXN];
void dfs1(int x, int father) {
  dep[x] = dep[father] + 1, fa[x] = father, sz[x] = 1;
  for (auto v : e[x]) {
    if (v == father) continue;
    dfs1(v, x);
    sz[x] += sz[v];
    if (sz[v] > sz[mx[x]]) mx[x] = v;
  }
}
void dfs2(int x, int Top) {
  top[x] = Top;
  if (mx[x]) dfs2(mx[x], Top);
  for (auto v : e[x]) {
    if (v == fa[x] || v == mx[x]) continue;
    dfs2(v, v);
  }
}
int getdis(int x, int y) {
  int ans = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    ans += dep[x] - dep[top[x]] + 1, x = fa[top[x]];
  }
  return ans + abs(dep[x] - dep[y]);
}
pair<int, int> operator+(const pair<int, int> &a, const int b) {
  if (a.first == -1 || b == -1) return make_pair(-1, -1);
  int X = getdis(a.first, b);
  int Y = getdis(a.second, b);
  int Z = getdis(a.first, a.second);
  if (X + Y == Z) return make_pair(a.first, a.second);
  if (X + Z == Y) return make_pair(a.second, b);
  if (Y + Z == X) return make_pair(a.first, b);
  return make_pair(-1, -1);
}
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
  pair<int, int> p = a + b.first;
  return (p.first == -1) ? make_pair(-1, -1) : p + b.second;
}
void build(int x, int l, int r) {
  if (l == r) {
    s[x] = make_pair(id[l], id[l]);
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  s[x] = s[x << 1] + s[x << 1 | 1];
}
void update(int x, int l, int r, int y) {
  if (l == r) {
    s[x] = make_pair(id[l], id[l]);
    return;
  }
  int mid = (l + r) >> 1;
  if (y <= mid)
    update(x << 1, l, mid, y);
  else
    update(x << 1 | 1, mid + 1, r, y);
  s[x] = s[x << 1] + s[x << 1 | 1];
}
void query(int x, int l, int r, pair<int, int> y) {
  if (l == r) {
    if ((y + s[x]).first != -1) upmax(ans, l);
    return;
  }
  int mid = (l + r) >> 1;
  pair<int, int> t = y + s[x << 1];
  if (t.first != -1)
    upmax(ans, mid), query(x << 1 | 1, mid + 1, r, t);
  else
    query(x << 1, l, mid, y);
}
signed main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]), ++a[i], id[a[i]] = i;
  for (int i = 2, x; i <= n; ++i) read(x), e[x].push_back(i);
  dfs1(1, 0);
  dfs2(1, 1);
  build(1, 1, n);
  int Case;
  read(Case);
  while (Case--) {
    int opt, x, y;
    read(opt);
    if (opt == 1) {
      read(x), read(y);
      swap(id[a[x]], id[a[y]]);
      swap(a[x], a[y]);
      update(1, 1, n, a[x]);
      update(1, 1, n, a[y]);
    } else
      ans = 0, query(1, 1, n, make_pair(id[1], id[1])), print(ans), putc('\n');
  }
  return 0;
}
