import sys, os c = 0; W = []; K = W.append for i in os.read(0, 67_067_067): if i > 45: c = c*10+i-48 elif c: K(c); c = 0 L = []; R = []; C = [] def node(): i = len(L); L.append(-1); R.append(-1); C.append(0); return i def pull(x): if ~x: C[x] = (C[L[x]] if ~L[x] else 0)+(C[R[x]] if ~R[x] else 0) N, Q = W[0], W[1]; T = []; F = []; P = []; Z = []; S = []; tmp = [] for i in range(1, N+1): root = node(); x = root; l = 1; r = N while l < r: m = (l+r)//2; y = node(); P.append(x) if i <= m: L[x] = y; x = y; r = m else: R[x] = y; x = y; l = m+1 C[x] = 1 while P: pull(P.pop()) T.append(root) for p in range(2, 3*Q+2, 3): q, i, j = W[p], W[p+1]-1, W[p+2] if q < 2: a, k, l, r = T[i], j, 1, N while l < r: m = (l+r)>>1; z = C[L[a]] if ~L[a] else 0 if k <= z: a = L[a]; r = m else: k -= z; a = R[a]; l = m+1 Z.append(l) elif q < 3: j -= 1; S.append((T[i], T[j], 1, N, 0)) while S: a, b, l, r, x = S.pop() if x: R[a] = tmp.pop(); L[a] = tmp.pop(); pull(a); tmp.append(a) else: if a < 0: tmp.append(b); continue if b < 0 or l == r: tmp.append(a); continue m = (l+r)>>1; S.extend(((a, b, l, r, 1), (R[a], R[b], m+1, r, 0), (L[a], L[b], l, m, 0))) T[i], T[j] = tmp.pop(), -1; F.append(j) else: a, k, l, r = T[i], j, 1, N while a >= 0 < k < C[a]: m = (l+r)>>1; z = C[L[a]] if L[a] >= 0 else 0; b = node() if k <= z: S.append((a, b, 1)); a = L[a]; r = m else: S.append((a, b, 0)); a = R[a]; k -= z; l = m+1 if a < 0: x = y = -1 elif k <= 0: x, y = -1, a else: x, y = a, -1 while S: a, b, c = S.pop() if c: L[a] = y; pull(a); L[b] = x; pull(b); x, y = b, a else: R[a] = x; pull(a); R[b] = y; pull(b); x, y = a, b T[i] = x; T[F.pop()] = y sys.stdout.write('\n'.join(map(str, Z)))