Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.

When Chef was visiting a fair in Byteland, he met a magician. The magician had $N$ boxes (numbered $1$ through $N$) and a gold coin. He challenged Chef to play a game with him; if Chef won the game, he could have the coin, but if he lost, the magician would kidnap Chef.

At the beginning of the game, the magician places the gold coin into the $X$-th box. Then, he performs $S$ swaps. To win, Chef needs to correctly identify the position of the coin after all swaps.

In each swap, the magician chooses two boxes $A$ and $B$, moves the contents of box $A$ (before the swap) to box $B$ and the contents of box $B$ (before the swap) to box $A$.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains three space-separated integers $N$, $X$ and $S$.
$S$ lines follow. Each of these lines contains two space-separated integers $A$ and $B$ denoting a pair of swapped boxes.

------  Output ------
For each test case, print a single line containing one integer — the number of the box containing the gold coin after all swaps are performed.

------  Constraints  ------
$1 ≤ T ≤ 100$
$2 ≤ N ≤ 10^{5}$
$1 ≤ S ≤ 10^{4}$
$1 ≤ X, A, B ≤ N$
$A \neq B$
the sum of $S$ for all test cases does not exceed $2*10^{5}$

------  Subtasks ------
Subtask #1 (100 points): original constraints

----- Sample Input 1 ------ 
1

5 2 4

4 2

3 4

3 2

1 2
----- Sample Output 1 ------ 
1
----- explanation 1 ------ 
Example case 1:
- after the first swap, the coin is in box $4$
- after the second swap, the coin is in box $3$
- after the third swap, the coin is in box $2$
- after the fourth swap, the coin is in box $1$

Source: codechef | Difficulty: EASY
Tags: Implementation

Complete search


Solutions:
for i in range(int(input())):
	(N, X, S) = map(int, input().split(' '))
	for j in range(1, S + 1):
		(A, B) = map(int, input().split(' '))
		if X == A:
			X = B
		elif X == B:
			X = A
	print(X)


t = int(input())
for x in range(t):
	(n, x, s) = map(int, input().split())
	for i in range(s):
		(a, b) = map(int, input().split())
		if x == a:
			x = b
		elif x == b:
			x = a
	print(x)


for _ in range(int(input())):
	(x, y, c) = map(int, input().split())
	ans = y
	for i in range(c):
		(a, b) = map(int, input().split())
		if ans == a or ans == b:
			if ans == a:
				ans = b
			else:
				ans = a
	print(ans)


T = int(input())
for i in range(0, T):
	temp = 0
	flag = 0
	(N, X, S) = map(int, input().split())
	for i in range(0, S):
		(A, B) = map(int, input().split())
		if X == A:
			X = B
		elif X == B:
			X = A
	print(X)


for _ in range(int(input())):
	(n, x, s) = map(int, input().split())
	for i in range(1, s + 1):
		(a, b) = map(int, input().split())
		if x == a:
			x = b
		elif x == b:
			x = a
	print(x)


for _ in range(int(input())):
	(N, X, S) = map(int, input().split())
	temp = X
	for i in range(S):
		(A, B) = map(int, input().split())
		if A == temp:
			(A, B) = (B, A)
			temp = A
		elif B == temp:
			(A, B) = (B, A)
			temp = B
	print(temp)


for _ in range(int(input())):
	(n, x, s) = map(int, input().split())
	curr_box = x
	for _ in range(s):
		(a, b) = map(int, input().split())
		if curr_box == a:
			curr_box = b
		elif curr_box == b:
			curr_box = a
	print(curr_box)


t = int(input())
for _ in range(t):
	(n, x, s) = map(int, input().split())
	l = [0] * n
	l[x - 1] = 'G'
	for i in range(s):
		(a, b) = map(int, input().split())
		(l[a - 1], l[b - 1]) = (l[b - 1], l[a - 1])
	print(l.index('G') + 1)


t = int(input())
while t > 0:
	(n, x, s) = map(int, input().split())
	currCoinBox = x
	for i in range(s):
		(a, b) = map(int, input().split())
		if a == currCoinBox:
			currCoinBox = b
		elif b == currCoinBox:
			currCoinBox = a
	print(currCoinBox)
	t -= 1


for _ in range(int(input())):
	(boxes, coin, times) = map(int, input().split())
	for __ in range(times):
		(x1, y1) = map(int, input().split())
		if coin == x1:
			coin = y1
		elif coin == y1:
			coin = x1
	print(coin)


t = int(input())
for i in range(t):
	(n, x, s) = map(int, input().split())
	for i in range(s):
		(A, B) = map(int, input().split())
		if x == A:
			x = B
		elif x == B:
			x = A
	print(x)


for _ in range(int(input())):
	(NUMBER_OF_BOX, PLACED_OF_BOX, NUMBER_OF_SWAP) = list(map(int, input().split()))
	M = 0
	for _ in range(NUMBER_OF_SWAP):
		(SWAPA, SWAPB) = list(map(int, input().split()))
		if PLACED_OF_BOX == SWAPA:
			PLACED_OF_BOX = SWAPB
		elif PLACED_OF_BOX == SWAPB:
			PLACED_OF_BOX = SWAPA
	print(PLACED_OF_BOX)


