import sys, os; from array import * c = 0; W = array('I'); A = W.append for i in os.read(0, 67_067_067): if i > 45: c = c*10+i-48 elif c: A(c); c = 0 N, M, Q = W[0], W[1], W[2] G = [[] for _ in range(N)] D = [N]*N for i in range(N): if i != (r:=W[3+i]-1): G[r].append(i) else: root = i; D[i] = 0 tin = [0]*N tout = [0]*N L = (N-1).bit_length()+1 jmp = [[-1]*N for _ in range(L)] stk = [(root, root, T:=0)] while stk: u, p, b = stk.pop() if b: tout[u] = (T:=T+1) else: tin[u] = (T:=T+1); stk.append((u, p, 1)); jmp[0][u] = p for i in range(1, L): jmp[i][u] = jmp[i-1][jmp[i-1][u]] for v in G[u]: if v != p: stk.append((v, u, 0)); D[v] = D[u]+1 H = [W[N+3+i]-1 for i in range(M)] pos = N+M+2 I = [-1]*N for i in range(M): I[H[i]] = i def combine(x, y): a, d = x; b, e = y if a < 0: return y if b < 0: return x p = d+D[a]; q = e+D[b] if tin[a] <= tin[b] < tout[b] <= tout[a]: lca = a elif tin[b] <= tin[a] < tout[a] <= tout[b]: lca = b else: for i in range(L-1, -1, -1): j = jmp[i][a] if tin[j] > tin[b] or tout[b] > tout[j]: a = j lca = jmp[0][a] return lca, (p if p>q else q)-D[lca] P = 1<<(2*M-1).bit_length() F = [(-1, 0)]*2*P; B = [(-1, 0)]*2*P for i in range(M): j = -i%M; F[P+i] = F[P+i+M] = B[P+j] = B[P+j+M] = (H[i], 0) for i in range(P-1, 0, -1): F[i] = combine(F[i<<1], F[i<<1|1]); B[i] = combine(B[i<<1], B[i<<1|1]) def update(S, i, v): i += P; S[i] = (v, 0) while i > 1: i >>= 1; S[i] = combine(S[i<<1], S[i<<1|1]) def get(S, p, r): l = p; p += P; x = (-1, 0) while 1: while p&1<1: p >>= 1 y = combine(x, S[p]) if y[1] > r: while p < P: p <<= 1; y = combine(x, S[p]) if y[1] <= r: x = y; p += 1 return min(p-P, 2*M)-l x = y; p += 1 if p&-p == p: break return M Z = array('I') for _ in range(Q): T = W[pos:=pos+1] if T&1: a = W[pos:=pos+1]-1; b = W[pos:=pos+1]-1; I[a], I[b] = I[b], I[a] if ~I[a]: H[x:=I[a]] = a; update(F, x, a); update(F, x+M, a); update(B, -x%M, a); update(B, -x%M+M, a) if ~I[b]: H[x:=I[b]] = b; update(F, x, b); update(F, x+M, b); update(B, -x%M, b); update(B, -x%M+M, b) else: x = get(F, p:=W[pos:=pos+1]-1, r:=W[pos:=pos+1]); y = get(B, -p%M, r); Z.append(x if x>y else y) sys.stdout.write('\n'.join(map(str, Z)))