showing wrong annswer dispite of getting write answer

Problem:

code:
from sys import stdin
x = 0
t = int(input())
for i in range(t):
n, x, s = map(int, stdin.readline().split())
for j in range(int(s)):
a, b = map(int, stdin.readline().split())
if a == x:
x = b
elif b == x:
x = a
print(x)

The answers for each testcases have to be printed in separate lines.

//