for T in range(int(input())):
	(n, x, s) = map(int, input().split())
	for _ in range(s):
		(a, b) = map(int, input().split())
		if a == x:
			x = b
		elif b == x:
			x = a
		else:
			pass
	print(x)


for i in range(int(input())):
	(n, x, s) = map(int, input().split())
	list1 = [0 for j in range(n)]
	list1[x - 1] = 1
	for j in range(s):
		(a, b) = map(int, input().split())
		(list1[a - 1], list1[b - 1]) = (list1[b - 1], list1[a - 1])
	print(list1.index(1) + 1)


for i in range(int(input())):
	(T, N, S) = map(int, input().split())
	for i in range(1, S + 1):
		(A, B) = map(int, input().split(' '))
		if N == A:
			N = B
		elif N == B:
			N = A
	print(N)


for _ in range(int(input())):
	(no_box, coin_box, no_of_swap) = map(int, input().split())
	for j in range(no_of_swap):
		(box_no1, box_no2) = map(int, input().split())
		if box_no1 == coin_box:
			coin_box = box_no2
		elif box_no2 == coin_box:
			coin_box = box_no1
	print(coin_box)


t = int(input())
for _ in range(t):
	(n, x, s) = map(int, input().split())
	for j in range(s):
		(a, b) = map(int, input().split())
		if x == a:
			x = b
		elif x == b:
			x = a
	print(x)


for i in range(int(input())):
	(n, x, s) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(0)
	l[x - 1] = 1
	for i in range(s):
		(a, b) = map(int, input().split())
		(l[a - 1], l[b - 1]) = (l[b - 1], l[a - 1])
	f = l.index(1)
	print(f + 1)


t = int(input())
for i in range(t):
	(n, x, s) = map(int, input().split())
	a = x
	for i in range(s):
		l = list(map(int, input().split()))
		if a in l:
			p = l.index(a)
			a = l[1 - p]
		else:
			pass
	print(a)


for i in range(int(input())):
	(n, x, s) = map(int, input().split())
	g = x
	for j in range(s):
		(a, b) = map(int, input().split())
		if g == a:
			g = b
		elif g == b:
			g = a
	print(g)


t = int(input())
for i in range(t):
	[n, x, s] = list(map(int, input().split()))
	l = []
	for j in range(s):
		l.append(list(map(int, input().split())))
	ip = x
	for [a, b] in l:
		if a == ip:
			ip = b
		elif b == ip:
			ip = a
	print(ip)


t = int(input())
for i in range(t):
	(N, X, S) = map(int, input().split(' '))
	l = []
	for j in range(1, N + 1):
		l.append(j)
	for j in range(1, S + 1):
		(A, B) = map(int, input().split(' '))
		(l[A - 1], l[B - 1]) = (l[B - 1], l[A - 1])
	print(l.index(X) + 1)


for i in range(int(input())):
	(N, X, S) = map(int, input().split())
	Goal = 0
	for i in range(S):
		(A, B) = map(int, input().split())
		if A == X:
			X = B
		elif X == B:
			X = A
	print(X)


for i in range(int(input())):
	(n, x, s) = map(int, input().split())
	for i in range(s):
		(p, q) = map(int, input().split())
		if x == q:
			x = p
		elif x == p:
			x = q
	print(x)


for _ in range(int(input())):
	(x, y, z) = map(int, input().split())
	for i in range(z):
		(a, b) = map(int, input().split())
		if y == a:
			y = b
		elif y == b:
			y = a
	print(y)


for i in range(int(input())):
	(x, y, z) = map(int, input().split())
	for i in range(z):
		(n, k) = map(int, input().split())
		if y == n:
			y = k
		elif y == k:
			y = n
	print(y)


t = int(input())
for _ in range(t):
	(a, b, c) = map(int, input().split())
	for i in range(1, c + 1):
		(m, n) = map(int, input().split())
		if b == m:
			b = n
		elif b == n:
			b = m
	print(b)


q = int(input())
for i in range(q):
	(n, x, s) = map(int, input().split())
	for j in range(s):
		(a, b) = map(int, input().split())
		if x == a or x == b:
			if x == a:
				x = b
			else:
				x = a
	print(x)


t = int(input())
for i in range(t):
	boxes = input().split(' ')
	gold_box = {'gold': boxes[1]}
	for j in range(int(boxes[2])):
		swap = input().split(' ')
		if swap[1] == gold_box.get('gold'):
			gold_box.update({'gold': swap[0]})
		elif swap[0] == gold_box.get('gold'):
			gold_box.update({'gold': swap[1]})
	print(int(gold_box.get('gold')))


for i in range(int(input())):
	(N, X, S) = map(int, input().split())
	A = []
	B = []
	for j in range(S):
		(a, b) = map(int, input().split())
		A.append(a)
		B.append(b)
	for j in range(S):
		if A[j] == X:
			X = B[j]
		elif B[j] == X:
			X = A[j]
	print(X)


t = int(input())
for tc in range(t):
	(n, x, s) = map(int, input().split())
	for j in range(s):
		(a, b) = map(int, input().split())
		if x == a or x == b:
			if x == a:
				x = b
			else:
				x = a
	print(x)


t = int(input())
for _ in range(t):
	(n, X, s) = map(int, input().split())
	for i in range(1, s + 1):
		(A, B) = map(int, input().split(' '))
		if X == A:
			X = B
		elif X == B:
			X = A
	print(X)