Sakuzyo - Imprinting

A.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them.

It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.

ConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.

CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.

Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then the descriptions of $t$ test cases follow.

The first line of a test case contains three integers $n$, $s$ and $k$ ($2 \le n \le 10^9$, $1 \le s \le n$, $1 \le k \le \min(n-1, 1000)$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.

The second line of a test case contains $k$ distinct integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of $k$ over all test cases does not exceed $1000$.


-----Output-----

For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant.


-----Example-----
Input
5
5 2 3
1 2 3
4 3 3
4 1 2
10 2 6
1 2 3 4 5 7
2 1 1
2
100 76 8
76 75 36 67 41 74 10 77

Output
2
0
4
0
2



-----Note-----

In the first example test case, the nearest floor with an open restaurant would be the floor $4$.

In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.

In the third example test case, the closest open restaurant is on the $6$-th floor.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Implementation

Complete search


Solutions:
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	l = max(1, s - 1001)
	r = min(n, s + 1001)
	d = 0
	while True:
		if s - d not in a and s - d >= l:
			print(d)
			break
		elif s + d not in a and s + d <= n:
			print(d)
			break
		else:
			d += 1


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	z = 0
	A = list(map(int, input().split()))
	while (s - z in A or s - z < 1) and (s + z in A or s + z > n):
		z += 1
	print(z)


T = int(input())
for _ in range(T):
	(n, s, k) = map(int, input().split())
	sl = [int(x) for x in input().split()]
	if not s in sl:
		print(0)
		continue
	if 1 not in sl:
		sl.append(min(sl) - 10)
	sl.append(max(sl) + 10)
	sl.sort()
	i = sl.index(s)
	l = s
	rl = []
	for x in sl[i + 1:]:
		if l >= n:
			break
		if x - l != 1:
			rl.append(abs(l + 1 - s))
			break
		l = x
	l = s
	for x in sl[:i][::-1]:
		if l <= 1:
			break
		if l - x != 1:
			rl.append(abs(l - 1 - s))
			break
		l = x
	print(min(rl))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print('0')
		continue
	else:
		i = 0
		while True:
			if s - i not in a and s - i > 0:
				break
			if s + i not in a and s + i <= n:
				break
			i += 1
	print(i)


def lc():
	return [int(i) for i in input().split()]
t = int(input())
while t:
	(n, s, k) = lc()
	fr = lc()
	var1 = s - 1
	var2 = s + 1
	if s not in fr:
		print(0)
	else:
		while True:
			if var1 <= 0:
				var1 = 1
			if var2 >= n:
				var2 = n
			if var1 not in fr or var2 not in fr:
				print(max(s - var1, var2 - s))
				break
			else:
				var1 -= 1
				var2 += 1
	t -= 1


t = int(input())
for _ in range(t):
	y = input().split()
	n = int(y[0])
	s = int(y[1])
	k = int(y[2])
	h = input().split()
	a = []
	for i in range(k):
		a.append(int(h[i]))
	s1 = s
	s2 = s
	x = 0
	while True:
		if s1 not in a and s1 <= n or (s2 not in a and s2 >= 1):
			print(x)
			break
		else:
			s1 += 1
			s2 -= 1
			x += 1


from math import *
from bisect import *
t = int(input())
for i in range(t):
	(n, s, k) = input().split()
	n = int(n)
	s = int(s)
	k = int(k)
	arr = list(map(int, input().split()))
	arr.sort()
	if s not in arr:
		print(0)
	else:
		flag = False
		flag1 = False
		ind = arr.index(s)
		for j in range(ind + 1, len(arr)):
			flag1 = True
			if arr[j] != s + (j - ind):
				flag = True
				break
		if flag == True:
			ind_u = arr[j - 1] + 1
		elif flag1 == True and arr[j] < n:
			ind_u = arr[j] + 1
		elif flag1 == False and arr[ind] + 1 <= n:
			ind_u = arr[ind] + 1
		else:
			ind_u = n + 10 ** 5
		flag = False
		flag1 = False
		for j in range(ind - 1, -1, -1):
			flag1 = True
			if arr[j] != s + (j - ind):
				flag = True
				break
		if flag == True:
			ind_l = arr[j + 1] - 1
		elif flag1 == True and arr[j] > 1:
			ind_l = arr[j] - 1
		elif flag1 == False and arr[ind] - 1 >= 1:
			ind_l = arr[ind] - 1
		else:
			ind_l = -n - 10 ** 5
		print(min(ind_u - s, s - ind_l))


import sys
n = int(sys.stdin.readline())
tests = []
for i in range(n):
	l = sys.stdin.readline()
	(n, s, k) = (int(v) for v in l.split())
	l = sys.stdin.readline()
	cl = set((int(v) for v in l.split()))
	tests.append((n, s, cl))

def findnext(n, s, cl):
	(fu, fd) = (s, s)
	while fu <= n or fd >= 1:
		if fu <= n and fu not in cl:
			return fu
		if fd >= 1 and fd not in cl:
			return fd
		fu += 1
		fd -= 1
for (n, s, cl) in tests:
	fr = findnext(n, s, cl)
	print(abs(fr - s))


for _ in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	fl = {int(x) for x in input().split()}
	i = 0
	while 1:
		if max(s - i, 1) not in fl or min(n, s + i) not in fl:
			print(i)
			break
		i += 1


for ii in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print(0)
	else:
		c = 0
		zz = 0
		for i in range(s - 1, 0, -1):
			c += 1
			if i not in a:
				zz += 1
				break
		if zz == 0:
			c = 0
		cc = 0
		zzz = 0
		for i in range(s + 1, n + 1):
			cc += 1
			if i not in a:
				zzz += 1
				break
		if zzz == 0:
			cc = 0
		if min(c, cc) == 0:
			print(max(c, cc))
		else:
			print(min(c, cc))


def f(n, s, a):
	t = 1
	l = s - 1
	r = s + 1
	while l >= 1 or r <= n:
		if l >= 1:
			if l not in a:
				return t
			else:
				l -= 1
		if r <= n:
			if r not in a:
				return t
			else:
				r += 1
		t += 1
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print('0')
	else:
		print(f(n, s, a))


import math

def solve_case():
	(n, s, k) = [int(x) for x in input().split()]
	cr = set([int(x) for x in input().split()])
	result = math.inf
	for i in range(max(1, s - 1000 - 1), min(s + 1000 + 1, n + 1)):
		if i not in cr:
			result = min(result, abs(i - s))
	return result

def main():
	n = int(input())
	for _ in range(n):
		print(solve_case())
main()


T = int(input())
for z in range(T):
	(n, s, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	mn = 999999999999999999999
	for i in range(s, n + 1):
		if i not in lst:
			mn = i - s
			break
	for i in range(s, 0, -1):
		if i not in lst:
			mn = min(mn, abs(i - s))
			break
	print(mn)


t = int(input())
while t:
	(n, s, k) = list(map(int, input().split()))
	k1 = list(map(int, input().split()))
	res = []
	for i in range(s, n + 1):
		if i not in k1:
			res.append(abs(s - i))
			break
	for i in range(s, 0, -1):
		if i not in k1:
			res.append(abs(s - i))
			break
	print(min(res))
	t -= 1


t = int(input())
for i in range(t):
	(n, s, k) = [int(p) for p in input().split()]
	closed = set([int(p) for p in input().split()])
	for i in range(n):
		if s + i not in closed and s + i <= n:
			print(i)
			break
		elif s - i not in closed and s - i > 0:
			print(i)
			break


sp = lambda : list(map(int, input().split()))
si = lambda : int(input())
TESTCASES = int(input())
for tc in range(TESTCASES):
	(n, s, k) = sp()
	floors = set(sp())
	stairs = 0
	while True:
		if s + stairs not in floors and s + stairs <= n:
			break
		elif s - stairs not in floors and s - stairs > 0:
			break
		stairs += 1
	print(stairs)


n = int(input())
for i in range(n):
	(n, s, k) = map(int, input().split())
	r = set(map(int, input().split()))
	for j in range(0, n):
		if s + j <= n:
			if s + j not in r:
				print(j)
				break
		if s - j > 0:
			if s - j not in r:
				print(j)
				break


numTestcases = int(input())
ans = []
for i in range(numTestcases):
	cLine = list(map(int, input().split(' ')))
	closedR = list(map(int, input().split(' ')))
	n = cLine[0]
	s = cLine[1]
	k = cLine[2]
	for c in range(n):
		if s + c not in closedR and s + c <= n:
			ans.append(str(c))
			break
		elif s - c not in closedR and s - c > 0:
			ans.append(str(c))
			break
print('\n'.join(ans))


for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	q = set(map(int, input().split()))
	l = s
	g = s
	while g in q and l in q:
		l = max(l - 1, 1)
		g = min(g + 1, n)
	if l in q:
		print(g - s)
	else:
		print(s - l)


t = int(input().strip())
for _ in range(t):
	(n, s, k) = map(int, input().strip().split())
	closed = set([int(i) for i in input().strip().split()])
	(ans1, ans2, ctr) = (1010, 1010, 0)
	for i in range(s, min(n, s + k) + 1):
		if i not in closed:
			ans1 = ctr
			break
		ctr += 1
	ctr = 0
	for i in range(s, max(0, s - k - 1), -1):
		if i not in closed:
			ans2 = ctr
			break
		ctr += 1
	print(min(ans1, ans2))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	if s not in a:
		print(0)
		continue
	if s < n:
		r = s + 1
	else:
		r = s
	if s > 1:
		l = s - 1
	else:
		l = s
	for j in range(1, n):
		if r not in a:
			print(abs(s - r))
			break
		if l not in a:
			print(abs(s - l))
			break
		if r < n:
			r += 1
		if l > 1:
			l -= 1


from sys import stdin as st
for _ in range(int(st.readline())):
	(n, s, k) = map(int, st.readline().split())
	a = set(map(int, st.readline().split()))
	i = 0
	while max(s - i, 1) in a and min(s + i, n) in a:
		i += 1
	print(i)


for i in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	for i in range(k + 1):
		if s - i >= 1 and s - i not in arr:
			print(i)
			break
		if s + i <= n and s + i not in arr:
			print(i)
			break


from sys import stdout

def bsearch(arr, target):
	low = 0
	high = len(arr) - 1
	if target > arr[-1] or target < arr[0]:
		return False
	while low <= high:
		mid = low + (high - low) // 2
		if arr[mid] > target:
			high = mid - 1
		elif arr[mid] < target:
			low = mid + 1
		else:
			return True
for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a.sort()
	found = False
	up = s
	down = s
	if not bsearch(a, s):
		print(0)
		continue
	_min = float('inf')
	while True:
		if up < n:
			up += 1
		if down > 1:
			down -= 1
		if not bsearch(a, up):
			print(up - s)
			break
		if not bsearch(a, down):
			print(s - down)
			break


for _ in range(int(input())):
	(n, s, k) = (int(x) for x in input().split())
	l = [int(x) for x in input().split()]
	if s not in l:
		print(0)
	else:
		for i in range(1, k + 1):
			if s - i >= 1 and (not s - i in l):
				print(i)
				break
			if s + i <= n and (not s + i in l):
				print(i)
				break


f = int(input())
for i in range(f):
	(n, s, k) = map(int, input().split())
	v = list(map(int, input().split()))
	i = s
	d = 0
	i2 = s
	d2 = 0
	while (i in v) == True:
		i = i + 1
		d = d + 1
		if i == n + 1:
			d = n
	while (i2 in v) == True:
		i2 = i2 - 1
		d2 = d2 + 1
		if i2 == 0:
			d2 = n
	if d > d2:
		print(d2)
	else:
		print(d)


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	l = 0
	w = 1
	d = s
	while w:
		if s not in a and s <= n:
			print(l)
			w = 0
		if d not in a and w != 0 and (d >= 1):
			print(l)
			w = 0
		l += 1
		s += 1
		d -= 1


t = int(input())
for i in range(0, t):
	n = list(map(int, input().split()))
	m = list(map(int, input().split()))
	if n[1] not in m:
		print('0')
	elif len(m) == n[0]:
		print('0')
	else:
		x = n[1]
		y = n[1]
		i = 1
		while True:
			x = x - 1
			y = y + 1
			if x not in m and x > 0:
				print(i)
				break
			elif y not in m and y < n[0] + 1:
				print(i)
				break
			i = i + 1


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	temp = list(map(int, input().split()))
	d = dict()
	for i in temp:
		d[i] = 1
	flag = 0
	tot = s
	dis1 = 10000000000
	dis2 = 10000000000
	while flag == 0 and tot <= n:
		if d.get(tot, 0) == 1:
			pass
		else:
			dis1 = tot - s
			flag = 1
		tot += 1
	flag = 0
	tot = s
	while flag == 0 and tot >= 1:
		if d.get(tot, 0) == 1:
			pass
		else:
			dis2 = abs(s - tot)
			flag = 1
		tot -= 1
	print(min(dis1, dis2))


t = int(input())
while t > 0:
	t -= 1
	(n, s, k) = map(int, input().split(' '))
	closed = set(map(int, input().split(' ')))
	i = 0
	while True:
		if s - i not in closed and s - i >= 1:
			print(i)
			break
		elif s + i not in closed and s + i <= n:
			print(i)
			break
		else:
			i += 1


n = int(input())
for _ in range(n):
	(k, s, f) = map(int, input().split())
	l = list(map(int, input().split()))
	i = 0
	while True:
		if s + i not in l and s + i <= k or (s - i not in l and s - i >= 1):
			print(i)
			break
		i += 1


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	(n, s, k) = M()
	a = L()
	i = 0
	while True:
		if s + i <= n and s + i not in a:
			print(i)
			break
		elif s - i >= 1 and s - i not in a:
			print(i)
			break
		i += 1


for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for f in range(0, k + 1):
		if s - f >= 1 and (not s - f in l):
			print(f)
			break
		if s + f <= n and s + f not in l:
			print(f)
			break


def f(x):
	ans = 0
	if x[-1] == len(x) - 1:
		ans = x[-1] + 1
	else:
		for i in range(len(x) - 1):
			if x[i + 1] - x[i] > 1:
				ans = x[i] + 1
				break
	return ans
for _ in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	test = [int(x) - s for x in input().split()]
	test = sorted(test)
	mint = 1 - s
	maxt = n - s
	if 0 not in test:
		print(0)
	else:
		ind = test.index(0)
		headt = test[:ind + 1]
		head = [-i for i in headt]
		head = sorted(head)
		tail = test[ind:]
		t = f(tail)
		h = f(head)
		if t > maxt:
			t = h
		elif -h < mint:
			h = t
		print(min(h, t))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = s
	m = 999999
	if s in a:
		count = 1
		l = 0
		while t > 1:
			if t - 1 in a:
				count += 1
				t = t - 1
			else:
				l = 1
				break
		if l == 1:
			m = count
		t = s
		count = 1
		l = 0
		while t < n:
			if t + 1 in a:
				count += 1
				t = t + 1
			else:
				l = 1
				break
		if l == 1:
			if count < m:
				m = count
		print(m)
	else:
		print('0')


t = int(input())
while t:
	t += -1
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	temp = s
	ind = -1
	for i in range(k):
		if l[i] == temp:
			ind = i
			break
	if ind == -1:
		print(0)
	else:
		ans = 9999999999
		temp -= -1
		ch = 0
		for i in range(ind + 1, k):
			if l[i] != temp:
				ch = 1
				break
			temp -= -1
		if ch == 1:
			ans = temp - s
		elif l[k - 1] != n:
			ans = temp - s
		ch = 0
		temp = s
		temp += -1
		for i in range(ind - 1, -1, -1):
			if l[i] != temp:
				ch = 1
				break
			temp += -1
		if ch == 1:
			ans = min(ans, s - temp)
		elif l[0] != 1:
			ans = min(ans, s - temp)
		print(ans)


T = int(input())
while T:
	(n, s, k) = map(int, input().split())
	List = list(map(int, input().split()))
	D = {}
	for i in List:
		D[i] = 1
	if s not in D:
		print(0)
	else:
		ans = 0
		for i in range(1, n):
			if s - i > 0:
				if s - i not in D:
					ans = i
					break
			if s + i <= n:
				if s + i not in D:
					ans = i
					break
		print(ans)
	T -= 1


t = int(input())
for _ in range(t):
	L = list(map(int, input().split()))
	n = L[0]
	s = L[1]
	k = L[2]
	bad = list(map(int, input().split()))
	tr = s
	while tr in bad:
		tr += 1
		if tr > n:
			tr = 10000000000
			break
	top_min = tr - s
	tr = s
	while tr in bad:
		tr -= 1
		if tr < 1:
			tr = -10000000000
			break
	bot_min = s - tr
	print(min(bot_min, top_min))


t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	(n, s, k) = (l[0], l[1], l[2])
	p = list(map(int, input().split()))
	a = s
	for i in range(0, max(n - s, s) + 1):
		if s - i not in p and s - i > 0:
			key = i
			break
		elif s + i not in p and s + i <= n:
			key = i
			break
	print(key)


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().strip().split()))
	a = list(map(int, input().strip().split()))
	a = set(a)
	b = s
	c = 1
	while 1 > 0:
		c = c + 1
		if b not in a:
			break
		else:
			b = b + 1
		if b == n + 1:
			b = 1000000000000000000000000
			break
		if c == 2000:
			break
	e = s
	d = 1
	while 1 > 0:
		d = d + 1
		if d == 2000:
			break
		if e not in a:
			break
		else:
			e = e - 1
		if e == 0:
			e = -100000000000000000000000
			break
	print(min(s - e, b - s))


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	(n, s, k) = mints()
	a = list(mints())
	a.sort()
	r = int(1000000000.0)
	for i in range(1, len(a)):
		if a[i] - a[i - 1] > 1:
			r = min(r, abs(s - (a[i] - 1)))
			r = min(r, abs(s - (a[i - 1] + 1)))
		if a[i - 1] < s and a[i] > s:
			r = 0
	if len(a) > 0:
		if a[0] > 1:
			r = min(r, abs(s - 1))
			r = min(r, abs(s - (a[0] - 1)))
			if s < a[0]:
				r = 0
		if n - a[-1] >= 1:
			r = min(r, abs(n - s))
			r = min(r, abs(s - (a[-1] + 1)))
			if s > a[-1]:
				r = 0
	print(r)
for i in range(mint()):
	solve()


tests = int(input())
for test in range(tests):
	(n, s, k) = [int(a) for a in input().split()]
	floors = set((int(a) for a in input().split()))
	for i in range(10000):
		if s + i <= n and s + i not in floors:
			print(i)
			break
		if s - i >= 1 and s - i not in floors:
			print(i)
			break


import bisect
for t in range(int(input())):
	(n, s, k) = [int(j) for j in input().split()]
	l = [int(j) for j in input().split()]
	l.sort()
	lo = 0
	hi = n - 1
	ins = bisect.bisect_left(l, s)
	try:
		if l[ins] == s:
			prev = l[ins]
			mx_rt = 10 ** 10
			mx_lt = 10 ** 10
			for i in range(ins + 1, len(l)):
				if l[i] == prev + 1:
					prev = l[i]
					continue
				else:
					mx_rt = i - ins
					break
			else:
				if l[len(l) - 1] + 1 <= n:
					mx_rt = len(l) - ins
			prev = l[ins]
			for i in range(ins - 1, -1, -1):
				if l[i] == prev - 1:
					prev = l[i]
					continue
				else:
					mx_lt = ins - i
					break
			else:
				if l[0] - 1 >= 1:
					mx_lt = ins + 1
			print(min(mx_rt, mx_lt))
		else:
			print(0)
	except:
		print(0)


t = int(input())
for _ in range(t):
	(a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	l1 = []
	(i, j) = (b, b)
	ans = 0
	while 1:
		if i > 0:
			if i not in l:
				ans = i
				break
		if j <= a:
			if j not in l:
				ans = j
				break
		j += 1
		i -= 1
	print(abs(ans - b))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = sorted(map(int, input().split()))
	ans = n if s in a else 0
	for i in range(k):
		if i < 1 or a[i] - 1 != a[i - 1]:
			if a[i] - 1 > 0:
				ans = min(ans, abs(a[i] - 1 - s))
		if i + 1 >= k or a[i] + 1 != a[i + 1]:
			if a[i] + 1 <= n:
				ans = min(ans, abs(a[i] + 1 - s))
	print(ans)


import math
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print(0)
	else:
		for i in range(n):
			if s - (i + 1) > 0 and s - (i + 1) not in a or (s + i + 1 <= n and s + i + 1 not in a):
				print(i + 1)
				break


t = int(input())
for tt in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	i = 0
	while True:
		x1 = s + i
		x2 = s - i
		if x1 not in a and x1 <= n:
			print(i)
			break
		if x2 > 0 and x2 not in a:
			print(i)
			break
		i += 1


test = int(input())
for t in range(test):
	(n, s, k) = [int(x) for x in input().split(' ')]
	ls = [int(x) for x in input().split(' ')]
	i = 1
	if s not in ls:
		print(0)
		continue
	while 1:
		if s + i not in ls and s + i < n + 1:
			print(i)
			break
		if s - i not in ls and s - i > 0:
			print(i)
			break
		i += 1


t = int(input())

def search(ar, t):
	l = 0
	r = len(ar) - 1
	while r - l >= 0:
		mid = (r + l) // 2
		if ar[mid] == t:
			return True
		if t < ar[mid]:
			r = mid - 1
		if t > ar[mid]:
			l = mid + 1
	return False
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	m = 9999999999999999999
	for i in range(s, n + 1):
		if not search(a, i):
			m = min(m, abs(s - i))
			break
	for i in range(s, 0, -1):
		if not search(a, i):
			m = min(m, abs(s - i))
			break
	print(m)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	i = 0
	while max(s - i, 1) in a and min(s + i, n) in a:
		i += 1
	print(i)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split(' '))
	x = list(map(int, input().split(' ')))
	i = 0
	for i in range(k + 1):
		if s + i <= n and s + i not in x:
			break
		if s - i >= 1 and s - i not in x:
			break
	print(i)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	k = list(map(int, input().split()))
	l = dict()
	for i in k:
		l[i] = 1
	for i in range(1005):
		i1 = s + i
		i2 = s - i
		if i2 > 0:
			if i2 not in l:
				print(i)
				break
		if i1 <= n:
			if i1 not in l:
				print(i)
				break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	clo = list(map(int, input().split()))
	step = 0
	while True:
		plu = s + step
		sub = s - step
		if plu <= n and plu not in clo or (sub >= 1 and sub not in clo):
			break
		step += 1
	print(step)


def findFood():
	for _ in range(int(input())):
		(n, s, k) = list(map(int, input().rstrip().split(' ')))
		rest = list(map(int, input().rstrip().split(' ')))
		if s not in rest:
			print(0)
			continue
		else:
			i = 1
			sdown = s - i
			sup = s + i
			while i <= n:
				if sdown not in rest and sdown > 0:
					print(i)
					break
				elif sup not in rest and sup <= n:
					print(i)
					break
				else:
					i += 1
					sdown -= 1
					sup += 1
findFood()


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	for j in range(n):
		(low, high) = (s - j, s + j)
		if low > 0 and low not in closed or (high <= n and high not in closed):
			print(j)
			break


import sys
input = sys.stdin.readline
hell = 1000000007

def meowmeow321():
	(n, s, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	ans = hell
	for i in range(s - 1005, s + 1005):
		if i > 0 and i <= n:
			if i not in a:
				ans = min(ans, abs(s - i))
	print(ans)
t = int(input())
for i in range(t):
	meowmeow321()


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	u = d = s
	while d > 0 or u <= n:
		if d not in a and d > 0:
			ans = d
			break
		elif u not in a and u <= n:
			ans = u
			break
		else:
			d -= 1
			u += 1
	print(abs(ans - s))


t = int(input())
while t:
	t -= 1
	(n, s, k) = map(int, input().split())
	arr = set(list(map(int, input().split())))
	ans = 0
	for i in range(k):
		if max(s - ans, 1) not in arr or min(s + ans, n) not in arr:
			break
		else:
			ans += 1
	print(ans)


def sortt(x):
	return abs(s - x)
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	ss = list(map(int, input().split()))
	ss.sort(key=sortt)
	ch2 = s
	ch1 = s
	for i in range(len(ss)):
		if ch1 == ss[i]:
			ch1 -= 1
		if ch2 == ss[i]:
			ch2 += 1
	if ch2 <= n and ch1 > 0:
		print(min(s - ch1, ch2 - s))
	elif ch2 <= n:
		print(ch2 - s)
	elif ch1 > 0:
		print(s - ch1)


for _ in range(int(input())):
	Min = 1001
	Max = 1001
	flag = False
	(n, s, k) = map(int, input().split())
	sp = list(map(int, input().split()))
	sp.sort()
	if s not in sp:
		print(0)
	else:
		for i in range(s, 0, -1):
			if i not in sp:
				Min = s - i
				break
		for i in range(s, n + 1):
			if i not in sp:
				Max = i - s
				break
		print(min(Min, Max))


t = int(input())
for i in range(t):
	line1 = list(map(int, input().split()))
	n = line1[0]
	s = line1[1]
	sa = s
	sb = s
	k = line1[2]
	line2 = list(map(int, input().split()))
	line2.sort()
	bef = -1
	aft = -1
	if s not in line2:
		print(0)
	else:
		ind = line2.index(s)
		for a in range(ind + 1, k):
			sa = sa + 1
			if line2[a] != sa:
				aft = sa
				break
		for a in range(ind):
			sb = sb - 1
			if line2[ind - a - 1] != sb:
				bef = sb
				break
		if aft != -1 and bef != -1:
			print(min(aft - s, s - bef))
		elif aft == -1 and bef != -1:
			if line2[-1] == n:
				print(s - bef)
			else:
				print(min(line2[-1] + 1 - s, s - bef))
		elif aft != -1 and bef == -1:
			if line2[0] == 1:
				print(aft - s)
			else:
				print(min(aft - s, s - line2[0] - 1))
		elif line2[0] == 1:
			print(line2[-1] + 1 - s)
		elif line2[-1] == n:
			print(s + 1 - line2[0])
		else:
			print(min(s + 1 - line2[0], line2[-1] + 1 - s))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	c = 0
	if s not in l:
		print(0)
	else:
		b = s - 1
		e = s + 1
		while 1:
			if b >= 1:
				if b not in l:
					print(abs(s - b))
					break
			b = b - 1
			if e <= n:
				if e not in l:
					print(abs(s - e))
					break
			e += 1


n = int(input())
for i in range(n):
	ans = 0
	(a, r, c) = list(map(int, input().strip().split()))
	renewing = set(list(map(int, input().strip().split())))
	down = up = r
	if r not in renewing:
		print(ans)
	else:
		while True:
			ans += 1
			if down > 1:
				down -= 1
			if up < a:
				up += 1
			if down not in renewing or up not in renewing:
				print(ans)
				break


T = int(input())
for g in range(T):
	(n, s, k) = map(int, input().split())
	l1 = [int(i) for i in input().split()]
	m = -1
	for j in range(s, 0, -1):
		if l1.count(j) == 0:
			m = abs(j - s)
			break
	for j in range(s + 1, n + 1):
		if l1.count(j) == 0:
			if abs(j - s) < m or m == -1:
				m = abs(j - s)
			break
	print(m)


for _ in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	if k == 1:
		if arr[0] != s:
			print(0)
		else:
			print(1)
	elif s not in arr:
		print(0)
	else:
		i = 1
		arr.sort()
		ans = 10 ** 10
		while i < k and arr[i] <= s:
			if arr[i] - arr[i - 1] > 1:
				ans = min(ans, s - arr[i] + 1)
			i += 1
		while i < k:
			if arr[i] - arr[i - 1] > 1:
				ans = min(ans, arr[i - 1] - s + 1)
			i += 1
		if arr[k - 1] < n:
			ans = min(ans, arr[k - 1] - s + 1)
		if arr[0] > 1:
			ans = min(ans, s - (arr[0] - 1))
		print(ans)


for z in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	a1 = n + 1
	a2 = n + 1
	if s not in a:
		ans = 0
	else:
		for i in range(s + 1, n + 1):
			if i not in a:
				a1 = i - s
				break
		for i in range(s - 1, 0, -1):
			if i not in a:
				a2 = s - i
				break
		ans = min(a1, a2)
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	rs = list(map(int, input().split()))
	d = {}
	for i in range(k):
		d[rs[i]] = 1
	i = 0
	while 1:
		if s + i <= n and s + i not in d or (s - i >= 1 and s - i not in d):
			print(i)
			break
		i += 1


for __ in range(int(input())):
	(n, s, k) = map(int, input().split(' '))
	forbidden_ones = list(map(int, input().split(' ')))
	res = 1000000000.0
	for i in range(min(len(forbidden_ones) + 1, n - s + 1)):
		if s + i not in forbidden_ones:
			res = i
			break
	for i in range(min(len(forbidden_ones) + 1, s)):
		if s - i not in forbidden_ones:
			if i < res:
				res = i
			break
	print(res)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	mini = 10 ** 9 + 5
	arr = list(map(int, input().split()))
	d = dict()
	for i in arr:
		d[i] = 1
	for i in range(s, n + 1):
		x = abs(i - s)
		if i not in d:
			mini = x
			break
	for i in range(s, 0, -1):
		x = abs(i - s)
		if i not in d:
			mini = min(mini, x)
			break
	print(mini)


import bisect
testcases = int(input())
for testcase in range(testcases):
	temparr = input()
	temparr = temparr.split()
	n = int(temparr[0])
	cur = int(temparr[1])
	closen = int(temparr[2])
	k = closen
	temparr = input()
	temparr = temparr.split()
	store = []
	storeclosed = []
	storeclosed.append(cur)
	flag = 0
	for i in temparr:
		i = int(i)
		if i == cur:
			flag = 1
			continue
		storeclosed.append(i)
	if closen == n - 1 and flag == 0:
		print(0)
		continue
	if flag == 0:
		print(0)
		continue
	storeclosed = sorted(storeclosed)
	index = bisect.bisect_left(storeclosed, cur)
	leftstore = cur - 1
	rightstore = cur + 1
	leftindex = index - 1
	rightindex = index + 1
	counter = 1
	storelen = len(storeclosed)
	while True:
		if leftindex < 0 and leftstore < 1:
			if leftstore >= 1:
				print(counter)
				break
			if rightindex >= storelen:
				print(counter)
				break
			if storeclosed[rightindex] != rightstore:
				print(counter)
				break
			rightindex += 1
			rightstore += 1
			counter += 1
			continue
		if rightindex >= k:
			if rightstore <= n:
				print(counter)
				break
			if leftindex == -1:
				print(counter)
				break
			if storeclosed[leftindex] != leftstore:
				print(counter)
				break
			leftindex -= 1
			leftstore -= 1
			counter += 1
			continue
		if storeclosed[leftindex] != leftstore:
			print(counter)
			break
		if storeclosed[rightindex] != rightstore:
			print(counter)
			break
		counter += 1
		leftstore -= 1
		leftindex -= 1
		rightstore += 1
		rightindex += 1


def stairs(n, l, s):
	d = {}
	for e in l:
		d[e - 1] = True
	(i, j) = (s - 1, s - 1)
	while i > -1 or j < n:
		if i > -1:
			if not d.get(i):
				return s - 1 - i
			i -= 1
		if j < n:
			if not d.get(j) == 1:
				return j - s + 1
			j += 1
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().strip().split())
	l = list(map(int, input().strip().split()))
	print(stairs(n, l, s))


t = int(input())
while t > 0:
	t -= 1
	nsk = input().split()
	n = int(nsk[0])
	s = int(nsk[1])
	k = int(nsk[2])
	x = input().split()
	l = [0] * k
	for i in range(k):
		l[i] = int(x[i])
	f = True
	c = s
	d = s
	count = 0
	while c > 0 or d <= n:
		if c > 0:
			if c in l:
				c -= 1
			else:
				count = s - c
				break
		if d <= n:
			if d in l:
				d += 1
			else:
				count = d - s
				break
	print(count)


t = int(input())
for _ in range(t):
	s1 = {}
	(n, s, k) = map(int, input().split())
	k1 = list(map(int, input().split()))
	for i in k1:
		s1[i] = 1
	if s not in s1.keys():
		t = 0
	else:
		i = s
		j = s
		i1 = False
		j1 = False
		t = 1
		while i >= 1 or j <= n:
			i -= 1
			j += 1
			if i >= 1:
				i1 = True
			if j <= n:
				j1 = True
			if i in s1.keys() and i >= 1:
				i1 = False
			if j in s1.keys() and j <= n:
				j1 = False
			if i1 or j1:
				break
			else:
				t += 1
	print(t)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	x = list(map(int, input().split()))
	x.append(0)
	x.append(n + 1)
	x.sort()
	c = 1000000000000.0
	for i in range(k + 1):
		if x[i + 1] - x[i] > 1:
			if s < x[i + 1] and s > x[i]:
				c = min(c, 0)
			if s > x[i + 1]:
				c = min(c, abs(x[i + 1] - 1 - s))
			if s < x[i]:
				c = min(c, abs(x[i] + 1 - s))
			if s == x[i] or s == x[i + 1]:
				c = min(c, 1)
	print(c)


from sys import stdin

def lc():
	return [int(i) for i in stdin.readline().split()]
t = int(stdin.readline())
while t:
	(n, s, k) = lc()
	fr = lc()
	fr.sort()
	var1 = s - 1
	var2 = s + 1
	if s not in fr:
		print(0)
	else:
		while True:
			if var1 <= 0:
				var1 = 1
			if var2 >= n:
				var2 = n
			if var1 not in fr or var2 not in fr:
				print(max(s - var1, var2 - s))
				break
			else:
				var1 -= 1
				var2 += 1
	t -= 1


from sys import stdin, stdout
import gc
gc.disable()
input = stdin.readline
t = int(input().strip())
for _ in range(t):
	(n, s, k) = list(map(int, input().split()))
	closed = set(map(int, input().split()))
	first = 0
	cur = s
	while cur <= n:
		if cur not in closed:
			break
		cur += 1
		first += 1
	else:
		first = float('inf')
	second = 0
	cur = s
	while cur > 0:
		if cur not in closed:
			break
		cur -= 1
		second += 1
	else:
		second = float('inf')
	print(min(first, second))


a = int(input())
for i in range(a):
	c = input().split()
	n = int(c[0])
	s = int(c[1])
	e = list(map(int, input().split()))
	min = n
	for j in range(n):
		if s + j <= n and s + j not in e:
			print(j)
			break
		if s - j > 0 and s - j not in e:
			print(j)
			break


for i in range(int(input())):
	floors = []
	(n, s, k) = map(int, input().split())
	floors = list(map(int, input().split()))
	down = 0
	up = 0
	for j in range(s, 0, -1):
		if j not in floors:
			down = j
			break
	for k in range(s, n + 1):
		if k not in floors:
			up = k
			break
	floors.clear()
	if down == 0:
		print(abs(up - s))
	elif up == 0:
		print(abs(down - s))
	elif down != 0 and up != 0:
		print(min(abs(up - s), abs(down - s)))


def find_dist(n, s, k, a):
	a.sort()
	p = -1
	for pos in range(k):
		if a[pos] == s:
			p = pos
			break
		elif a[pos] > s:
			return 0
	if p == -1:
		return 0
	max_l = 0
	max_r = 0
	for d in range(1, 1001):
		if p - d >= 0:
			if a[p - d] != s - d:
				max_l = d
				break
		else:
			if a[0] != 1:
				max_l = d
			break
	for d in range(1, 1001):
		if p + d < k:
			if a[p + d] != s + d:
				max_r = d
				break
		else:
			if a[k - 1] != n:
				max_r = d
			break
	if max_l == 0:
		return max_r
	if max_r == 0:
		return max_l
	return min(max_l, max_r)
q = int(input())
for i in range(q):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	print(find_dist(n, s, k, a))


import math
n = int(input())
for i in range(n):
	e = 10 ** 10
	f = 10 ** 10
	(a, b, c) = [int(i) for i in input().split()]
	d = [int(i) for i in input().split()]
	for i in range(b, a + 1):
		if i not in d:
			e = i
			break
	for i in range(b, 0, -1):
		if i not in d:
			f = i
			break
	if e < 10 ** 10 or f < 10 ** 10:
		print(min(abs(b - e), abs(b - f)))
	else:
		print(0)


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	(x, y) = (s, s)
	(p, q) = (0, 0)
	for i in range(x, 0, -1):
		if i not in a:
			p += 1
			break
	for j in range(y, n + 1):
		if j not in a:
			q += 1
			break
	if p != 0 and q != 0:
		print(min(s - i, j - s))
	elif p == 0:
		print(j - s)
	elif q == 0:
		print(s - i)


n = int(input())
for i in range(n):
	(n, s, k) = map(int, input().split())
	l = sorted(list(map(int, input().split())))
	for j in range(n):
		if max(s - j, 1) not in l or min(s + j, n) not in l:
			print(j)
			break


q = int(input())
for _ in range(q):
	(n, s, k) = [int(t) for t in input().split(' ')]
	m = set((int(t) for t in input().split(' ')))
	mn = 1000
	for i in range(s, min(s + 1001, n + 1)):
		if not i in m:
			mn = min(mn, abs(s - i))
	for i in range(max(1, s - 1200), s):
		if not i in m:
			mn = min(mn, abs(s - i))
	print(mn)


T = int(input())
for _ in range(T):
	(n, s, k) = map(int, input().split())
	illegal = [int(i) for i in input().split()]
	for i in range(0, k + 1):
		u = s - i
		v = s + i
		if u >= 1 and (not u in illegal) or (v <= n and (not v in illegal)):
			print(i)
			break


def bs(a, n, x):
	low = 0
	high = n - 1
	ans = -1
	while low <= high:
		mid = (low + high) // 2
		if a[mid] == x:
			return mid
		if a[mid] < x:
			low = mid + 1
		else:
			high = mid - 1
	return ans
for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	i = bs(l, k, s)
	if i == -1:
		print(0)
	else:
		(le, re) = (-1, -1)
		lj = i - 1
		c = 1
		while lj >= 0:
			if l[lj] != s - c:
				le = s - c
				break
			c += 1
			lj -= 1
		rj = i + 1
		c = 1
		while rj < k:
			if l[rj] != s + c:
				re = s + c
				break
			c += 1
			rj += 1
		if re == -1 and k < n and (l[k - 1] < n):
			re = l[k - 1] + 1
		if le == -1 and k < n and (l[0] > 1):
			le = l[0] - 1
		if le == -1 and re == -1:
			print(le)
		elif le == -1:
			print(re - s)
		elif re == -1:
			print(s - le)
		else:
			print(min(s - le, re - s))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	se = set(map(int, input().split()))
	m1 = 9999999999999999
	m2 = 9999999999999999
	for i in range(s, 0, -1):
		if i not in se:
			m1 = s - i
			break
	for i in range(s, n + 1):
		if i not in se:
			m2 = i - s
			break
	print(min(m1, m2))


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in range(n):
		if s - i > 0:
			if s - i not in l:
				print(i)
				break
		if s + i <= n:
			if s + i not in l:
				print(i)
				break


def main():
	t = int(input())
	out_lst = []
	for i in range(t):
		a = list(map(int, input().strip().split()))[:3]
		n = a[0]
		s = a[1]
		k = a[2]
		if n < 1 or n > 1000000000:
			return False
		elif s > n or s < 1:
			return False
		elif k > n or k > 11000 or k < 1:
			return False
		else:
			k_lst = list(map(int, input().strip().split()))[:k]
			if s in k_lst:
				p = s
				m = s
				p_c = 0
				m_c = 0
				while True:
					p += 1
					p_c += 1
					if p <= n:
						if p not in k_lst:
							break
					else:
						p_c = n
						break
				while True:
					m -= 1
					m_c += 1
					if m > 0:
						if m not in k_lst:
							break
					else:
						m_c = n
						break
				out_lst.append(min(p_c, m_c))
			else:
				out_lst.append(0)
	for i in out_lst:
		print(i)
main()


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set(list(map(int, input().split())))
	sr = s
	sl = s
	ansl = 0
	ansr = 0
	endl = False
	endr = False
	while not endl:
		if sl in a:
			if sl - 1 > 0:
				sl -= 1
				ansl += 1
			else:
				endl = True
				ansl = 1001
		else:
			endl = True
	while not endr:
		if sr in a:
			if sr + 1 <= n:
				sr += 1
				ansr += 1
			else:
				endr = True
				ansr = 1001
		else:
			endr = True
	ans = min(ansr, ansl)
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, s, k) = [int(i) for i in sys.stdin.readline().split()]
	floors = set([int(i) for i in sys.stdin.readline().split()])
	for ans in range(1002):
		if s + ans <= n and s + ans not in floors:
			print(ans)
			break
		if s - ans >= 1 and s - ans not in floors:
			print(ans)
			break


def f(n, L, x):
	if x not in L:
		return 0
	d = {}
	for i in range(len(L)):
		d[L[i]] = True
	counter = x + 1
	counter2 = x - 1
	while counter <= n:
		if counter not in d:
			break
		counter = counter + 1
	while counter2 > 0:
		if counter2 not in d:
			break
		counter2 = counter2 - 1
	if counter2 == 0:
		return abs(x - counter)
	elif counter == n + 1:
		return abs(x - counter2)
	return min(abs(x - counter), abs(x - counter2))
s = int(input(''))
while s != 0:
	z = [int(x) for x in input('').split()]
	z2 = [int(x) for x in input('').split()]
	print(f(z[0], z2, z[1]))
	s = s - 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	num = list(map(int, input().split()))
	for i in range(n):
		left = s - i
		right = s + i
		if left >= 1 and left not in num:
			print(i)
			break
		if right <= n and right not in num:
			print(i)
			break
	else:
		assert False


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = set(a)
	if s in b:
		d = 1
		while True:
			if s - d >= 1 and s - d not in b:
				break
			if s + d <= n and s + d not in b:
				break
			d += 1
		print(d)
	else:
		print(0)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	closed = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in closed:
			print(i)
			break
		elif s + i <= n and s + i not in closed:
			print(i)
			break


def getAns():
	a.sort()
	if s < a[0] or s > a[-1]:
		return 0
	ans = 1000000000
	if a[0] != 1:
		ans = s - a[0] + 1
	if a[-1] != n:
		ans = min(ans, a[-1] + 1 - s)
	for i in range(k - 1):
		if a[i + 1] - a[i] > 1:
			if a[i] < s < a[i + 1]:
				return 0
			ans = min(ans, abs(s - a[i] - 1))
			ans = min(ans, abs(s - a[i + 1] + 1))
	return ans
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	print(getAns())


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	dictt = {}
	for j in a:
		dictt[j] = 1
	for i in range(max(s, n - s + 1)):
		if i + s <= n and i + s not in dictt:
			break
		if s - i > 0 and s - i not in dictt:
			break
	print(i)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s in a:
		min = n
		for i in range(1, n):
			m = s + i
			l = s - i
			if m not in a and m <= n:
				if i < min:
					min = i
					break
			if l not in a and l >= 1:
				if i < min:
					min = i
					break
		print(min)
	else:
		print(0)


T = int(input())
for ti in range(T):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	closed.sort()
	if s not in closed:
		print(0)
	else:
		(l, r) = (s - 1, s + 1)
		if l < 1:
			l = 1
		if r > n:
			r = n
		c = 1
		while l in closed and r in closed:
			c += 1
			l -= 1
			r += 1
			if l < 1:
				l = 1
			if r > n:
				r = n
		print(c)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	bad = set(map(int, input().split()))
	ds = 0
	while True:
		if s + ds <= n and s + ds not in bad:
			break
		if s - ds >= 1 and s - ds not in bad:
			break
		ds += 1
	print(ds)


t = int(input())
for ll in range(t):
	(n, s, k) = [int(m) for m in input().split(' ')]
	floors = set()
	vals = [int(m) for m in input().split(' ')]
	for val in vals:
		floors.add(val)
	if s not in floors:
		print(0)
	else:
		dif = 1
		while True:
			if s + dif <= n and s + dif not in floors:
				print(dif)
				break
			if s - dif > 0 and s - dif not in floors:
				print(dif)
				break
			dif += 1


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in range(1001):
		if s - i > 0:
			c = l.count(s - i)
			if c != 0:
				pass
			else:
				print(i)
				break
		if s + i <= n:
			p = l.count(s + i)
			if p != 0:
				pass
			else:
				print(i)
				break


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().strip().split()))
	closed = list(map(int, input().strip().split()))
	if s not in closed:
		print(0)
		continue
	for i in range(1, n + 1):
		up = s + i
		down = s - i
		if up <= n:
			if up not in closed:
				print(i)
				break
		if down >= 1:
			if down not in closed:
				print(i)
				break


q = int(input())
for qq in range(q):
	res = -1
	(n, s, k) = map(int, input().split())
	dat = list(map(int, input().split()))
	dat = list(set(dat))
	dat.sort()
	if s not in dat:
		print(0)
		continue
	ind = dat.index(s)
	r = l = -1
	for i in range(ind + 1, len(dat)):
		if dat[i] - dat[i - 1] != 1:
			r = dat[i - 1] + 1
			break
	if r == -1 and dat[-1] != n:
		r = dat[-1] + 1
	for i in range(ind - 1, -1, -1):
		if dat[i + 1] - dat[i] != 1:
			l = dat[i + 1] - 1
			break
	if l == -1 and dat[0] != 1:
		l = dat[0] - 1
	if r == l == -1:
		print('!!!!!!!!!!!!!!!!!!')
	elif r == -1:
		print(s - l)
	elif l == -1:
		print(r - s)
	else:
		print(min(s - l, r - s))


for i in range(int(input())):
	[n, s, k] = list(map(int, input().split()))
	up = s
	down = s
	found_up = False
	found_down = False
	renovation = list(map(int, input().split()))
	while up <= n and up in renovation:
		up += 1
		if up <= n and up not in renovation:
			found_up = True
	while down >= 1 and down in renovation:
		down -= 1
		if down >= 1 and down not in renovation:
			found_down = True
	if found_down == True and found_up == True:
		print(min(up - s, s - down))
	elif found_up == True and found_down == False:
		print(up - s)
	else:
		print(s - down)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	x = 0
	l = set(l)
	if s not in l:
		print(0)
		continue
	while 1:
		if min(n, s + x) not in l or max(1, s - x) not in l:
			print(x)
			break
		x += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	for i in range(n):
		r = s + i
		if r <= n and r not in a:
			print(abs(r - s))
			break
		r = s - i
		if r > 0 and r not in a:
			print(abs(r - s))
			break


import math
testCases = int(input())
for _ in range(testCases):
	(n, s, k) = [int(x) for x in input().split()]
	array = [int(x) for x in input().split()]
	if s not in array:
		print(0)
	else:
		answer = 0
		checkFloor = 0
		while True:
			checkFloor += 1
			if s - checkFloor > 0:
				lower = s - checkFloor
			else:
				lower = -1
			if s + checkFloor <= n:
				upper = s + checkFloor
			else:
				upper = -1
			if lower != -1 and lower not in array:
				answer = abs(s - lower)
				break
			if upper != -1 and upper not in array:
				answer = abs(s - upper)
				break
		print(answer)


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	ans = None
	for i in range(k + 1):
		if s - i > 0:
			if s - i not in c:
				ans = i
		if s + i <= n:
			if s + i not in c:
				ans = i
		if ans is not None:
			break
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l1 = list(map(int, input().split()))
	if s not in l1:
		print(0)
	else:
		i = 1
		while 1:
			if s - i > 0 and s - i not in l1:
				print(i)
				break
			elif s + i <= n and s + i not in l1:
				print(i)
				break
			i = i + 1


t = int(input())
for ii in range(t):
	(n, s, m) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	se = set(a)
	p = s
	an = []
	if p not in a:
		print(0)
		continue
	while 1:
		if p == 0:
			break
		if p in a:
			p -= 1
		else:
			an.append(p)
			break
	p = s
	while 1:
		if p == n + 1:
			break
		if p in a:
			p += 1
		else:
			an.append(p)
			break
	for i in range(len(an)):
		an[i] = abs(s - an[i])
	print(min(an))


def main():
	for _ in range(int(input())):
		(n, s, k) = map(int, input().split())
		a = list(map(int, input().split()))
		if s not in a:
			print(0)
			continue
		ans = 1
		t1 = s + 1
		t2 = s - 1
		for i in range(n):
			if t2 > 0:
				if t2 not in a:
					break
				t2 -= 1
			if t1 <= n:
				if t1 not in a:
					break
				t1 += 1
			ans += 1
		print(ans)
main()


from sys import stdin
t = int(stdin.readline().rstrip())
while t > 0:
	(n, s, k) = map(int, stdin.readline().split())
	se = set()
	l = list(map(int, stdin.readline().split()))
	for i in l:
		se.add(i)
	if s not in se:
		print(0)
	else:
		m = pow(10, 9)
		for i in range(s - 1, max(0, s - 1001), -1):
			if i not in se:
				m = min(m, abs(s - i))
				break
		for i in range(s + 1, min(n + 1, s + 1001), 1):
			if i not in se:
				m = min(m, abs(s - i))
				break
		print(m)
	t -= 1


t = int(input())
while t > 0:
	t = t - 1
	(n, s, k) = map(int, input().split())
	st = input().split()
	s = s - 1
	a = [int(i) - 1 for i in st]
	dis = []
	dis.append(k)
	for i in range(max(0, s - k), min(s + k, n)):
		if i not in a:
			dis.append(abs(s - i))
	print(min(dis))


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	se = set(map(int, input().split()))
	i = 0
	while True:
		if s - i > 0 and s - i not in se or (s + i <= n and s + i not in se):
			print(i)
			break
		else:
			i += 1


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	st = set(list(map(int, input().split())))
	if s not in st:
		print(0)
		continue
	i = s
	c = 0
	c2 = 0
	while i in st and i <= n:
		i += 1
		c += 1
	(valid1, valid2) = (False, False)
	if i <= n and i not in st:
		valid1 = True
	i = s
	while i <= n and i in st:
		i -= 1
		c2 += 1
	if i >= 1 and i not in st:
		valid2 = True
	if valid1 and valid2:
		print(min(c, c2))
	elif valid1:
		print(c)
	else:
		print(c2)


for _ in range(int(input())):
	(n, k, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	ss = set(arr)
	i = k
	j = k
	while i <= n or j > 0:
		if i <= n and i not in ss:
			print(i - k)
			break
		elif j > 0 and j not in ss:
			print(k - j)
			break
		i += 1
		j -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, s, k) = list(map(int, input().split()))
	it = list(map(int, input().split()))
	it = set(it)
	for i in range(n):
		if s + i <= n and s + i not in it:
			ans = i
			break
		if s - i >= 1 and s - i not in it:
			ans = i
			break
	print(ans)


from collections import defaultdict
import math
a = int(input())
for i in range(a):
	save1 = math.inf
	save2 = math.inf
	al = defaultdict(int)
	(n, s, k) = map(int, input().split())
	ans = list(map(int, input().split()))
	for i in range(len(ans)):
		al[ans[i]] = 1
	for i in range(2000):
		if al[s + i] == 0 and s + i <= n:
			save1 = i
			break
	for i in range(2000):
		if s - i > 0:
			if al[s - i] == 0:
				save2 = i
				break
	print(min(save1, save2))


t = int(input())
for k in range(0, t):
	(n, s, k) = map(int, input().split())
	close_rest = list(map(int, input().split()))
	if s not in close_rest:
		ans = 0
		print(ans)
	else:
		ans1 = n + 1
		ans2 = n + 1
		for i in range(s + 1, n + 1):
			if i not in close_rest:
				ans1 = i - s
				break
		for j in range(s - 1, 0, -1):
			if j not in close_rest:
				ans2 = s - j
				break
		ans = min(ans1, ans2)
		print(ans)


t = int(input())
result = []
for i in range(t):
	(n, s, k) = map(int, input().split())
	blocks = list(map(int, input().split()))
	i = 0
	while max(1, s - i) in blocks and min(s + i, n) in blocks:
		i += 1
	result.append(i)
for k in range(len(result)):
	print(result[k])


for x in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	h = list(map(int, input().split()))
	i = 0
	while max(s - i, 1) in h and min(s + i, n) in h:
		i += 1
	print(i)


def binary_search(arr, l, r, elem):
	if r >= l:
		m = (l + r) // 2
		if arr[m] == elem:
			return m
		elif elem > arr[m]:
			return binary_search(arr, m + 1, r, elem)
		elif elem < arr[m]:
			return binary_search(arr, l, m - 1, elem)
	else:
		return -1
for _ in range(int(input())):
	(num_floor, c_floor, num_closed) = map(int, input().split())
	closed_arr = sorted(list(map(int, input().split())))
	c_idx = None
	r = binary_search(closed_arr, 0, num_closed - 1, c_floor)
	if r == -1:
		print(0)
		continue
	else:
		c_idx = r
	c_left = 0
	c_right = 0
	while True:
		c_left -= 1
		if c_idx + c_left >= 0:
			if c_floor + c_left != closed_arr[c_idx + c_left]:
				print(-1 * c_left)
				break
		c_right += 1
		if c_idx + c_right <= num_closed - 1:
			if c_floor + c_right != closed_arr[c_idx + c_right]:
				print(c_right)
				break
		if c_idx + c_left < 0 and closed_arr[0] > 1:
			print(-1 * c_left)
			break
		if c_idx + c_right > num_closed - 1 and closed_arr[-1] < num_floor:
			print(c_right)
			break


def solve(f, n, s, k):
	for i in range(n):
		if s - i >= 0 and s - i + 1 not in f:
			return i
		if s + i < n and s + i + 1 not in f:
			return i
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	print(' ', solve(set(a), n, s - 1, k))


t = int(input())
for j in range(t):
	ch = input()
	L = [int(i) for i in ch.split()]
	n = L[0]
	s = L[1]
	k = L[2]
	ch = input()
	L = [int(i) for i in ch.split()]
	S = set(L)
	for i in range(n):
		if s - i not in S and s - i > 0 or (s + i not in S and s + i <= n):
			print(i)
			break


t = int(input())
for _ in range(t):
	n = list(map(int, input().rstrip().split()))
	a = n[0]
	b = n[1]
	c = n[2]
	x = list(map(int, input().rstrip().split()))
	k = []
	l = []
	for i in range(b, 0, -1):
		if i not in x:
			k.append(i)
			break
	for i in range(b + 1, a + 1):
		if i not in x:
			l.append(i)
			break
	if len(k) == 0:
		print(l[0] - b)
	elif len(l) == 0:
		print(b - k[0])
	elif b - k[0] <= l[0] - b:
		print(b - k[0])
	else:
		print(l[0] - b)


x = int(input())
for _ in range(x):
	(n, s, k) = map(int, input().strip().split())
	a = [int(i) for i in input().strip().split()]
	b = []
	if s not in a:
		print(0)
	else:
		s1 = 0
		s2 = 0
		for i in range(s, n + 1):
			if i not in a:
				s1 = i - s
				break
		for i in range(s, 0, -1):
			if i not in a:
				s2 = s - i
				break
		if s1 != 0 and s2 != 0:
			print(min(s1, s2))
		elif s1 == 0:
			print(s2)
		else:
			print(s1)


t = int(input())
for i in range(0, t):
	(n, s, k) = [int(x) for x in input().split()]
	a = []
	a = list(map(int, input().split(' ')))
	cnt = 0
	z = 0
	while k + 1 >= 1:
		if s - z not in a and s - z >= 1:
			print(cnt)
			break
		elif s + z not in a and s + z <= n:
			print(cnt)
			break
		k = k - 1
		z = z + 1
		cnt = cnt + 1


import sys
input = sys.stdin.readline
t = int(input().strip())
while t > 0:
	(n, s, k) = map(int, input().split())
	ans = n
	a = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in a:
			print(i)
			break
		if s + i <= n and s + i not in a:
			print(i)
			break
	t -= 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	if s not in a:
		print(0)
		continue
	for i in range(1, 1001):
		if 0 < s - i <= n:
			if s - i not in a:
				print(i)
				break
		if 0 < s + i <= n:
			if s + i not in a:
				print(i)
				break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in a:
			print(i)
			break
		elif s + i <= n and s + i not in a:
			print(i)
			break


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	for x in range(n):
		if s - x > 0 and s - x not in a:
			print(x)
			break
		elif s + x <= n and s + x not in a:
			print(x)
			break


from bisect import *
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.append(1e+18)
	a = [-1e+18] + a
	a.sort()
	b = list(a)
	f = bisect_left(a, s)
	ans = 1e+18
	if a[f] == s:
		a1 = 1e+18
		a2 = 1e+18
		for i in range(s - 1, 0, -1):
			m = bisect_left(a, i)
			if a[m] == i:
				continue
			else:
				a1 = abs(s - i)
				break
		else:
			a1 = 1e+19
		for i in range(s + 1, n + 1):
			m = bisect_left(a, i)
			if a[m] == i:
				continue
			else:
				a2 = abs(s - i)
				break
		else:
			a2 = 1e+19
		print(min(a1, a2))
	else:
		print(0)


def everyNumber(n, s, k, k_lst):
	move = [s, s]
	while move[0] >= 1:
		if move[0] not in k_lst:
			break
		move[0] -= 1
	if move[0] < 1:
		del move[0]
	while move[-1] <= n:
		if move[-1] not in k_lst:
			break
		move[-1] += 1
	if move[-1] > n:
		del move[-1]
	return min(abs(s - move[0]), abs(move[-1] - s))

def main():
	t = int(input())
	output_lst = list()
	number = list()
	for i in range(t):
		(n, s, k) = map(int, input().split())
		k_lst = list(map(int, input().split()))
		number.append(everyNumber(n, s, k, k_lst))
	for i in number:
		print(i)
main()


for _ in range(int(input())):
	(n, s, k) = (int(x) for x in input().strip().split())
	arr = [int(x) for x in input().strip().split()]
	if s not in arr:
		print(0)
	else:
		a = set(arr)
		i = s
		j = s
		while i > 0:
			if i not in a:
				ind = i
				break
			i -= 1
		while j < n + 1:
			if j not in a:
				ind = j
				break
			j += 1
		if i != 0 and j != n + 1:
			print(min(abs(j - s), abs(i - s)))
		elif i == 0:
			print(abs(j - s))
		else:
			print(abs(i - s))


def inp():
	return map(int, input().split())
INF = int(1000000000.0) + 1

def solve():
	(n, s, k) = inp()
	rest = list(inp())
	d = {}
	for i in range(k):
		rest[i] = abs(s - rest[i])
	rest.sort()
	if rest[0] == 0:
		rest = [0] + rest
	res = 0
	pair = False
	for e in rest:
		if e == res:
			if pair or s + e > n or s <= e:
				res = e + 1
				pair = False
			else:
				pair = True
		else:
			pair = False
	return res

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


import math
import sys

def listinput():
	return list(map(int, input().split(' ')))

def main():
	answer = ''
	for test in range(int(input())):
		(n, s, k) = listinput()
		closed = set(listinput())
		for i in range(k + 1):
			(l, h) = (s - i, s + i)
			if l <= 0:
				l = 1
			if h > n:
				h = n
			if l not in closed or h not in closed:
				answer += str(i) + '\n'
				break
		pass
	print(answer)
main()


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	j = s
	g = s
	while j in a and j <= n:
		j += 1
	while g in a and g > 0:
		g -= 1
	answer = n
	if j < n + 1:
		answer = min(answer, j - s)
	if g > 0:
		answer = min(answer, s - g)
	print(answer)


import sys
from bisect import bisect_left as bl, bisect_right as br, bisect
import math
from timeit import default_timer as dt
from itertools import zip_longest as zl
from itertools import groupby as gb
from itertools import combinations as comb
from itertools import combinations_with_replacement as cwr
from collections import defaultdict as dd
from collections import deque as dq
from collections import Counter as c
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return input()

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]
for _ in range(int(data())):
	(n, s, k) = sp()
	arr = set(l())
	result = mod
	for i in range(max(1, s - 1000), min(n, s + 1000) + 1):
		if i not in arr:
			result = min(abs(s - i), result)
	print(result)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if not s in a:
		print(0)
		continue
	else:
		x = s - 1
		y = s + 1
		while x >= 0 or y <= n:
			if not x in a and x > 0:
				print(s - x)
				break
			if not y in a and y <= n:
				print(y - s)
				break
			x = x - 1
			y = y + 1


from bisect import bisect_left
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	now = bisect_left(a, s)
	if now >= k or now <= -1 or a[now] != s:
		print(0)
		continue
	else:
		(tmp1, tmp2) = (10 ** 10, 10 ** 10)
		i = 1
		check = True
		while check:
			if now - i < 0 and a[0] > 1:
				tmp1 = s - (a[0] - 1)
				check = False
			elif k - 1 > now - i >= 0 and a[now - i] != a[now] - i:
				tmp1 = s - (a[now - i + 1] - 1)
				check = False
			if now + i >= k and a[k - 1] < n:
				tmp2 = a[k - 1] + 1 - s
				check = False
			elif 0 < now + i < k and a[now + i] != a[now] + i:
				tmp2 = a[now + i - 1] + 1 - s
				check = False
			if check:
				i += 1
		print(min(tmp1, tmp2))


for _ in range(int(input())):
	(w, find, si) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	if find not in l:
		print(0)
	else:
		idx = l.index(find)
		(upper, low) = (idx, idx)
		while upper < si - 1 and l[upper] + 1 == l[1 + upper]:
			upper += 1
		while low > 0 and l[low - 1] + 1 == l[low]:
			low -= 1
		low_ = l[low]
		upper_ = l[upper]
		if low_ == 1 and l[low] == 1:
			low_ = float('inf')
		if upper_ == w and l[upper] == w:
			upper_ = float('-inf')
		if l[low] == 1:
			print(l[upper] + 1 - find)
		elif l[upper] == w:
			print(find - (l[low] - 1))
		else:
			print(min(find - (low_ - 1), upper_ + 1 - find))


t = int(input())
for i in range(t):
	(n, s, k) = (int(i) for i in input().split())
	l1 = [int(i) for i in input().split()]
	if s not in l1:
		print(0)
	else:
		for i in range(1, n):
			if s - i > 0 and s - i not in l1 or (s + i <= n and s + i not in l1):
				print(i)
				break


def binarySearch(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	s1 = set(l)
	e = []
	ans = -1
	l.sort()
	w = binarySearch(l, 0, k - 1, s)
	if w == -1:
		print(0)
		continue
	(hh1, hh2) = (1, 1)
	p1 = l[w]
	p2 = l[w]
	while p1 > 0 and hh1 == 1:
		if p1 not in s1:
			hh1 = 2
			break
		p1 = p1 - 1
	while p2 <= n and hh2 == 1:
		if p2 not in s1:
			hh2 = 2
			break
		p2 = p2 + 1
	if hh1 == 2 and hh2 == 1:
		print(abs(s - p1))
	elif hh2 == 2 and hh1 == 1:
		print(abs(s - p2))
	elif hh2 == 2 and hh1 == 2:
		print(min(abs(s - p1), abs(s - p2)))


from math import factorial
from collections import Counter

def RL():
	return list(map(int, input().split()))

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0
mod = 1000000007
N = int(input())
for _ in range(N):
	(n, s, k) = RL()
	clo = set(RL())
	res = 0
	while True:
		if s + res <= n and s + res not in clo or (s - res > 0 and s - res not in clo):
			print(res)
			break
		res += 1


from bisect import bisect_left

def index(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return -1

def get_shortest_path(floors, current, closed_foors):
	if current not in closed_floors:
		return 0
	i = 1
	while True:
		add = current + i
		minus = current - i
		if add <= floors and index(closed_floors, add) == -1:
			return i
		if minus > 0 and index(closed_floors, minus) == -1:
			return i
		i += 1
n = int(input())
for i in range(n):
	(floors, current, closed) = map(int, input().split())
	closed_floors = list(map(int, input().split()))
	closed_floors.sort()
	print(get_shortest_path(floors, current, closed_floors))


from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_str():
	return list(sys.stdin.readline().split())
T = inp()
for _ in range(T):
	(n, s, k) = inpl()
	aa = set(inpl())
	ans = INF
	for x in range(max(1, s - k - 5), min(n + 1, s + k + 5)):
		if x in aa:
			continue
		else:
			ans = min(ans, abs(x - s))
	print(ans)


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	d = {el: 0 for el in list(map(int, input().split()))}
	res = 0
	while True:
		if s + res <= n and s + res not in d:
			break
		if s - res >= 1 and s - res not in d:
			break
		res += 1
	print(res)


def get_shortest_path(floors, current, closed_foors, closed):
	if current not in closed_floors:
		return 0
	highers = [i + current for i in range(0, closed + 1)]
	lowers = [current - i for i in range(closed, 0, -1)]
	(lower_h, lower_s) = (10000000000, -10000000000)
	for i in highers:
		if i < lower_h and i <= floors and (i not in closed_floors):
			lower_h = i
	for i in lowers:
		if i > lower_s and i > 0 and (i not in closed_floors):
			lower_s = i
	lower_h = abs(abs(lower_h) - current)
	lower_s = abs(abs(lower_s) - current)
	return lower_h if lower_h < lower_s else lower_s
n = int(input())
for i in range(n):
	(floors, current, closed) = map(int, input().split())
	closed_floors = set(map(int, input().split()))
	print(get_shortest_path(floors, current, closed_floors, closed))


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	c = -1
	d = -1
	if s in arr:
		for i in range(s, n + 1):
			if i not in arr:
				c = i
				break
		for j in range(s, 0, -1):
			if j not in arr:
				d = j
				break
		if c == -1:
			print(abs(d - s))
		elif d == -1:
			print(abs(c - s))
		else:
			print(min(abs(c - s), abs(d - s)))
	else:
		print(0)


t = int(input())
for i in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	d = {}
	x = input().split()
	for e in x:
		d[int(e)] = ''
	for j in range(0, k + 1):
		if d.get(s - j) is None and s - j >= 1:
			print(j)
			break
		if d.get(s + j) is None and s + j <= n:
			print(j)
			break


import bisect
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list((int(i) for i in input().split()))
	a.sort()
	if s not in a:
		print(0)
	elif s <= min(a) and min(a) > 1:
		print(1)
	elif s >= max(a) and max(a) < n:
		print(1)
	else:
		index1 = index2 = 0
		y = bisect.bisect(a, s)
		for i in range(a[y - 1], n + 1):
			if i not in a:
				index1 = i
				break
		for j in range(a[y - 1], 0, -1):
			if j not in a:
				index2 = j
				break
		if index2 == 0:
			print(abs(index1 - s))
		else:
			print(min(abs(index1 - s), abs(index2 - s)))


from collections import Counter as C
for q in range(int(input())):
	(n, s, k) = map(int, input().split())
	close = C(list(map(int, input().split())))
	l = r = 0
	fr = False
	fl = False
	for i in range(s, n + 1):
		if close.get(i, -1) == -1:
			fr = True
			break
		r += 1
	for i in range(s, 0, -1):
		if close.get(i, -1) == -1:
			fl = True
			break
		l += 1
	if fr == False:
		r = float('inf')
	if fl == False:
		l = float('inf')
	print(min(l, r))


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	begg = s
	endd = s
	while 1:
		if begg != 0 and begg not in l:
			print(abs(s - begg))
			break
		elif endd <= n and endd not in l:
			print(abs(s - endd))
			break
		else:
			if begg != 0:
				begg -= 1
			endd += 1
			if endd > n and begg == 0:
				print(abs(s - begg))
				break


for t in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	for i in range(n):
		if s + i not in a and s + i <= n or (s - i not in a and s - i > 0):
			print(i)
			break


for _ in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	ka = [int(i) for i in input().split()]
	ct = 0
	fl = 0
	for i in range(0, k + 1):
		if s + i <= n and (not s + i in ka):
			print(i)
			break
		elif s - i >= 1 and (not s - i in ka):
			print(i)
			break


test = int(input())
for i in range(test):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	a = s
	count = 0
	ans1 = 0
	ans2 = 0
	while count < n:
		if a not in arr:
			ans2 = count
			break
		else:
			count += 1
			a -= 1
		if a == 0:
			ans2 = n + 1
			break
	a = s
	count = 0
	while count < n:
		if a not in arr:
			ans1 = count
			break
		else:
			count += 1
			a += 1
		if a == n + 1:
			ans1 = n + 1
			break
	print(min(ans1, ans2))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	arry = [int(a) for a in input().split()]
	if s not in arry:
		print(0)
	else:
		(a1, a2) = (n, n)
		for j in range(s + 1, n + 1):
			if j not in arry:
				a1 = abs(s - j)
				break
		k = s - 1
		while k >= 1:
			if k not in arry:
				a2 = s - k
				break
			k -= 1
		print(min(a1, a2))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	if s not in l:
		print(0)
	else:
		f = s
		b = s
		c1 = 0
		c2 = 0
		flag = 0
		while b > 0 or f <= n:
			if f <= n and f not in l:
				flag = 1
				break
			elif f <= n:
				c1 += 1
				f += 1
			if b > 0 and b not in l:
				flag = 2
				break
			elif b > 0:
				c2 += 1
				b = b - 1
		if flag == 1:
			print(c1)
		else:
			print(c2)


t = int(input())
while t > 0:
	(n, s, k) = map(int, input().split())
	ss = {}
	ss = [int(x) for x in input().split()]
	ans = 0
	while True:
		if s + ans <= n and s + ans not in ss:
			break
		if s - ans >= 1 and s - ans not in ss:
			break
		ans += 1
	print(ans)
	t -= 1


t = int(input())
ans = []
for i in range(t):
	(n, s, k) = input().split()
	n = int(n)
	s = int(s)
	k = int(k)
	cl = [int(i) for i in input().split()]
	if s not in cl:
		ans.append(0)
	else:
		for j in range(1, n):
			if s + j not in cl and s + j <= n:
				ans.append(j)
				break
			elif s - j not in cl and s - j >= 1:
				ans.append(j)
				break
for i in range(t):
	print(ans[i])


import sys
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	shift = 0
	while True:
		if s - shift >= 1 and s - shift not in a or (s + shift <= n and s + shift not in a):
			print(shift)
			break
		else:
			shift += 1


T = int(input())
for t in range(T):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	a.sort()
	if a.count(s) == 0:
		print(0)
	else:
		ind = a.index(s)
		ans = -1
		for i in range(1, ind + 1):
			if a[ind - i] != s - i:
				ans = i
				break
		for i in range(1, len(a) - ind):
			if a[ind + i] != s + i:
				if ans == -1:
					ans = i
				elif i < ans:
					ans = i
				break
		if ans != -1:
			if ind + 1 < ans and a[0] != 1:
				ans = ind + 1
			l = len(a)
			if l - ind < ans and a[l - 1] != n:
				ans = l - ind
		if ans == -1:
			if a[0] != 1:
				ans = ind + 1
			l = len(a)
			if ans == -1 or (a[l - 1] != n and l - ind < ans):
				ans = l - ind
		print(ans)


a = []
for www in range(int(input())):
	(n, s, k) = [int(s) for s in input().split()]
	arr = [int(s) for s in input().split()]
	for ans in range(10000):
		if (s - ans in arr) == False and s - ans > 0:
			a.append(ans)
			break
		if (s + ans in arr) == False and s + ans <= n:
			a.append(ans)
			break
for i in a:
	print(i)


from sys import stdin
t = int(stdin.readline().strip())
for i in range(t):
	(n, m, k) = map(int, stdin.readline().strip().split())
	s = set(map(int, stdin.readline().strip().split()))
	ans = 10000000
	lim = max(1, m - 1100)
	lim2 = min(n + 1, m + 1100)
	for j in range(lim, lim2):
		if j not in s:
			ans = min(ans, abs(j - m))
	print(ans)


te = int(input())
while te > 0:
	te = te - 1
	(n, s, q) = list(map(int, input().split()))
	closedFloors = list(map(int, input().split()))
	floors = {}
	for i in closedFloors:
		floors[i] = 0
	f = s
	b = s
	ans = 0
	while f > 0 and b <= n:
		if floors.get(f) is None or floors.get(b) is None:
			break
		ans += 1
		f -= 1
		b += 1
	if f <= 0:
		while b <= n:
			if floors.get(b) is None:
				break
			ans += 1
			b += 1
	if b > n:
		while f > 0:
			if floors.get(f) is None:
				break
			ans += 1
			f -= 1
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr_k = set(list(map(int, input().split())))
	mini = 99999999999999999999999
	for i in range(max(s - 1001, 1), min(s + 1001, n + 1)):
		if i not in arr_k:
			mini = min(mini, abs(s - i))
	print(mini)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	ans = float('+inf')
	for i in range(s, 0, -1):
		if i not in a:
			ans = min(s - i, ans)
			break
	for i in range(s, n + 1):
		if i not in a:
			ans = min(i - s, ans)
			break
	print(ans)


def bin(s, x):
	pp = 0
	l = len(x)
	g = 0
	if x[0] == s:
		return 'yes'
	else:
		while l != g:
			h = int((l + g) // 2)
			if s > x[h]:
				g = h + 1
			elif s < x[h]:
				l = h
			else:
				return 'yes'
				pp = 1
				break
		if pp == 0:
			return 'no'
r = []
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	x = list(map(int, input().split()))
	x.sort()
	w = 0
	while True:
		if s + w <= n:
			if bin(s + w, x) == 'no':
				r.append(w)
				break
			elif s - w > 0:
				if bin(s - w, x) == 'no':
					r.append(w)
					break
		elif s - w > 0:
			if bin(s - w, x) == 'no':
				r.append(w)
				break
		w += 1
for i in r:
	print(i)


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(n, s, k) = [int(x) for x in inp().split()]
	a = set([int(x) for x in inp().split()])
	minimum = 10 ** 9
	for i in range(1001):
		if s + i > n:
			break
		if s + i not in a:
			minimum = min(minimum, i)
	for i in range(1001):
		if s - i <= 0:
			break
		if s - i not in a:
			minimum = min(minimum, i)
	print(minimum)


def quickest_lunch(num_floor, cur_floor, floors_not_working):
	upper = float('inf')
	lower = float('inf')
	for i in range(cur_floor, num_floor + 1):
		if i not in floors_not_working:
			upper = i
			break
	for i in range(cur_floor, 0, -1):
		if i not in floors_not_working:
			lower = i
			break
	return min(abs(upper - cur_floor), abs(lower - cur_floor))

def read_line():
	num_test_case = int(input())
	ans = []
	for i in range(num_test_case):
		(num_floor, cur_floor, _) = input().split()
		floors_not_working = input().split()
		floors_not_working = [int(ch) for ch in floors_not_working]
		ans.append(quickest_lunch(int(num_floor), int(cur_floor), floors_not_working))
	for a in ans:
		print(a)
read_line()


t = int(input())
for _ in range(t):
	(n, s, k) = list(map(int, input().split()))
	c = set(map(int, input().split()))
	(l, u) = (s, s)
	for i in range(10000):
		if l not in c or u not in c:
			print(i)
			break
		if l > 1:
			l -= 1
		if u < n:
			u += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [1] + [(s - i < 1) + (s + i > n) for i in range(1, k)]
	for x in a:
		if abs(x - s) < k:
			c[abs(x - s)] += 1
	p = 0
	while p < k and c[p] == 2:
		p += 1
	print(p)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	s = s - 1
	for i in range(n):
		if s - i > -1:
			if not s - i + 1 in a:
				print(i)
				break
		if s + i < n:
			if not s + i + 1 in a:
				print(i)
				break


from sys import stdin, stdout
read = stdin.readline
write = stdout.write
print = lambda x: write(str(x) + '\n')

def readint():
	return int(read())

class Sol:

	def __init__(self, n, s, k, closed):
		self.n = n
		self.s = s
		self.k = k
		self.closed = closed

	@classmethod
	def input(cls):
		return cls(*map(int, read().split()), set(map(int, read().split())))

	def solve(self):
		for i in range(1024):
			up = self.s + i
			if up <= self.n and up not in self.closed:
				return i
			down = self.s - i
			if down >= 1 and down not in self.closed:
				return i
for _ in range(readint()):
	print(Sol.input().solve())


try:
	for _ in range(int(input())):
		(n, s, k) = map(int, input().split())
		closed = [int(i) for i in input().split()][:k]
		lower = s
		upper = s
		while lower > 0 or upper <= n:
			if lower not in closed and lower > 0:
				print(s - lower)
				break
			elif upper not in closed and upper <= n:
				print(upper - s)
				break
			else:
				lower -= 1
				upper += 1
except Exception as e:
	print(e)


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().split()))
	closed = set(map(int, input().split()))
	ans = 0
	while ans <= n:
		if s + ans not in closed and s + ans <= n:
			print(ans)
			break
		elif s - ans not in closed and 1 <= s - ans:
			print(ans)
			break
		else:
			ans += 1


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	lo = s - 1
	hig = n - s
	l1 = s - 1
	l2 = s + 1
	c1 = 0
	c2 = 0
	if s not in l:
		print(c1)
		continue
	for k in range(max(lo, hig)):
		if l1 >= 1:
			c1 = c1 + 1
			if l1 not in l:
				print(c1)
				break
			l1 = l1 - 1
		if l2 <= n:
			c2 = c2 + 1
			if l2 not in l:
				print(c2)
				break
			l2 = l2 + 1


T = int(input())
for _ in range(T):
	(n, s, k) = map(int, input().split())
	closed = [int(x) for x in input().split()]
	dico = {}
	for i in range(len(closed)):
		dico[closed[i]] = 1
		left = right = s
		while left in dico:
			left -= 1
		while right in dico:
			right += 1
	if left == 0:
		print(right - s)
	elif right == n + 1:
		print(s - left)
	else:
		print(min(right - s, s - left))


for _ in range(int(input())):
	(n, k, s) = map(int, input().split())
	t = list(map(int, input().split()))
	if k not in t:
		print(0)
	else:
		dist = 1
		while k + dist <= n or k - dist >= 1:
			if k + dist > n:
				if k - dist not in t:
					break
			elif k - dist <= 0:
				if k + dist not in t:
					break
			elif k + dist not in t or k - dist not in t:
				break
			dist += 1
		print(dist)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	q = set(map(int, input().split()))
	d = s
	u = s
	while d in q and u in q:
		d = max(d - 1, 1)
		u = min(u + 1, n)
	if d in q:
		print(u - s)
	else:
		print(s - d)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	sett = set(range(max(s - 1002, 1), min(s + 1002, n + 1)))
	for f in a:
		sett.discard(f)
	for i in range(1002):
		if s + i in sett or s - i in sett:
			print(i)
			break


import math

def main():
	(n, s, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	dif = 0
	max_dif = []
	for i in range(0, 1001):
		if s + i <= n and (not s + i in lst):
			print(i)
			return 0
		if s - i >= 1 and (not s - i in lst):
			print(i)
			return 0
t = int(input())
for i in range(t):
	main()


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def get_min_step(n, s, k, ak):
	center_idx = 1002
	ds = [2 for i in range(center_idx * 2 - 1)]
	for i in ak:
		if i - s + center_idx > center_idx * 2 - 2 or i - s + center_idx < 0:
			continue
		ds[i - s + center_idx] = 1
	if ds[center_idx] == 2:
		return 0
	for i in range(center_idx - 1):
		if (ds[center_idx - i] != 2 or s - i < 1) and (ds[center_idx + i] != 2 or s + i > n):
			continue
		return i
testCount = inp()
s_ = ''
for i in range(testCount):
	(n, s, k) = invr()
	ak = inlt()
	print(get_min_step(n, s, k, ak))


import math
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	dic = {}
	l = s - math.ceil(k / 2)
	r = s + math.ceil(k / 2)
	if l < 1:
		l = 1
	if r > n:
		r = n
	if s - l < r - s:
		r += k - (s - l)
	elif s - l > r - s:
		l -= k - (r - s)
	if l < 1:
		l = 1
	if r > n:
		r = n
	for i in range(l, r + 1):
		dic[i] = 0
	arr = list(map(int, input().split()))
	for i in arr:
		if l <= i and i <= r:
			dic[i] = 1
	i = s
	j = s
	while i >= l or j <= r:
		if i >= l and dic[i] == 0:
			print(s - i)
			break
		if j <= r and dic[j] == 0:
			print(j - s)
			break
		i -= 1
		j += 1


iters = int(input())
for i in range(iters):
	(n, s, k) = input().split()
	(n, s, k) = (int(n), int(s), int(k))
	ks = list(map(int, input().split()))
	for j in range(0, max(n - s, s) + 1):
		if s + j <= n and (not s + j in ks):
			print(j)
			break
		elif s - j > 0 and (not s - j in ks):
			print(j)
			break


t = int(input())
for T in range(t):
	(n, s, k) = map(int, input().split(' '))
	A = list(map(int, input().split(' ')))
	if s not in A:
		print(0)
		continue
	l = float('inf')
	for i in range(s - 1, 0, -1):
		if i not in A:
			l = s - i
			break
	h = float('inf')
	for i in range(s + 1, n + 1):
		if i not in A:
			h = i - s
			break
	print(min(l, h))


def floors(n, s, k):
	if s not in k:
		return 0
	i = 1
	while True:
		if s - i > 0:
			if s - i not in k:
				return i
		if s + i <= n:
			if s + i not in k:
				return i
		i += 1
t = int(input())
for i in range(t):
	(n, s, sizek) = map(int, input().split())
	k = list(map(int, input().split()))
	print(floors(n, s, k))


t = int(input())
while t > 0:
	t -= 1
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	m = h = n - 1
	for i in range(s, n + 1):
		if i not in l:
			m = i - s
			break
	for i in range(s, 0, -1):
		if i not in l:
			h = s - i
			break
	print(min(m, h))


T = int(input())
for t in range(T):
	(n, s, k) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(0, n):
		if s - i >= 1 and s - i not in c:
			print(i)
			break
		if s + i <= n and s + i not in c:
			print(i)
			break


n = int(input())
for i in range(n):
	(a, s, c) = map(int, input().split())
	l = []
	x = list(map(int, input().split()))
	for i in range(0, c + 1):
		if s - i >= 1 and (not s - i in x):
			print(i)
			break
		if s + i <= a and (not s + i in x):
			print(i)
			break


t = int(input())
for i in range(t):
	(n, s, k) = tuple([int(x) for x in input().split(' ')])
	a = {int(x) for x in input().split(' ')}
	x = 0
	for j in range(n + 1):
		x = s - j
		if 1 <= x <= n and x not in a:
			break
		x = s + j
		if 1 <= x <= n and x not in a:
			break
	print(abs(x - s))


import sys
input = sys.stdin.readline
import collections
from collections import defaultdict
n = int(input())
for i in range(n):
	(p, s, k) = map(int, input().split())
	closed = [int(i) for i in input().split()]
	closed.sort()
	min1 = 10000000
	min2 = 10000000
	for i in range(s, 0, -1):
		if i not in closed:
			min1 = i
			break
	for i in range(s + 1, p + 1):
		if i not in closed:
			min2 = i
			break
	print(min(abs(s - min1), abs(s - min2)))


t = int(input())
while t > 0:
	t -= 1
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	i = 0
	while 1:
		j = s + i
		k = s - i
		if j not in a and j <= n or (k not in a and k > 0):
			print(i)
			break
		else:
			i += 1


t = int(input())
for j in range(t):
	b = input()
	b = list(map(int, b.split()))
	n = b[0]
	s = b[1]
	g = b[2]
	a = input()
	k = list(map(int, a.split()))
	d = {}
	for i in k:
		d[i] = 1
	if s not in k:
		print(0)
	else:
		step = 1
		while True:
			if s - step > 0 and s - step not in d.keys():
				print(step)
				break
			if s + step <= n and s + step not in d.keys():
				print(step)
				break
			step += 1


def search(x, a):
	(L, R) = (0, len(a) - 1)
	while L <= R:
		M = (L + R) // 2
		if a[M] == x:
			return True
		elif a[M] < x:
			L = M + 1
		else:
			R = M - 1
	return False
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	d = 0
	while True:
		if not search(s - d, a) and s - d >= 1:
			print(d)
			break
		if not search(s + d, a) and s + d <= n:
			print(d)
			break
		d += 1


def solve(n, s, a_k):
	for i in range(0, n):
		c_l = s - i
		if c_l < 1:
			c_l = 1
		c_h = s + i
		if c_h > n:
			c_h = n
		if c_l not in a_k or c_h not in a_k:
			return i
	return -1
t = int(input())
for _ in range(t):
	(n, s, k) = [int(r) for r in input().split(' ')]
	a_k = {int(r) for r in input().split(' ')}
	print(solve(n, s, a_k))


t = int(input())
for p in range(t):
	a = []
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	f = 0
	while True:
		if s + f not in a and s + f <= n or (s - f not in a and s - f >= 1):
			print(f)
			break
		f = f + 1


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	i = 0
	while max(s - i, 1) in l and min(s + i, n) in l:
		i += 1
	print(i)


CASES = int(input())
for _ in range(CASES):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	for i in range(k + 1):
		a = s + i
		b = s - i
		if a not in closed and a <= n and (a >= 1):
			print(i)
			break
		if b not in closed and b <= n and (b >= 1):
			print(i)
			break


t = int(input())
for i in range(t):
	a = list(map(int, input().split(' ')))
	n = a[0]
	s = a[1]
	k = a[2]
	closed = list(map(int, input().split(' ')))
	closed.sort()
	floor_found = False
	x = s
	y = s + 1
	while floor_found == False:
		if x != 0:
			if x in closed and x >= 1:
				x -= 1
			else:
				floor_found = True
				print(s - x)
				break
		if y != n + 1:
			if y in closed and y <= n:
				y += 1
			else:
				floor_found = True
				print(y - s)
				break


t = int(input())
for l in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	i = s
	now = float('inf')
	while i > 0:
		if i not in a:
			now = min(now, s - i)
			break
		i -= 1
	i = s
	while i <= n:
		if i not in a:
			now = min(now, i - s)
			break
		i += 1
	print(now)


from collections import deque
import itertools
import operator
import queue
import time
import math
import sys
import os
t = int(input())
for j in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = pow(10, 12)
	for i in range(s, s + k + 1):
		if i <= n and i not in a:
			ans = min(i - s, ans)
	for i in range(s, s - k - 1, -1):
		if i > 0 and i not in a:
			ans = min(ans, s - i)
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = set(list(map(int, input().split())))
	ans = 0
	(prev, nextt) = (s, s)
	while True:
		if prev >= 1 and prev not in closed or (nextt <= n and nextt not in closed):
			print(ans)
			break
		ans += 1
		prev -= 1
		nextt += 1


from functools import lru_cache, reduce
from sys import setrecursionlimit
import math
t = int(input().strip())
for _ in range(t):
	data = list(map(int, input().strip().split()))
	closed = list(map(int, input().strip().split()))
	closed.sort()
	l = data[1]
	r = data[1]
	while True:
		if l >= 1 and l not in closed:
			print(abs(data[1] - l))
			break
		if r <= data[0] and r not in closed:
			print(abs(data[1] - r))
			break
		if l >= 1:
			l -= 1
		if r <= data[0]:
			r += 1


x = int(input())
for i in range(x):
	(n, s, k) = [int(x) for x in input().split(' ')]
	l = [int(x) for x in input().split(' ')]
	for j in range(k + 2):
		a = s + j
		b = s - j
		if a not in l and 0 < a <= n or (b not in l and 0 < b <= n):
			break
	print(j)


for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	mas = list(map(int, input().split()))
	c = 0
	while s + c in mas and s - c in mas:
		c += 1
	if s - c == 0:
		while s + c in mas:
			c += 1
	if s + c > n:
		while s - c in mas:
			c += 1
	print(c)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = 0
	s1 = s
	y = set(a)
	c = 0
	while t == 0:
		if s in y:
			s -= 1
			c += 1
		else:
			t = 1
	if s == 0:
		c = n
	s = s1
	c1 = 0
	t = 0
	while t == 0:
		if s in y:
			s += 1
			c1 += 1
		else:
			t = 1
	if s == n + 1:
		c1 = n
	print(min(c, c1))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 5)
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l = set(l)
	ans1 = inf
	ans2 = inf
	for i in range(0, 1003):
		z = s + i
		if z not in l and z <= n:
			ans1 = abs(s - z)
			break
	for i in range(0, 1003):
		z = s - i
		if z not in l and z >= 1:
			ans2 = abs(s - z)
			break
	print(min(ans1, ans2))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	lis = [x for x in map(int, input().split())]
	for i in range(n + 1):
		if s - i not in lis and (s - i > 0 and s - i < n + 1):
			print(abs(abs(s - i) - s))
			break
		elif s + i not in lis and (s + i > 0 and s + i < n + 1):
			print(abs(s + i) - s)
			break


from sys import stdin, stdout
t = int(stdin.readline())
for testcase in range(t):
	(n, s, k) = map(int, stdin.readline().split())
	closed = list(map(int, stdin.readline().split()))
	for i in range(n):
		if s + i not in closed and 1 <= s + i <= n or (s - i not in closed and 1 <= s - i <= n):
			stdout.write(str(i) + '\n')
			break


testes = int(input())
for t in range(testes):
	ent = input().split(' ')
	pisos = int(ent[0])
	conner = int(ent[1])
	fechados = int(ent[2])
	conjunto = set()
	ent = input().split(' ')
	closed = set()
	for k in range(fechados):
		closed.add(int(ent[k]))
	conjunto.add(conner)
	if closed.isdisjoint(conjunto):
		print(0)
	else:
		i = conner + 1
		j = conner - 1
		if i > pisos:
			i = pisos
		if j < 1:
			j = 1
		dist = 999999
		flag = False
		while flag == False:
			conjunto = set()
			conjunto2 = set()
			conjunto.add(i)
			conjunto2.add(j)
			if conjunto.isdisjoint(closed):
				dist = abs(i - conner)
				break
			elif conjunto2.isdisjoint(closed):
				dist = abs(j - conner)
				break
			if not i >= pisos:
				i += 1
			if not j <= 1:
				j -= 1
		print(dist)


T = int(input())
for cas in range(T):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	i = s
	j = s
	while True:
		if a.count(i) == 0 and i > 0:
			ans = s - i
			break
		if a.count(j) == 0 and j <= n:
			ans = j - s
			break
		i = i - 1
		j = j + 1
	print(ans)


t = int(input())
for _ in range(t):
	variables = [int(_) for _ in input().split()]
	n = variables[0]
	s = variables[1]
	k = variables[2]
	closed = [int(i) for i in input().split()]
	minimum = n
	tmp = n
	for x in range(s, 0, -1):
		if x not in closed:
			tmp = abs(s - x)
			if tmp < minimum:
				minimum = tmp
			break
	for x in range(s, n + 1):
		if x not in closed:
			tmp = abs(s - x)
			if tmp < minimum:
				minimum = tmp
			break
	print(minimum)


for i in range(int(input())):
	x = list(map(int, input().split()))
	n = x[0]
	s = x[1]
	k = x[2]
	A = list(map(int, input().split()))
	m = 10000000001
	c = 0
	c1 = 0
	c2 = 0
	i = s
	j = s
	while i > 0:
		if i not in A:
			break
		c1 += 1
		i -= 1
	while j <= n:
		if j not in A:
			break
		c2 += 1
		j += 1
	if i == 0:
		print(c2)
	elif j == n + 1:
		print(c1)
	else:
		print(min(c1, c2))


import sys

def main():
	tests = int(sys.stdin.readline()[:-1])
	for i in range(tests):
		(n, s, k) = map(int, sys.stdin.readline()[:-1].split())
		a = tuple(map(int, sys.stdin.readline()[:-1].split()))
		for j in range(n + 1):
			if 0 < s + j < n + 1 and (not s + j in a) or (0 < s - j < n + 1 and (not s - j in a)):
				print(j)
				break
main()


for _ in [0] * int(input()):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	is_close = {}
	for i in a:
		is_close[i - 1] = True
	for i in range(n):
		if s - 1 + i < n:
			if not is_close.get(s - 1 + i, False):
				print(i)
				break
		if s - 1 - i >= 0:
			if not is_close.get(s - 1 - i, False):
				print(i)
				break


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for j in range(k + 1):
		if s + j <= n and s + j not in l:
			print(j)
			break
		elif s - j > 0 and s - j not in l:
			print(j)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	ar = dict({x: 0 for x in map(int, input().split())})
	mi = n
	if ar.get(s, -1) == -1:
		print(0)
	else:
		for j in range(1, 1004):
			n1 = min(abs(s + j), n)
			n2 = max(s - j, 1)
			if ar.get(n1, -1) == -1:
				mi = j
				break
			if ar.get(n2, -1) == -1:
				mi = j
				break
		print(mi)


T = int(input())
for q in range(T):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	for i in range(k + 1):
		if max(s - i, 1) not in a or min(s + i, n) not in a:
			print(i)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = list(map(int, input().split()))
	items = list(map(int, input().split()))
	items = sorted(items)
	if s not in items:
		print(0)
	else:
		k = 1
		while s - k in items and s + k in items:
			k += 1
		if s - k < 1:
			while s + k in items:
				k += 1
		if s + k > n:
			while s - k in items:
				k += 1
		print(k)


from sys import stdin
reader = (s.rstrip() for s in stdin)

def mpint():
	return map(int, inp().split())
inp = reader.__next__
for case in range(int(inp())):
	(n, s, k) = mpint()
	close = set(mpint())
	not_close = [s]
	left = right = s
	while len(not_close) <= 1000 and (left >= 1 or right <= n):
		left -= 1
		right += 1
		if left >= 1:
			not_close.append(left)
		if right <= n:
			not_close.append(right)
	for nc in not_close:
		if nc not in close:
			print(abs(nc - s))
			break


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	karr = list(map(int, input().split()))
	for i in range(k + 1):
		if s - i >= 1 and (not s - i in karr):
			print(i)
			break
		if s + i <= n and (not s + i in karr):
			print(i)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(k):
		c[i] = abs(c[i] - s)
	c.sort()
	j = 0
	num = 0
	while True:
		if 0 < s - num <= s and s < s + num <= n:
			if j < k and c[j] == num:
				if j + 1 < k and c[j + 1] == num:
					j += 2
					num += 1
				else:
					break
			else:
				break
		elif j < k and c[j] == num:
			j += 1
			num += 1
		else:
			break
	print(num)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	ngs = set(map(int, input().split()))
	ans = 10 ** 9
	for i in range(max(1, s - k - 1), min(n + 1, s + k + 2)):
		if i not in ngs and abs(i - s) < ans:
			ans = abs(i - s)
	print(ans)


import bisect
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	close = list(input().split())
	close = [int(x) for x in close]
	close = sorted(close)
	index = bisect.bisect_left(close, s)
	for i in range(1000000000):
		if s + i <= n and s + i not in close:
			print(i)
			break
		if s - i >= 1 and s - i not in close:
			print(i)
			break


t = int(input())
while t > 0:
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	d = {}
	for i in arr:
		d[i] = 1
	c = 0
	while 1:
		if s + c <= n and d.get(s + c, 0) == 0:
			break
		if s - c > 0 and d.get(s - c, 0) == 0:
			break
		c += 1
	print(c)
	t -= 1


for _ in ' ' * int(input()):
	(a, b, c) = map(int, input().split())
	d = list(map(int, input().split()))
	k = a
	for i in range(b, a + 1):
		if i not in d:
			k = i - b
			break
	for i in range(b - 1, 0, -1):
		if i not in d:
			k = min(k, b - i)
			break
	print(k)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	for i in range(k + 1):
		if max(s - i, 1) not in a or min(s + i, n) not in a:
			print(i)
			break


for _ in [0] * int(input()):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	for i in range(k + 2):
		if s - i > 0 and s - i not in closed:
			break
		if s + i <= n and s + i not in closed:
			break
	print(i)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	up = s
	down = s
	k = set(a)
	while True:
		if up not in k and up > 0:
			print(s - up)
			break
		elif down not in k and down <= n:
			print(down - s)
			break
		up = up - 1
		down = down + 1


t = int(input())
for _ in range(t):
	(n, s, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a.sort()
	f = s
	for i in a:
		if i == f:
			f = f + 1
	f1 = s
	a.sort(reverse=True)
	for i in a:
		if i > s:
			continue
		if f1 == i:
			f1 -= 1
	if f1 != 0 and f <= n:
		f = min(f - s, s - f1)
	elif f1 == 0:
		f = f - s
	else:
		f = s - f1
	print(f)


tests = int(input())
for te in range(tests):
	(n, s, k) = list(map(int, input().split()))
	lista = list(map(int, input().split()))
	lista.sort()
	left_index = -1
	right_index = -1
	dica = {}
	ans = 100000
	for i in range(k):
		dica[lista[i]] = 1
	for i in range(s, s + 1001):
		if i not in dica and i <= n:
			ans = min(ans, i - s)
			break
	for i in range(s, s - 1001, -1):
		if i not in dica and i > 0:
			ans = min(ans, s - i)
			break
	print(ans)


t = int(input())
for o in range(t):
	(n, s, k) = list(map(int, input().split()))
	close = list(map(int, input().split()))
	u = s
	l = s
	for i in range(n):
		if u not in close and 1 <= u <= n:
			print(abs(u - s))
			break
		elif l not in close and 1 <= l <= n:
			print(abs(l - s))
			break
		else:
			u += 1
			l -= 1


t = int(input())

def solve():
	(n, s, k) = map(int, input().split())
	closed = set(list(map(int, input().split())))
	i = 0
	for i in range(k + 1):
		if s - i >= 1 and s - i not in closed:
			print(i)
			break
		if s + i <= n and s + i not in closed:
			print(i)
			break
	return
for _ in range(t):
	solve()


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	p = 0
	q = 0
	for i in range(0, n + 1):
		if s + i <= n and (not s + i in a):
			p = i
			break
		if s - i > 0 and (not s - i in a):
			q = i
			break
	if p == 0:
		print(q)
	elif q == 0:
		print(p)
	elif p <= q and p != 0:
		print(p)
	elif q < p and q != 0:
		print(q)


from collections import Counter
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = dict(Counter(a))
	m = 0
	if a.get(s):
		i = s
		j = 1
		while j < min(1001, n):
			if i - j > 0 and a.get(i - j) == None:
				m = j
				break
			if i + j <= n and a.get(i + j) == None:
				m = j
				break
			j += 1
		print(m)
	else:
		print(0)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	i = 0
	f = 0
	while s - i > 0 or s + i <= n:
		if s - i > 0 and s - i not in a:
			print(i)
			f = 1
			break
		if s + i <= n and s + i not in a:
			print(i)
			f = 1
			break
		i += 1
	if not f:
		print(0)


def solve(n, s, closed):
	min_moves = float('inf')
	floor = s
	time = 0
	while floor > 0:
		if floor not in closed:
			min_moves = min(min_moves, time)
			break
		floor -= 1
		time += 1
	floor = s
	time = 0
	while floor <= n:
		if floor not in closed:
			min_moves = min(min_moves, time)
			break
		floor += 1
		time += 1
	print(min_moves)

def main():
	t = int(input())
	for i in range(t):
		(n, s, k) = map(int, input().split())
		closed = set(map(int, input().split()))
		solve(n, s, closed)
main()


from collections import Counter

def dist(n, s, closed):
	if s not in closed:
		return 0
	(neg, pos) = ([0], [0])
	for a in closed:
		if a - s < 0:
			neg.append(s - a)
		else:
			pos.append(a - s)
	neg = sorted(neg)
	pos = sorted(pos)
	for i in range(len(neg) - 1):
		if neg[i + 1] - neg[i] > 1:
			min_neg = neg[i]
			break
	else:
		min_neg = neg[-1]
	for i in range(len(pos) - 1):
		if pos[i + 1] - pos[i] > 1:
			min_pos = pos[i]
			break
	else:
		min_pos = pos[-1]
	if s - min_neg - 1 == 0:
		min_neg = min_pos
	if min_pos + s == n:
		min_pos = min_neg
	return min(min_neg, min_pos) + 1
t = int(input())
data = []
closed = []
for i in range(t):
	data.append(tuple((int(x) for x in input().split())))
	closed.append(set((int(x) for x in input().split())))
for i in range(t):
	print(dist(data[i][0], data[i][1], closed[i]))


t = int(input())

def binarySerarch(a, x):
	start = 0
	end = len(a) - 1
	while start <= end:
		mid = (start + end) // 2
		if a[mid] == x:
			return True
		elif a[mid] > x:
			end = mid - 1
		else:
			start = mid + 1
	return False
for q in range(t):
	(n, s, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a.sort()
	if binarySerarch(a, s) == 0:
		print(0)
		continue
	bol = False
	ans1 = 0
	ans2 = 0
	for i in range(1, n):
		if s + i > n:
			break
		if binarySerarch(a, s + i) == 0:
			ans1 = i
			break
	for i in range(1, n):
		if s - i < 1:
			break
		if binarySerarch(a, s - i) == 0:
			ans2 = i
			break
	if ans1 == 0 and ans2 != 0:
		print(ans2)
	elif ans2 == 0 and ans1 != 0:
		print(ans1)
	else:
		print(min(ans1, ans2))


import math
for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	arr = sorted(list(map(int, input().split())))
	ss = math.inf
	sss = math.inf
	for a in range(s, 0, -1):
		if a not in arr:
			ss = s - a
			break
	for aa in range(s, n + 1):
		if aa not in arr:
			sss = aa - s
			break
	print(min(ss, sss))


t = int(input())
for i in range(t):
	(n, s, k) = (int(i) for i in input().split())
	A = [int(i) for i in input().split()]
	p = 0
	while True:
		if s - p not in A and s - p > 0:
			print(p)
			break
		elif s + p not in A and s + p <= n:
			print(p)
			break
		else:
			p += 1


def main():
	t = int(input())
	for i in range(t):
		st = input()
		st = st.strip().split(' ')
		n = int(st[0])
		s = int(st[1])
		k = int(st[2])
		l = []
		st = input()
		st = st.strip().split(' ')
		for j in range(k):
			num = int(st[j])
			l.append(num)
		flag = False
		for j in range(k):
			if s - j > 0 and s - j not in l or (s + j <= n and s + j not in l):
				flag = True
				print(j)
				break
		if flag == False:
			print(k)
main()


import math
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	start = 0
	ind = 0
	end = k - 1
	if s not in arr:
		print('0')
	else:
		arr.sort()
		while start < end:
			if s == arr[start]:
				ind = start
				break
			elif s == arr[end]:
				ind = end
				break
			if s > arr[(start + end) // 2]:
				start = (start + end) // 2 + 1
			elif s < arr[(start + end) // 2]:
				end = (start + end) // 2 - 1
			else:
				ind = (start + end) // 2
				break
		j = ind - 1
		val = s - 1
		small = arr[0] - 1
		large = arr[k - 1] + 1
		while j >= 0:
			if arr[j] != val:
				small = val
				break
			else:
				j -= 1
				val -= 1
		j = ind + 1
		val = s + 1
		while j < k:
			if arr[j] != val:
				large = val
				break
			else:
				j += 1
				val += 1
		if small <= 0:
			print(large - s)
		elif large > n:
			print(s - small)
		elif s - small <= large - s:
			print(s - small)
		else:
			print(large - s)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		(n, s, k) = LI()
		a = LI()
		b = set(a)
		r = n
		for i in range(n):
			c = s + i
			if c <= n and c not in b:
				r = i
				break
			c = s - i
			if c > 0 and c not in b:
				r = i
				break
		rr.append(r)
	return JA(rr, '\n')
print(main())


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [*map(int, input().split())]
	miniup = 10 ** 9
	minidown = 10 ** 9
	if s not in a:
		print(0)
	else:
		for i in range(1, n):
			diff = s - i
			if diff <= 0:
				break
			elif diff not in a:
				miniup = i
				break
		for i in range(1, n):
			diff = s + i
			if diff >= n + 1:
				break
			elif diff not in a:
				minidown = i
				break
		if miniup > minidown:
			print(minidown)
		else:
			print(miniup)


from math import inf
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	closed = {int(x) for x in input().split()}
	under = inf
	for si in range(s, 0, -1):
		if si not in closed:
			under = s - si
			break
	over = inf
	for si in range(s, n + 1):
		if si not in closed:
			over = si - s
			break
	print(min(under, over))


def main():
	T = int(input())
	for t in range(T):
		line = input().split()
		(n, s, k) = (int(line[0]), int(line[1]), int(line[2]))
		dic = set()
		line = input().split()
		for x in line:
			dic.add(int(x))
		i = 0
		while True:
			if 0 < s - i <= n and s - i not in dic or (0 <= s + i <= n and s + i not in dic):
				print(i)
				break
			i += 1
main()


def main():
	import sys
	input = sys.stdin.readline
	t = int(input())
	for _ in range(t):
		(n, s, k) = map(int, input().split())
		A = set(list(map(int, input().split())))
		ans_plus = ans_minus = 10 ** 9
		for i in range(k + 1):
			if s + i > n:
				break
			if s + i not in A:
				ans_plus = i
				break
		for i in range(k + 1):
			if s - i <= 0:
				break
			if s - i not in A:
				ans_minus = i
				break
		print(min(ans_plus, ans_minus))
main()


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().split(' ')))
	closed = set(map(int, input().split(' ')))
	if s not in closed:
		print(0)
		continue
	closestUp = closestDown = s
	while closestUp in closed and closestUp < n + 1:
		closestUp += 1
	while closestDown in closed and closestDown > 0:
		closestDown -= 1
	if closestDown == 0:
		print(closestUp - s)
	elif closestUp == n + 1:
		print(s - closestDown)
	else:
		print(min(s - closestDown, closestUp - s))


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	lis = [int(x) for x in input().split()]
	step = 0
	for j in range(0, n + 1):
		s1 = s - j
		s2 = s + j
		if s1 not in lis and s1 > 0 or (s2 not in lis and s2 <= n):
			break
		step += 1
	print(step)


T = int(input())
for _ in range(T):
	(n, s, k) = map(int, input().split())
	close = set(map(int, input().split()))
	l_near = -1000000
	r_near = n + 100000000000
	s_clone = s
	while s_clone > 0:
		if s_clone not in close:
			l_near = s_clone
			break
		s_clone -= 1
	s_clone = s
	while s_clone <= n:
		if s_clone not in close:
			r_near = s_clone
			break
		s_clone += 1
	print(min(abs(r_near - s), abs(l_near - s)))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	(n, s, k) = inp()
	arr = list(inp())
	dic = dd(bool)
	for i in arr:
		dic[i] = True
	if not dic[s]:
		ans = 0
	else:
		ans = float('inf')
		val1 = 0
		flag1 = False
		for i in range(s - 1, max(s - 1001, 0), -1):
			if dic[i]:
				val1 += 1
			else:
				val1 += 1
				flag1 = True
				break
		if flag1:
			ans = min(ans, val1)
		val2 = 0
		flag2 = False
		for i in range(s + 1, min(s + 1001, n + 1)):
			if dic[i]:
				val2 += 1
			else:
				val2 += 1
				flag2 = True
				break
		if flag2:
			ans = min(ans, val2)
	out(ans)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	min_i = float('INF')
	for i in range(s, min(n + 1, s + 1001)):
		if i not in a:
			min_i = min(min_i, abs(s - i))
	for i in range(s, max(0, s - 1001), -1):
		if i not in a:
			min_i = min(min_i, abs(s - i))
	print(min_i)


t = int(input())
while t > 0:
	(total_floors, conner_floor, num_closed_rest) = map(int, input().strip().split())
	closed_rests = list(map(int, input().split()))
	closed_rests = dict.fromkeys(list(sorted(closed_rests)))
	left_closest_rest = [None] * len(closed_rests)
	right_closest_rest = [None] * len(closed_rests)
	for (idx, each_closed_store) in enumerate(closed_rests.keys()):
		if each_closed_store - 1 in closed_rests:
			left_nearest_open_store = left_closest_rest[idx - 1]
		elif each_closed_store - 1 >= 1:
			left_nearest_open_store = each_closed_store - 1
		else:
			left_nearest_open_store = None
		left_closest_rest[idx] = left_nearest_open_store
	for (idx, each_closed_store) in reversed(list(enumerate(closed_rests.keys()))):
		if each_closed_store + 1 in closed_rests:
			right_nearest_open_store = right_closest_rest[idx + 1]
		elif each_closed_store + 1 <= total_floors:
			right_nearest_open_store = each_closed_store + 1
		else:
			right_nearest_open_store = None
		right_closest_rest[idx] = right_nearest_open_store
	if conner_floor in closed_rests:
		idx = list(closed_rests.keys()).index(conner_floor)
		left_closest_store = left_closest_rest[idx]
		right_closest_store = right_closest_rest[idx]
		if left_closest_store is not None and right_closest_store is not None:
			dist_from_left_closest = conner_floor - left_closest_store
			dist_from_right_closest = right_closest_store - conner_floor
			print(min(dist_from_left_closest, dist_from_right_closest))
		elif left_closest_store is None:
			print(right_closest_store - conner_floor)
		else:
			print(conner_floor - left_closest_store)
	else:
		print(0)
	t -= 1


def solver(arr1, arr2):
	(n, s) = arr1[:2]
	if s not in arr2:
		return 0
	else:
		a = b = s
		arr2.sort()
		for i in range(s + 1, n + 1):
			if i not in arr2:
				a = i
				break
		for j in range(s, 0, -1):
			if j not in arr2:
				b = j
				break
		if a - s > s - b:
			if b == s:
				return a - s
			else:
				return s - b
		elif a == s:
			return s - b
		else:
			return a - s
i = int(input())
for j in range(0, i):
	arr1 = [int(num) for num in input().split()]
	arr2 = [int(num) for num in input().split()]
	print(solver(arr1, arr2))


T = int(input())
for ti in range(T):
	(n, s, k) = input().split()
	(n, s, k) = (int(n), int(s), int(k))
	ans = 0
	closed = input().split()
	closed = [int(k) for k in closed]
	while True:
		if s - ans > 0 and s - ans not in closed:
			break
		if s + ans <= n and s + ans not in closed:
			break
		ans += 1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	f = 0
	l.sort()
	try:
		ind = l.index(s)
	except:
		print(0)
		f = 1
	if f == 0:
		ansmi = 0
		ansmx = 0
		for i in range(ind - 1, -1, -1):
			if l[i] != l[i + 1] - 1:
				ansmi = abs(s - l[i + 1]) + 1
				break
		for i in range(ind, k - 1):
			if l[i + 1] != l[i] + 1:
				ansmx = abs(s - l[i]) + 1
				break
		if ansmx == 0 and l[k - 1] != n:
			ansmx = abs(s - l[k - 1]) + 1
		if ansmi == 0 and l[0] != 1:
			ansmi = abs(s - l[0]) + 1
		if ansmx == 0:
			print(ansmi)
		elif ansmi == 0:
			print(ansmx)
		else:
			print(min(ansmi, ansmx))


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, S, K, A):
	A = set(A)
	if S not in A:
		return 0
	numGreater = 0
	while S + numGreater in A:
		numGreater += 1
		if S + numGreater > N:
			numGreater = float('inf')
	numLess = 0
	while S - numLess in A:
		numLess += 1
		if S - numLess <= 0:
			numLess = float('inf')
	return min(numGreater, numLess)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N, S, K) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, S, K, A)
	print(ans)


for i in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	l = set([int(i) for i in input().split()])
	i = 0
	res = 0
	while 1:
		if s + i <= n and s + i not in l:
			break
		if s - i >= 1 and s - i not in l:
			break
		i += 1
		res += 1
	print(res)


T = int(input())
for test_no in range(T):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in a):
			print(i)
			break
		if s + i <= n and (not s + i in a):
			print(i)
			break
	else:
		assert False


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, s, k) = value()
	a = [0] + array() + [n + 1]
	a.sort()
	ans = inf
	if s not in a:
		ans = 0
	for i in range(1, k + 2):
		if a[i] == a[i - 1] + 1:
			continue
		ans = min(ans, abs(s - a[i - 1] - 1), abs(s - a[i] + 1))
	print(ans)


q = int(input())
for qq in range(q):
	(n, s, k) = map(int, input().split())
	dat = list(map(int, input().split()))
	res = 0
	while True:
		if s + res not in dat and s + res <= n:
			break
		if s - res not in dat and s - res >= 1:
			break
		res += 1
	print(res)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = set(l)
	flag = 0
	for j in range(1001):
		if s - j not in d and s - j > 0:
			print(j)
			flag = 1
			break
		elif s + j not in d and s + j <= n:
			print(j)
			flag = 1
			break
	if s == 0 and flag == 0:
		print(s + 1000)
	elif flag == 0:
		print(s - 1000)


import sys
q = int(input())
result = []
for _ in range(q):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, next(sys.stdin).split(' ')))
	up = 0
	down = 0
	for i in range(s, n + 1):
		if i not in closed:
			break
		up += 1
	for i in range(s, 0, -1):
		if i not in closed:
			break
		down += 1
	if s + up <= n and up < down or s - down < 1:
		result.append(str(up))
	else:
		result.append(str(down))
print('\n'.join(result))


t = int(input())
while t > 0:
	(n, s, nk) = map(int, input().split())
	k = list(map(int, input().split()))
	d = {}
	for i in k:
		if i not in d:
			d[i] = 1
	p1 = s
	p2 = s
	while p1 > 0 or p2 <= n:
		if p1 > 0 and p1 not in d:
			print(s - p1)
			break
		elif p2 < n + 1 and p2 not in d:
			print(p2 - s)
			break
		else:
			p1 -= 1
			p2 += 1
	t -= 1


for i in range(int(input())):
	(a, b, c) = list(map(int, input().split()))
	d = [int(x) for x in input().split()]
	for i in range(c + 1):
		if b - i >= 1 and (not b - i in d):
			print(i)
			break
		if b + i <= a and (not b + i in d):
			print(i)
			break


num_t = int(input())
for _ in range(num_t):
	(n, s, k) = map(int, input().strip().split())
	ks = [int(i) for i in input().split()]
	assert len(ks) == k
	ks = set(ks)
	for i in range(0, 10000):
		(l, h) = (s - i, s + i)
		if l > 0 and (not l in ks):
			print(i)
			break
		if h < n + 1 and (not h in ks):
			print(i)
			break


t = int(input())
MAXX = 99999999999999
for i in range(t):
	(n, s, k) = list(map(int, input().split(' ')))
	closed = sorted(list(map(int, input().split(' '))))
	best = 0 if s not in closed else MAXX
	for i in range(k):
		el = closed[i]
		l_n = el - 1 if (i == 0 or closed[i - 1] + 1 != el) and el - 1 > 0 else MAXX
		l_r = el + 1 if (i == k - 1 or closed[i + 1] - 1 != el) and el + 1 <= n else MAXX
		if abs(l_n - s) < best:
			best = abs(l_n - s)
		if abs(l_r - s) < best:
			best = abs(l_r - s)
	print(best)


def main():
	(n, s, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	minimum = 1000000000
	minimum_ = minimum
	T = False
	while not T:
		for i in range(s - 1000, s + 1001):
			if i > 0 and i <= s:
				if i in l:
					pass
				else:
					minimum = min(minimum, s - i)
			elif i > 0 and i <= n:
				if i in l:
					pass
				else:
					minimum = min(minimum, i - s)
		T = True
	print(minimum)
a = int(input())
for i in range(a):
	main()


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 1000000002
	for i in range(max(1, s - 1001), min(n + 1, s + 1001)):
		if i not in l:
			ans = min(ans, abs(s - i))
	print(ans)


INF = 10 ** 10

def main():
	(n, s, k) = get_list()
	li = set(get_list())
	i = 0
	while i < n:
		if s + i <= n and s + i not in li:
			print(i)
			break
		elif s - i > 0 and s - i not in li:
			print(i)
			break
		i += 1
import sys
from collections import defaultdict, Counter
from bisect import bisect_left, bisect_right
import math
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]
print(*out, sep='\n')


def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + int((r - l) / 2)
		if arr[mid] == x:
			return True
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return False
n = int(input())
for i in range(n):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	c = s
	g1 = 0
	g2 = 0
	c1 = s
	while binarySearch(l, 0, len(l) - 1, c):
		g1 = g1 + 1
		c = c - 1
		if c < 1:
			g1 = 1000000000
			break
	while binarySearch(l, 0, len(l) - 1, c1):
		g2 = g2 + 1
		c1 = c1 + 1
		if c1 > n:
			g2 = 1000000000
			break
	if g1 < g2:
		print(g1)
	else:
		print(g2)


for _ in [0] * int(input()):
	(n, s, k) = map(int, input().split())
	l = set(map(int, input().split()))
	floors = 0
	while max(s - floors, 1) in l and min(s + floors, n) in l:
		floors += 1
	print(floors)


for t in range(int(input())):
	(n, p, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c1 = p
	c2 = p
	while 1:
		if c1 not in a:
			p = c1 - p
			break
		elif c1 < n:
			c1 += 1
		if c2 not in a:
			p = p - c2
			break
		elif c2 > 1:
			c2 -= 1
	print(p)


t = int(input())
for kek in range(t):
	(n, s, k) = map(int, input().split())
	a = set(list(map(int, input().split())))
	ans = 1001
	for i in range(max(s - k - 2, 1), min(s + k + 2, n + 1)):
		if i not in a:
			if abs(s - i) < ans:
				ans = abs(s - i)
	print(ans)


t = int(input())
for _ in range(t):
	(n, s, k) = [int(i) for i in input().split(' ')]
	ks = [int(i) for i in input().split(' ')]
	i = j = s
	res = []
	while i > 0:
		if i not in ks:
			res.append(s - i)
			break
		else:
			i -= 1
	while j <= n:
		if j not in ks:
			res.append(j - s)
			break
		else:
			j += 1
	print(min(res))


from collections import defaultdict
for _ in range(int(input())):
	(n, k, s) = map(int, input().split())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	for i in a:
		d[i] = 1
	if d[k] == 0:
		print(0)
	else:
		f = 0
		for i in range(1, k):
			if k - i > 0:
				if d[k - i] == 0:
					f = 1
					print(i)
					break
			if k + i <= n:
				if d[k + i] == 0:
					f = 1
					print(i)
					break
		if f == 0:
			for i in range(2 * k, n + 1):
				if d[i] == 0:
					print(i - k)
					break


import sys, os
from math import *
from random import *

class Answer(BaseException):

	def __init__(self, txt):
		self.__text = txt

	def what(self):
		return self.__text

def answer(arg):
	raise Answer

def gcd(a, b: int):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b: int):
	return a * b // gcd(a, b)

def readArr(__type):
	return list(map(__type, input().split()))

def chomp(s: str):
	return s[:-1] if s[-1] == '\n' else s

def readFile(name: str):
	f = open(name, 'r')
	r = f.readlines()
	f.close()
	return r

def init_all():
	sys.setrecursionlimit(max(sys.getrecursionlimit(), 500000))

def lower_bound(a, k):
	(l, r) = (0, len(a) - 1)
	while l != r:
		mid = (l + r) // 2
		if a[mid] < k:
			l = mid + 1
		else:
			r = mid
	return l

def upper_bound(a, k):
	(l, r) = (0, len(a) - 1)
	while l + 1 < r:
		mid = (l + r) // 2
		if a[mid] <= k:
			l = mid
		else:
			r = mid - 1
	if a[r] == k:
		return r
	else:
		return l

def solve():
	(n, s, k) = readArr(int)
	a = set(readArr(int))
	ans = 2 ** 60
	for i in range(s, 0, -1):
		if not i in a:
			ans = min(ans, abs(i - s))
			break
	for i in range(s, n + 1):
		if not i in a:
			ans = min(ans, abs(i - s))
			break
	print(ans)

def main(argv: list):
	init_all()
	t = 1
	t = int(input())
	for __ in range(t):
		solve()
	return 0
try:
	exit(main(sys.argv))
except Answer as a:
	print(a.what())
	exit(0)
except Exception as e:
	print('An exception occured:', *e.args)
	exit(-1)


import math, collections, sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	delta = 0
	while True:
		if s - delta >= 1 and s - delta not in a:
			print(delta)
			break
		elif s + delta <= n and s + delta not in a:
			print(delta)
			break
		delta += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(m1, m2) = (1001, 1001)
	if s not in a:
		print(0)
		continue
	for i in range(s - 1, 0, -1):
		if i not in a:
			m1 = s - i
			break
	for i in range(s + 1, n + 1):
		if i not in a:
			m2 = i - s
			break
	print(min(m1, m2))


def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	close = list(map(int, input().split()))
	close.sort()
	res = binarySearch(close, 0, len(close) - 1, s)
	if res == -1:
		print(0)
	else:
		i = res - 1
		j = res + 1
		temp_i = temp_j = close[res]
		count_i = count_j = 1
		flag_i = flag_j = False
		while True:
			if i == -1 and j == k:
				break
			if i != -1:
				if close[i] != temp_i - 1:
					flag_i = True
					break
				else:
					count_i += 1
					temp_i = close[i]
					i -= 1
			if j < k:
				if close[j] != temp_j + 1:
					flag_j = True
					break
				else:
					count_j += 1
					temp_j = close[j]
					j += 1
		if flag_i and flag_j:
			if count_i < count_j:
				print(count_i)
			else:
				print(count_j)
		elif flag_i:
			if close[-1] != n:
				if k - res < count_i:
					print(k - res)
				else:
					print(count_i)
			else:
				print(count_i)
		elif flag_j:
			if close[0] != 1:
				if res + 1 < count_j:
					print(res + 1)
				else:
					print(count_j)
			else:
				print(count_j)
		elif close[0] != 1 and close[-1] != n:
			print(min(res + 1, k - res))
		elif close[0] != 1:
			print(res + 1)
		else:
			print(count_j)


from math import inf

def solve(n, s, k, ks):
	d = inf
	for i in range(s - 1002, s + 1002):
		if i < 0 or i + 1 > n:
			continue
		if i + 1 not in ks:
			d = min(d, abs(s - (i + 1)))
	return d

def main():
	for _ in range(int(input())):
		(n, s, k) = list(map(int, input().split()))
		ks = set(map(int, input().split()))
		print(solve(n, s, k, ks))
main()


from bisect import bisect_left
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(n + 1):
		if s - i >= 1 and s - i not in a:
			print(i)
			break
		elif s + i <= n and s + i not in a:
			print(i)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	x = set()
	for i in arr:
		x.add(i)
	if s not in x:
		print(0)
	else:
		(v1, v2) = (-1, -1)
		for i in range(s - 1, 0, -1):
			if i not in x:
				v1 = i
				break
		for i in range(s + 1, n + 1):
			if i not in x:
				v2 = i
				break
		if v1 == -1:
			print(abs(s - v2))
		elif v2 == -1:
			print(abs(s - v1))
		else:
			print(min(abs(s - v1), abs(s - v2)))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	next_s = n + 1
	prev_s = 0
	offs = set(list(map(int, input().split())))
	ans = 0
	if s in offs:
		for j in range(1, 1002):
			if s - j >= 1 and s - j not in offs:
				ans = j
				break
			if s + j <= n and s + j not in offs:
				ans = j
				break
	print(ans)


from sys import stdin, stdout
t = int(stdin.readline())
for i in range(t):
	(n, s, k) = map(int, stdin.readline().split())
	a = [int(ai) for ai in stdin.readline().split()]
	for j in range(n + 1):
		if s + j not in a and s + j <= n or (s - j not in a and s - j > 0):
			stdout.write('{}\n'.format(j))
			break


def find_ans(n, s, k, arr):
	if s not in arr:
		return 0
	up = 10 ** 9
	down = 10 ** 9
	for i in range(s, n + 1):
		if i not in arr:
			up = i - s
			break
	for i in range(s, 0, -1):
		if i not in arr:
			down = s - i
			break
	return min(up, down)
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(find_ans(n, s, k, arr))


import collections
local = False
if local:
	file = open('A.txt', 'r')

def readInput():
	if local:
		return file.readline()
	else:
		return input()

def stairs(n, s, k, closed):
	sclosed = set(closed)
	minup = n + 1
	mindown = n + 1
	for i in range(s, n + 1):
		if i not in sclosed:
			minup = i - s
			break
	for i in range(s, 0, -1):
		if i not in sclosed:
			mindown = s - i
			break
	return min(minup, mindown)
t = int(readInput())
for i in range(t):
	nsk = readInput().split()
	(n, s, k) = (int(nsk[0]), int(nsk[1]), int(nsk[2]))
	closed = [int(x) for x in readInput().split()]
	print(stairs(n, s, k, closed))


for x in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	if s not in l:
		print(0)
	else:
		for y in range(1, n):
			if s - y > 0 and s - y not in l or (s + y <= n and s + y not in l):
				print(y)
				break


import math
t = int(input())
while t > 0:
	(n, s, k) = map(int, input().split())
	nu = list(map(int, input().split()))
	t -= 1
	result = math.inf
	for i in range(max(1, s - 1000 - 1), min(s + 1000 + 1, n + 1)):
		if i not in nu:
			result = min(result, abs(i - s))
	print(result)


for _ in range(int(input())):
	(X, Closed) = (list(map(int, input().split())), list(map(int, input().split())))
	for i in range(X[2] + 1):
		if X[1] + i in range(1, X[0] + 1) and X[1] + i not in Closed or (X[1] - i in range(1, X[0] + 1) and X[1] - i not in Closed):
			print(i)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for i in range(max(s, n - s + 1)):
		if s - i > 0 and s - i not in arr:
			print(i)
			break
		elif s + i < n + 1 and s + i not in arr:
			print(i)
			break


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	ans = 0
	while max(s - ans, 1) in closed and min(s + ans, n) in closed:
		ans += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	f = list(map(int, input().split()))
	L = []
	c = 0
	c3 = 0
	for i in range(s, n + 1):
		if i in f:
			c += 1
		elif i not in f:
			c3 += 1
			break
	c1 = 0
	c2 = 0
	j = s
	while j > 0:
		if j in f:
			c1 += 1
		elif j not in f:
			c2 += 1
			break
		j -= 1
	if c2 == 1 and c3 == 1:
		print(min(c, c1))
	elif c2 == 1 and c3 == 0:
		print(c1)
	elif c2 == 0 and c3 == 1:
		print(c)


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	d = {}
	for i in input().split():
		if i not in d:
			d[int(i)] = 1
	if s not in d:
		print(0)
		continue
	i = j = s
	while 1:
		if i > 0 and i not in d:
			break
		if j < n + 1 and j not in d:
			break
		if i == 0 and j == n + 1:
			break
		i -= 1
		j += 1
	print(min(abs(i - s), abs(j - s)))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	A = list(map(int, input().split()))
	var = s
	to = 0
	while var < n:
		if var in A:
			to += 1
			var += 1
		else:
			break
	temp = s
	pl = 0
	while temp > 0:
		if temp in A:
			pl += 1
			temp -= 1
		else:
			break
	if temp == 0:
		print(to)
	elif var == n:
		print(pl)
	else:
		print(min(to, pl))


from collections import defaultdict
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	for i in range(k + 1):
		if s - i > 0 and s - i not in l:
			print(i)
			break
		if s + i <= n and s + i not in l:
			print(i)
			break


T = int(input())
for i in range(T):
	(n, s, k) = list(map(int, input().split()))
	ks = list(map(int, input().split()))
	up = down = s
	count = 0
	while True:
		if up in ks and down in ks:
			if up < n:
				up += 1
			if 1 < down:
				down -= 1
			count += 1
		else:
			print(count)
			break


t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	s = int(l[1])
	k = int(l[2])
	l = input().split()
	li = [int(i) for i in l]
	curr = s
	while curr in li:
		curr -= 1
	curr1 = curr
	curr = s
	while curr in li:
		curr += 1
	curr2 = curr
	if curr2 > n or curr2 <= 0:
		print(abs(curr1 - s))
	elif curr1 > n or curr1 <= 0:
		print(abs(curr2 - s))
	elif abs(curr1 - s) > abs(curr2 - s):
		print(abs(curr2 - s))
	else:
		print(abs(curr1 - s))


from sys import stdin

def main():
	input = lambda : stdin.readline()[:-1]
	T = int(input())
	for _ in [0] * T:
		(N, S, K) = map(int, input().split())
		A = list(map(int, input().split()))
		ans = float('inf')
		for i in range(N):
			(n1, n2) = (S - i, S + i)
			if n1 > 0 and n1 not in A:
				print(i)
				break
			if n2 <= N and n2 not in A:
				print(i)
				break
main()


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	k_floors = list(map(int, input().split()))
	if s not in k_floors:
		print(0)
	else:
		k_floors.sort()
		z = k_floors[k_floors.index(s)]
		i = z - 1
		j = z + 1
		minn = n
		while True:
			if i not in k_floors and i > 0:
				c = abs(i - s)
				minn = min(c, minn)
				break
			elif j not in k_floors and j <= n:
				c = abs(j - s)
				minn = min(c, minn)
				break
			i -= 1
			j += 1
		print(minn)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set((int(x) for x in input().split()))
	k = 0
	k1 = 0
	fl = False
	fl1 = False
	for j in range(s, n + 1):
		if j in a:
			if j == n and k != 0:
				k -= 1
				fl1 = True
			elif j == n and k == 0:
				fl1 = True
				break
			k += 1
		else:
			break
	for j in range(s, 0, -1):
		if j in a:
			if j == 1:
				fl = True
				break
			k1 += 1
		else:
			break
	if k1 <= k and (not fl) or fl1:
		print(k1)
		continue
	print(k)


t = int(input())
for _ in range(t):
	(n_floors, floor, n_closed) = [int(i) for i in input().split()]
	closed = [int(i) for i in input().split()]
	a = 0
	while True:
		if (floor - a in closed or floor - a < 1) and (floor + a in closed or floor + a > n_floors):
			a += 1
		else:
			print(a)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = [int(el) for el in input().split()]
	dic = {}
	for i in range(k):
		dic[arr[i]] = 1
	i = s
	ans = 1000
	while i >= max(1, s - k):
		if i not in dic:
			ans = s - i
			break
		else:
			i -= 1
	i = s + 1
	while i <= min(s + k, n):
		if i not in dic:
			ans = min(ans, i - s)
			break
		else:
			i += 1
	print(ans)


import sys
for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	closed = list(map(int, input().split()))
	ans = sys.maxsize
	for i in range(s, n + 1):
		if i not in closed:
			ans = min(ans, abs(s - i))
			break
	for j in range(s, 0, -1):
		if j not in closed:
			ans = min(ans, abs(s - j))
			break
	print(ans)


def main():
	t = int(input())
	for i in range(t):
		(n, s, k) = map(int, input().split())
		L = {int(i) for i in input().split()}
		l = s
		r = s
		g = 0
		while l in L and r in L:
			if l > 1:
				l -= 1
			if r < n:
				r += 1
			g += 1
		print(g)
main()


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	occ = []
	a = list(map(int, input().split()))
	dis = 0
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in a:
			print(i)
			break
		if s + i <= n and s + i not in a:
			print(i)
			break


from math import fabs
n = int(input())
for m in range(n):
	(floors_num, current, closed_num) = list(map(int, input().split()))
	closed = list(map(int, input().split()))
	up = current
	down = current
	if current not in closed:
		print(0)
	else:
		while True:
			if up + 1 <= floors_num:
				if up + 1 not in closed:
					print(up + 1 - current)
					break
				else:
					up += 1
			if down - 1 > 0:
				if down - 1 not in closed:
					print(current - down + 1)
					break
				else:
					down -= 1


t = int(input())
while t:
	t -= 1
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	(i, j) = (s, s)
	while True:
		if i not in l or j not in l:
			break
		if i + 1 <= n:
			i += 1
		if j - 1 > 0:
			j -= 1
	if i in l and j not in l:
		print(abs(s - j))
	elif j in l and i not in l:
		print(abs(s - i))
	else:
		print(min(abs(s - i), abs(s - j)))


t = int(input())
for e in range(t):
	(n, s, k) = map(int, input().split())
	p = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in p):
			print(i)
			break
		if s + i <= n and (not s + i in p):
			print(i)
			break


a = int(input())
for i in range(0, a):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	minn = float('inf')
	ans = 0
	i = 0
	while True:
		if s + i not in arr and s + i <= n:
			ans = i
			break
		if s - i not in arr and s - i > 0:
			ans = i
			break
		i = i + 1
	print(ans)


t = int(input())
for x in range(t):
	n_s_k = list(map(int, input().split(' ')))
	(n, s, k) = n_s_k
	arr = set(map(int, input().split(' ')))
	flag = 1
	x1 = s
	x2 = s
	while flag:
		if not x1 in arr:
			print(abs(s - x1))
			break
		if not x2 in arr:
			print(abs(s - x2))
			break
		if x1 < n:
			x1 += 1
		if x2 > 1:
			x2 -= 1


import sys
for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	i = 0
	while True:
		if s - i > 0 and s - i not in a or (s + i < n + 1 and s + i not in a):
			print(i)
			break
		else:
			i += 1


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split(' '))
	l = list(map(int, input().split(' ')))
	l.sort()
	d = 10 ** 20
	if s in l:
		for j in range(1, len(l)):
			if l[j] != l[j - 1] + 1:
				d = min(d, abs(l[j - 1] + 1 - s))
				d = min(d, abs(l[j] - 1 - s))
		if n not in l:
			d = min(abs(max(l) + 1 - s), d)
		if 1 not in l:
			d = min(abs(min(l) - 1 - s), d)
		print(d)
	else:
		print(0)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	ls = [int(a) for a in input().split()]
	ls.sort()
	for i in range(1002):
		if s - i not in ls and s - i > 0:
			print(i)
			break
		if s + i not in ls and s + i <= n:
			print(i)
			break


t = int(input(''))
result = []
for i in range(t):
	(n, s, k) = tuple(map(int, input('').split(' ')))
	closed = list(map(int, input('').split(' ')))
	steps = 0
	while max(s - steps, 1) in closed and min(s + steps, n) in closed:
		steps += 1
	result.append(steps)
for i in result:
	print(i)


a = int(input())
vv = 100000000000000000
nn = 100000000000000000
for i in range(a):
	(n, m, c) = map(int, input().split())
	s = list(map(int, input().split()))
	for i in range(m, n + 1):
		if i not in s:
			vv = i - m
			break
	for i in range(m, 0, -1):
		if i not in s:
			nn = m - i
			break
	print(min(vv, nn))
	vv = 100000000000000000
	nn = 100000000000000000


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	x = list(map(int, input().split()))
	x.sort()
	if s not in x:
		print(0)
	else:
		p = s
		q = s
		g = 0
		h = 0
		y = []
		for i in range(1, s):
			p = p - 1
			g = g + 1
			if p not in x:
				y.append(p)
				break
		for j in range(s, n):
			q = q + 1
			h = h + 1
			if q not in x:
				y.append(q)
				break
		if len(y) == 1:
			print(abs(y[0] - s))
		else:
			a = abs(y[0] - s)
			b = abs(y[1] - s)
			print(min(a, b))


import math
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	x = math.inf
	for i in range(k + 2):
		if s + i <= n and s + i not in a:
			x = i
			break
	y = math.inf
	for i in range(k + 2):
		if s - i >= 1 and s - i not in a:
			y = i
			break
	print(min(x, y))


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	c1 = 1000
	c2 = 1000
	res = 1000
	if s not in l:
		print(0)
	else:
		r = l.index(s)
		ll = min(l[-1] + 1, n)
		if r != len(l) - 1:
			for i in range(l[r] + 1, ll + 1, 1):
				if i not in l:
					c1 = abs(s - i)
					break
		elif l[-1] != n:
			c1 = 1
		for i in range(l[r] - 1, 0, -1):
			if i not in l:
				c2 = abs(s - i)
				break
		print(min(c1, c2))


from sys import stdin
import math

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(n, s, k) = [int(x) for x in inp().split()]
	K = [int(x) for x in inp().split()]
	if not s in K:
		print(0)
		continue
	for i in range(k + 1):
		if s - i > 0 and s + i <= n:
			if s - i in K and s + i in K:
				continue
		elif s + i <= n:
			if s + i in K:
				continue
		elif s - i > 0:
			if s - i in K:
				continue
		print(i)
		break


t = int(input())
all_close = []
al = []
for i in range(t):
	(n, s, k) = map(int, input().split())
	all_close.append(set(map(int, input().split())))
	al.append([n, s])
ans = [-1] * t
for dist in range(0, 1001):
	for i in range(t):
		if ans[i] == -1:
			(n, s) = (al[i][0], al[i][1])
			if s - dist > 0 and s - dist not in all_close[i]:
				ans[i] = dist
			elif s + dist <= n and s + dist not in all_close[i]:
				ans[i] = dist
for i in range(t):
	print(ans[i])


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	ar = list(map(int, input().split()))
	x = s
	c = 0
	ans1 = 0
	ans2 = 0
	while c < n:
		if x not in ar:
			ans1 = c
			break
		else:
			c += 1
			x += 1
		if x == n + 1:
			ans1 = n + 1
			break
	x = s
	c = 0
	while c < n:
		if x not in ar:
			ans2 = c
			break
		else:
			c += 1
			x -= 1
		if x == 0:
			ans2 = n + 1
			break
	print(min(ans1, ans2))


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	(n, s, k) = li()
	se = set(li())
	ans = float('inf')
	for i in range(s, n + 1):
		if i not in se:
			ans = min(ans, i - s)
			break
	for i in range(s, 0, -1):
		if i not in se:
			ans = min(ans, s - i)
			break
	print(ans)


for t in range(int(input())):
	(n, m, k) = map(int, input().split())
	b = list(map(int, input().split()))
	a = {}
	for i in range(k):
		a[b[i]] = m - b[i]
	q = m
	count = 0
	store = 9999999
	while q != 0 and count != 1000:
		if q not in a:
			store = q
			break
		q -= 1
	count = 0
	bag = 9999999
	q = m
	while q != n + 1 and count != 1000:
		if q not in a:
			bag = q
			break
		q += 1
	if abs(m - store) < abs(m - bag):
		print(abs(m - store))
	else:
		print(abs(m - bag))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	h = {i: 1 for i in l}
	g = []
	for i in range(s, n + 1):
		if i not in h:
			g.append(i - s)
			break
	for j in range(s, 0, -1):
		if j not in h:
			g.append(s - j)
			break
	print(min(g))


t = int(input())
for j in range(t):
	n = list(map(int, input().split(' ')))
	closed_rest = list(map(int, input().split(' ')))
	up = 0
	down = 0
	for i in range(n[0]):
		if n[1] + i <= n[0] and n[1] + i not in closed_rest:
			print(up)
			break
		if n[1] - i >= 1 and n[1] - i not in closed_rest:
			print(down)
			break
		up += 1
		down += 1


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	l.sort()
	ans = n
	for i in range(k + 1):
		if i == 0:
			low = 1
			high = l[i] - 1
		elif i == k:
			low = l[i - 1] + 1
			high = n
		else:
			low = l[i - 1] + 1
			high = l[i] - 1
		if low > high:
			continue
		if s < low:
			ans = min(ans, low - s)
		elif s > high:
			ans = min(ans, s - high)
		else:
			ans = 0
	print(ans)


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
lx = lambda x: map(int, input().split(x))
from math import log10, log2, ceil, factorial as fac, gcd, inf
for _ in range(1, t() + 1):
	(n, s, k) = ll()
	a = list(ll())
	if s not in a:
		print(0)
		continue
	a.sort()
	l = []
	b = 1
	for i in range(k):
		if a[i] != b:
			l.append([b, a[i] - 1])
			b = a[i] + 1
		else:
			b = a[i] + 1
	if b <= n:
		l.append([b, n])
	ans = inf
	for i in l:
		(x, y) = i
		if x <= s <= y:
			ans = min(ans, abs(s - x), abs(y - s))
		elif s <= x:
			ans = min(ans, abs(x - s))
		elif s >= y:
			ans = min(ans, abs(y - s))
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = [int(x) for x in input().split()]
	if s not in closed:
		print('0')
	else:
		closed.sort()
		cur_index = closed.index(s)
		keep_looking = True
		iterator = 1
		while keep_looking:
			left = cur_index - iterator
			right = cur_index + iterator
			if left >= 0:
				diff = closed[cur_index] - closed[left]
				if diff != iterator:
					keep_looking = False
					break
			elif closed[0] != 1:
				keep_looking = False
				break
			if right < k:
				diff = closed[right] - closed[cur_index]
				if diff != iterator:
					keep_looking = False
					break
			elif closed[k - 1] != n:
				keep_looking = False
				break
			iterator += 1
		print(iterator)


t = int(input())
f = ''
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(x, y) = (s, s)
	for i in range(k + 1):
		if x in a:
			x += 1
		if y in a:
			y -= 1
	if x == n + 1:
		f += str(s - y) + '\n'
	elif y == 0:
		f += str(x - s) + '\n'
	else:
		d = min(s - y, x - s)
		f += str(d) + '\n'
print(f)


def bs(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1
t = int(input())
while t > 0:
	(n, s, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	a = sorted(a)
	tof = s
	l = s
	r = s
	c = 0
	d = 0
	for i in range(n):
		if l > 0:
			c += 1
			f = bs(a, 0, len(a) - 1, l)
			if f == -1:
				print(c - 1)
				break
			else:
				l -= 1
		if r <= n:
			d += 1
			f = bs(a, 0, len(a) - 1, r)
			if f == -1:
				print(d - 1)
				break
			else:
				r += 1
	t -= 1


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def solve(adj, colors, n):
	pass
for _ in range(iinput()):
	(n, s, k) = rinput()
	a = rlinput()
	temp = 0
	while 1:
		if s + temp <= n and s + temp not in a:
			print(temp)
			break
		elif s - temp >= 1 and s - temp not in a:
			print(temp)
			break
		temp += 1


a = int(input())
test = []
closed = []
for i in range(a):
	case = [int(val) for val in input().split(' ')]
	clo = [int(val) for val in input().split(' ')]
	test.append(case)
	closed.append(clo)
for i in range(a):
	case = test[i]
	f = case[1]
	close = closed[i]
	i = 0
	while True:
		if f + i <= case[0] and i + f not in close:
			print(i)
			break
		elif f - i >= 1 and f - i not in close:
			print(i)
			break
		i += 1


for t in range(int(input())):
	(n, start, k) = map(int, input().split())
	lk = list(map(int, input().split()))
	for i in range(0, k + 1):
		if start + i <= n and start + i not in lk:
			print(i)
			break
		if start - i > 0 and start - i not in lk:
			print(i)
			break


test = int(input())
for t in range(test):
	xiao = 1001
	(n, s, k) = map(int, input().split())
	floor = input().split()
	for i in range(len(floor)):
		floor[i] = int(floor[i])
	if s in floor:
		i = s - 1
		while i >= 1:
			if i not in floor:
				xiao = s - i
				break
			else:
				i -= 1
		j = s + 1
		while j <= n:
			if j not in floor:
				xiao1 = j - s
				if xiao1 < xiao:
					xiao = xiao1
				break
			else:
				j += 1
	else:
		xiao = 0
	print(xiao)


for t in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	rep = set()
	for i in arr:
		rep.add(i)
	mind = k
	for i in range(max(1, s - k), min(n, s + k) + 1):
		if i not in rep:
			mind = min(mind, abs(i - s))
	print(mind)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	kar = [int(x) for x in input().split()]
	ans = 0
	while True:
		if s + ans > 0 and s + ans < n + 1 and (s + ans not in kar):
			break
		elif s - ans > 0 and s - ans < n + 1 and (s - ans not in kar):
			break
		else:
			ans += 1
	print(ans)


import math

def main():
	for _ in range(int(input())):
		(num_floor, current_pos, closed_floor) = map(int, input().split(' '))
		l = list(map(int, input().split(' ')))
		l.sort()
		for i in range(closed_floor + 1):
			if current_pos - i >= 1:
				if current_pos - i not in l:
					print(i)
					break
			if current_pos + i <= num_floor:
				if current_pos + i not in l:
					print(i)
					break
main()


import sys, math
import io, os

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	(n, s, k) = mdata()
	a = set(mdata())
	ans = INF
	for i in range(max(1, s - 1001), min(s + 1001, n + 1)):
		if i not in a:
			ans = min(ans, abs(i - s))
	out(ans)


for ttt in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 10 ** 9
	flag = 0
	for i in range(k + 1):
		if s + i <= n and s + i not in arr:
			print(i)
			break
		if s - i >= 1 and s - i not in arr:
			print(i)
			break


t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a = sorted(a)
	for j in range(1002):
		if l[1] not in a:
			print(0)
			break
		if l[1] + j + 1 not in a:
			if l[1] + j + 1 <= l[0]:
				print(j + 1)
				break
		if l[1] - j - 1 not in a:
			if l[1] - j - 1 > 0:
				print(abs(-j - 1))
				break


for t in range(int(input())):
	(fl, s, k) = map(int, input().split())
	(l, d) = ([], {})
	l = [int(x) for x in input().split()]
	(mx, mn) = (max(l), min(l))
	d = {i: 1 for i in l}
	if s < mn or s > mx:
		print(0)
	else:
		(n, m, k) = (-1, -1, True)
		for i in range(s, mx + 1):
			if i not in d:
				n += 1
				k = False
				break
			else:
				n += 1
		if k == True and i < fl:
			n += 1
		elif i == fl:
			k = True
			n = 1000000000 + 4
		for i in range(s, mn - 1, -1):
			if i not in d:
				m += 1
				k = False
				break
			else:
				m += 1
		if k == True and i > 1:
			m += 1
		elif i == 1:
			m = 1000000000 + 4
		print(min(m, n))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	closed.sort()
	for i in range(n + 1):
		(key_1, key_2) = (s - i, s + i)
		if key_1 > 0 and key_1 not in closed or (key_2 < n + 1 and key_2 not in closed):
			print(i)
			break


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	if s not in l:
		print(0)
	else:
		for i in range(k + 1):
			if s - i > 0 and s - i not in l or (s + i <= n and s + i not in l):
				print(i)
				break


for _ in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	floors = set([int(x) for x in input().split()])
	for i in range(n):
		if s + i not in floors and 1 <= s + i <= n:
			break
		elif s - i not in floors and 1 <= s - i <= n:
			break
	print(i)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	result = -1
	steps = 0
	g = s
	while result == -1:
		if s not in closed and s > 0:
			result = steps
		elif g not in closed and g < n + 1:
			result = steps
		else:
			g += 1
			s -= 1
			steps += 1
	print(steps)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	(n, s, k) = get_int_list()
	lst = set(get_int_list())
	i = 0
	while True:
		below = max(s - i, 1)
		above = min(n, s + i)
		if below not in lst:
			ans = s - below
			break
		if above not in lst:
			ans = above - s
			break
		i += 1
	print(ans)


def solve(n, s, busy_set):
	i = 0
	while (s + i in busy_set or s + i > n) and (s - i in busy_set or s - i < 1):
		i += 1
	return i
n_cases = int(input())
for _ in range(n_cases):
	(n, s, k) = map(int, input().split())
	busy_set = set(map(int, input().split()))
	print(solve(n, s, busy_set))


t = int(input())
for _ in range(t):
	inp = input().split()
	n = int(inp[0])
	s = int(inp[1])
	k = int(inp[2])
	closed = input().split()
	closed = [int(x) for x in closed]
	closed = sorted(closed)
	extra = [0] + closed + [10 ** 10]
	left_c = 0
	right_c = 0
	for i in range(k + 2 - 1):
		if extra[i] < s and extra[i + 1] > s:
			left_c = i - 1
			right_c = i
			break
		if extra[i] == s:
			left_c = i - 1
			right_c = i - 1
			break
	left = s
	if left >= closed[0]:
		while left >= 1:
			if closed[left_c] == left:
				left -= 1
				left_c -= 1
				if left_c < 0:
					break
			else:
				break
	right = s
	if right <= closed[-1]:
		while right <= n:
			if closed[right_c] == right:
				right += 1
				right_c += 1
				if right_c == len(closed):
					break
			else:
				break
	if left == 0:
		print(right - s)
	elif right == n + 1:
		print(s - left)
	elif s - left < right - s:
		print(s - left)
	else:
		print(right - s)


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = set(list(map(int, input().split())))
	(down, up) = (0, 0)
	if s not in closed:
		print(0)
	else:
		for i in range(s - 1, 0, -1):
			if i not in closed:
				down = s - i
				break
		for j in range(s + 1, n + 1):
			if j not in closed:
				up = j - s
				break
		if up != 0 and down != 0:
			print(min(down, up))
		else:
			print(max(down, up))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print(0)
	else:
		td = tu = s
		while td in a and td > 0:
			td -= 1
		while tu in a and tu < n:
			tu += 1
		if td == 0:
			print(tu - s)
		elif tu == n:
			print(s - td)
		else:
			print(min(tu - s, s - td))


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split(' '))
	arr = list(map(int, input().split()))
	d = {}
	d = defaultdict(lambda : 0, d)
	for i in arr:
		d[i] = 1
	if d[s] == 0:
		print(0)
	else:
		i = s
		ans = ans1 = 1000000001
		while i >= 1:
			if d[i] == 0:
				ans = abs(i - s)
				break
			i -= 1
		i = s + 1
		while i <= n:
			if d[i] == 0:
				ans1 = abs(i - s)
				break
			i += 1
		print(min(ans, ans1))


import sys
inp = sys.stdin.readline
input = lambda : inp().strip()
flush = sys.stdout.flush

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	t = iin()
	while t:
		t -= 1
		(n, s, k) = lin()
		a = lin()
		a.sort()
		a = set(a)
		i = s
		j = s
		while i in a:
			i += 1
		while j in a:
			j -= 1
		ans = min(s - j, i - s)
		if j < 1:
			ans = i - s
		if i > n:
			ans = s - j
		print(ans)
main()


import math
from collections import deque
se = set()
t = int(input())
while t:
	t -= 1
	s = input().split(' ')
	n = int(s[0])
	ss = int(s[1])
	k = int(s[2])
	se.clear()
	s = input().split(' ')
	for i in range(k):
		se.add(int(s[i]))
	for i in range(n):
		if ss + i <= n and (not ss + i in se):
			print(i)
			break
		if ss > i and (not ss - i in se):
			print(i)
			break


def solve(n, s, k_arr):
	(up, down) = (0, 0)
	(upFound, downFound) = (False, False)
	for i in range(s, n + 1):
		if i in k_arr:
			up += 1
		else:
			upFound = True
			break
	for i in range(s, 0, -1):
		if i in k_arr:
			down += 1
		else:
			downFound = True
			break
	if upFound and downFound:
		return min(up, down)
	if upFound and (not downFound):
		return up
	if not upFound and downFound:
		return down
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	k_arr = list(map(int, input().split()))
	print(solve(n, s, k_arr))


def bisectLeft(arr, k):
	low = 0
	high = len(arr)
	while low < high:
		mid = (low + high) // 2
		if arr[mid] < k:
			low = mid + 1
		else:
			high = mid
	return low
for t in range(int(input())):
	(n, s, k) = map(int, input().split())
	closedFloors = list(map(int, input().split()))
	closedFloors.sort()
	ind = bisectLeft(closedFloors, s)
	res = k
	for i in range(k):
		if ind + i < k and closedFloors[ind + i] != s + i or (ind + i >= k and closedFloors[-1] != n):
			res = i
			break
		if ind - i >= 0 and closedFloors[ind - i] != s - i or (ind - i < 0 and closedFloors[0] != 1):
			res = i
			break
	print(res)


t = int(input())
while t:
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	top = bottom = s
	if s not in a:
		print(0)
	else:
		while top <= n or bottom >= 1:
			top += 1
			bottom -= 1
			if top <= n:
				if top not in a:
					print(top - s)
					break
			if bottom >= 1:
				if bottom not in a:
					print(s - bottom)
					break
	t -= 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	free_l = -n - 1
	et = s
	while et > 0:
		if et not in closed:
			free_l = et
			break
		et -= 1
	free_r = 2 * n + 1
	et = s
	while et <= n:
		if et not in closed:
			free_r = et
			break
		et += 1
	print(min(s - free_l, free_r - s))


for j in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	for i in range(n):
		if s > i and s - i not in a or (s + i <= n and s + i not in a):
			print(i)
			break


def checkup(n, s, arr):
	if s > n:
		return 1000000
	if s in arr:
		return 1 + checkup(n, s + 1, arr)
	else:
		return 0

def checkdown(n, s, arr):
	if s == 0:
		return 1000000
	if s in arr:
		return 1 + checkdown(n, s - 1, arr)
	else:
		return 0
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	num = list(map(int, input().split()))
	ans = min(checkup(n, s, num), checkdown(n, s, num))
	print(ans)


t = int(input())

def solve():
	(n, s, k) = map(int, input().split())
	closed = set(list(map(int, input().split())))
	ans = 1001
	curr = s
	cnt = 0
	while curr >= 1:
		if curr not in closed:
			ans = min(ans, cnt)
			break
		curr -= 1
		cnt += 1
	curr = s
	cnt = 0
	while curr <= n:
		if curr not in closed:
			ans = min(ans, cnt)
			break
		curr += 1
		cnt += 1
	print(ans)
	return
for _ in range(t):
	solve()


T = int(input())
for i in range(T):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	min = 1000000000
	if s in l:
		for x in l:
			if int(x - 1) not in l and x != 1:
				if abs(x - s - 1) < min:
					min = abs(x - s - 1)
			if int(x + 1) not in l and x != n:
				if abs(x - s + 1) < min:
					min = abs(x - s + 1)
	else:
		min = 0
	print(min)


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	for i in range(1010):
		if s + i <= n and s + i not in a or (s - i > 0 and s - i not in a):
			print(i)
			break


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	(N, X, K) = MAP()
	S = set(LIST())
	cnt1 = 0
	for i in range(X, N + 1):
		if i not in S:
			break
		cnt1 += 1
	else:
		cnt1 = INF
	cnt2 = 0
	for i in range(X, 0, -1):
		if i not in S:
			break
		cnt2 += 1
	else:
		cnt2 = INF
	ans = min(cnt1, cnt2)
	print(ans)
	del S


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	minn = n + 1
	for i in range(s, n + 1):
		if i not in l:
			minn = abs(i - s)
			break
	for i in range(s - 1, 0, -1):
		if i not in l:
			minn = min(minn, abs(i - s))
			break
	print(minn)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	if s not in a:
		print(0)
	else:
		counter1 = 0
		s_check1 = s
		min_count = float('Inf')
		while s_check1 in a:
			s_check1 += 1
			counter1 += 1
		if s_check1 == n + 1:
			counter1 = float('Inf')
		counter2 = 0
		s_check2 = s
		while s_check2 in a:
			s_check2 -= 1
			counter2 += 1
		if s_check2 == 0:
			counter2 = float('Inf')
		print(min(counter1, counter2))


from sys import stdin
T = int(input())
for _ in range(T):
	(n, s, k) = list(map(int, stdin.readline().rstrip().split(' ')))
	arr = list(map(int, stdin.readline().rstrip().split(' ')))
	minFloors = 10000000000.0
	for i in range(s, n + 1):
		if i not in arr:
			minFloors = i - s
			break
	for i in range(s - 1, 0, -1):
		if i not in arr:
			if s - i < minFloors:
				minFloors = s - i
			break
	print(minFloors)


def find(l, n, s, k):
	l.add(0)
	l.add(n + 1)
	f = 0
	g = 0
	if s in l:
		c = s
		d = s
		while f != 1 and g != 1:
			if c != 0:
				c = c - 1
			if c in l:
				f = 0
			else:
				f = 1
				g = 1
			if d != n + 1:
				d = d + 1
			if d in l:
				g = 0
			else:
				g = 1
		if c == 0:
			print(d - s)
		elif d == n + 1:
			print(s - c)
		elif d - s < s - c:
			print(d - s)
		else:
			print(s - c)
	else:
		print(0)
z = int(input())
for y in range(z):
	(n, s, k) = map(int, input().split())
	l = set(map(int, input().split()))
	find(l, n, s, k)


from math import inf
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = set(list(map(int, input().split())))
	ans = inf
	for f in range(s, n + 1):
		if f not in arr:
			ans = min(ans, f - s)
			break
	for f in range(s, 0, -1):
		if f not in arr:
			ans = min(ans, s - f)
			break
	print(ans)


MAX_K = 1005
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	disable_floors = set(map(int, input().split()))
	ret = 0
	for i in range(MAX_K):
		if s + i <= n and s + i not in disable_floors:
			ret = i
			break
		elif s - i > 0 and s - i not in disable_floors:
			ret = i
			break
	print(ret)


for _ in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	d = dict()
	a = list(map(int, input().split()))
	for i in range(k):
		d[a[i]] = 1
	l = s
	num1 = 0
	while l >= 1:
		if not d.get(l):
			break
		num1 += 1
		l -= 1
	if l < 1:
		num1 = 99999999999999
	num2 = 0
	r = s
	while r <= n:
		if not d.get(r):
			break
		num2 += 1
		r += 1
	if r > n:
		num2 = 99999999999999
	print(min(num1, num2))


def floor():
	for _ in range(t):
		(n, s, k) = [int(x) for x in input().split()]
		a = {int(x): True for x in input().split()}
		mindis = float('inf')
		for i in range(n):
			prevp = s - i
			nextp = s + i
			inornop = a.get(prevp, False)
			inornon = a.get(nextp, False)
			if prevp > 0 and (not inornop):
				yield i
				break
			elif nextp <= n and (not inornon):
				yield i
				break
t = int(input())
ans = floor()
print(*ans, sep='\n')


t = int(input())
while t:
	t -= 1
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	if s not in a:
		print(0)
	else:
		a.sort()
		res = a.index(s)
		i = res - 1
		j = res + 1
		mx1 = 1000000000
		mx2 = 1000000000
		while i >= 0:
			if a[i + 1] - a[i] > 1:
				mx1 = res - i
				break
			i -= 1
		while j < k:
			if a[j] - a[j - 1] > 1:
				mx2 = j - res
				break
			j += 1
		if a[0] != 1:
			mx1 = min(mx1, s - (a[0] - 1))
		if a[k - 1] != n:
			mx2 = min(mx2, a[k - 1] + 1 - s)
		print(min(mx1, mx2))


import sys
tc = int(sys.stdin.readline())
for _ in range(tc):
	(n, s, k) = map(int, sys.stdin.readline().split())
	arr = list(map(int, sys.stdin.readline().split()))
	if s not in arr:
		print(0)
	else:
		ans = 9876543210
		arr.sort()
		for i in range(k):
			if 1 <= arr[i] - 1 <= n and arr[i] - 1 not in arr:
				ans = min(ans, abs(s - (arr[i] - 1)))
			if 1 <= arr[i] + 1 <= n and arr[i] + 1 not in arr:
				ans = min(ans, abs(s - (arr[i] + 1)))
		print(ans)


t = int(input())
for _ in range(0, t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	m = 10000000
	for i in range(0, k + 1):
		if s - i not in a and s - i > 0:
			m = min(m, abs(i))
		elif s - i < 1:
			break
	for i in range(0, k + 1):
		if s + i not in a and s + i < n + 1:
			m = min(m, abs(i))
		elif s + i > n:
			break
	print(m)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	mass = [int(x) for x in input().split()]
	mass.sort()
	answer = 10000000000.0
	for j in range(1, len(mass)):
		if mass[j - 1] + 1 != mass[j]:
			if mass[j - 1] < s and s < mass[j]:
				answer = 0
				break
			if mass[j] <= s:
				ch = mass[j] - 1
			else:
				ch = mass[j - 1] + 1
			answer = min(answer, abs(s - ch))
	if mass[0] != 1:
		if mass[0] > s:
			answer = 0
		else:
			answer = min(answer, abs(mass[0] - 1 - s))
	if mass[-1] != n:
		if mass[-1] < s:
			answer = 0
		else:
			answer = min(answer, abs(mass[-1] + 1 - s))
	print(answer)


from collections import defaultdict as dc
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = dc(int)
	for i in l:
		d[i] = 1
	if d[s] == 0:
		print(0)
	else:
		i = s - 1
		(a, b) = (n, n)
		while i >= 1 and i >= s - k:
			if d[i] == 0:
				a = s - i
				break
			i -= 1
		j = s + 1
		while j <= n and j <= s + k:
			if d[j] == 0:
				b = j - s
				break
			j += 1
		print(min(a, b))


t = int(input())
A = []
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print('0')
	else:
		i = s
		j = s
		ct = 0
		while i != 0 and j != n + 1:
			ct = ct + 1
			if i > 1:
				i = i - 1
			if j < n:
				j = j + 1
			if i not in a or j not in a:
				break
		print(ct)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	if s not in A:
		print(0)
	else:
		i = A.index(s)
		for j in range(k):
			if i + j + 1 >= k and A[-1] != n or (i + j + 1 < k and A[i + j + 1] != s + j + 1):
				print(j + 1)
				break
			elif i - j - 1 < 0 and A[0] != 1 or (i - j - 1 >= 0 and A[i - j - 1] != s - j - 1):
				print(j + 1)
				break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	cl = list(map(int, input().split()))
	cl.sort()
	fans = 1e+20
	if s in cl:
		for i in range(k):
			if i != 0:
				if cl[i - 1] != cl[i] - 1:
					fans = min(fans, abs(s - (cl[i] - 1)))
			elif cl[i] - 1 > 0:
				fans = min(fans, abs(s - (cl[i] - 1)))
			if i != k - 1:
				if cl[i + 1] != cl[i] + 1:
					fans = min(fans, abs(s - (cl[i] + 1)))
			elif cl[i] + 1 <= n:
				fans = min(fans, abs(s - (cl[i] + 1)))
	else:
		fans = 0
	print(fans)


for _ in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	lst = [int(i) for i in input().split()]
	i = 0
	while True:
		if s - i > 0:
			if s - i not in lst:
				print(i)
				break
		if s + i <= n:
			if s + i not in lst:
				print(i)
				break
		i += 1


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	ac = dict()
	for i in range(k):
		ac[arr[i]] = 1
	lower = s
	flag1 = 0
	while lower > 0:
		if lower not in ac.keys():
			flag1 = 1
			break
		else:
			lower -= 1
	upper = s
	flag2 = 0
	while upper <= n:
		if upper not in ac.keys():
			flag2 = 1
			break
		else:
			upper += 1
	if flag1 and flag2:
		print(min(s - lower, upper - s))
	elif flag1:
		print(s - lower)
	else:
		print(upper - s)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	m = 1000000000
	for i in range(s, n + 1):
		if i not in a:
			m = min(m, abs(s - i))
			break
	for i in range(s, 0, -1):
		if i not in a:
			m = min(m, abs(s - i))
			break
	print(m)


from math import *
from bisect import *
from collections import Counter, defaultdict, deque
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	d = defaultdict(bool)
	(n, s, k) = M()
	a = LI()
	for i in a:
		d[i] = True
	if not d[s]:
		print(0)
	else:
		ans = n
		for i in range(s - 1, max(s - 1001, 0), -1):
			if d[i] != True:
				ans = s - i
				break
		for i in range(s + 1, min(n + 1, s + 1001)):
			if d[i] != True:
				ans = min(ans, i - s)
				break
		print(ans)


for t in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	dic = {}
	for i in a:
		dic[i] = True
	if s not in a:
		print(0)
	else:
		(d1, d2) = (0, 0)
		(status1, status2) = (False, False)
		for i in range(s - 1, 0, -1):
			if dic.get(i, False) == True:
				d1 += 1
			else:
				d1 += 1
				status1 = True
				break
		for i in range(s + 1, n + 1):
			if dic.get(i, False) == True:
				d2 += 1
			else:
				d2 += 1
				status2 = True
				break
		if status1 == True and status2 == True:
			print(min(d1, d2))
		elif status1 == True:
			print(d1)
		elif status2 == True:
			print(d2)
		else:
			print(0)


from sys import stdin

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())
debugging = False

def dprint(*args):
	if debugging:
		print(*args)
	else:
		pass
t = intput()
for _ in range(t):
	(n, s, k) = intsput()
	closed_ = list(intsput())
	closed = set()
	for x in closed_:
		closed.add(x)
	found = []
	for higher in range(s, s + 1002):
		if higher <= n and higher not in closed:
			found.append(higher)
			break
	for lower in range(s, s - 1003, -1):
		if lower >= 1 and lower not in closed:
			found.append(lower)
			break
	abc = []
	for x in found:
		abc.append(abs(s - x))
	print(min(abc))


t = int(input())
for g in range(t):
	sq = str(input()).split()
	n = int(sq[0])
	s = int(sq[1])
	sa = str(input()).split()
	l = [int(x) for x in sa]
	i = 0
	while True:
		if s + i < n + 1 and s + i not in l:
			print(i)
			break
		elif s - i > 0 and s - i not in l:
			print(i)
			break
		i += 1


from sys import stdin

def solve():
	t = int(stdin.readline().strip())
	for _ in range(t):
		(n, s, k) = [int(i) for i in stdin.readline().strip().split()]
		floors = set([int(i) for i in stdin.readline().strip().split()])
		found = n
		for e in range(max(1, s - 1000), min(s + 1000, n) + 1):
			if e not in floors:
				found = min(found, abs(s - e))
		print(found)
solve()


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	j = 0
	jh = 1
	while jh > 0:
		if s + j not in l and s + j <= n:
			print(s + j - s)
			jh = 0
		elif s - j not in l and s - j >= 1:
			print(s - s + j)
			jh = 0
		else:
			j += 1


def R():
	return map(int, input().split())

def I():
	return int(input())

def S():
	return str(input())

def L():
	return list(R())
from collections import Counter
import math
import sys
for _ in range(I()):
	(n, s, k) = R()
	xx = L()
	a = set()
	for x in xx:
		a.add(x)
	k2 = s
	k3 = s
	while k2 in a:
		k2 += 1
	while k3 in a:
		k3 -= 1
	if k3 <= 0:
		k3 = -10 ** 10
	if k2 > n:
		k2 = 10 ** 10
	print(min(k2 - s, s - k3))


def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]
from sys import stdin
from bisect import *
for i in range(int(input())):
	(n, s, k) = arr_inp(1)
	a = sorted(arr_inp(1))
	if s not in a:
		print(0)
	elif len(a) == 1 or s < a[0] - 1 or s > a[-1] + 1 or (a[0] == s and s > 1) or (a[-1] == s and s < n):
		print(1)
	else:
		(ix, ans) = (bisect_left(a, s), [])
		c1 = 1
		for i in range(ix + 1, k):
			if a[i] > c1 + s:
				ans.append(s + c1)
				break
			c1 += 1
		c1 = 1
		for i in range(ix - 1, -1, -1):
			if a[i] < s - c1:
				ans.append(s - c1)
				break
			c1 += 1
		if a[0] > 1 and a[0] != s:
			ans.append(a[0] - 1)
		if a[-1] < n and a[-1] != s:
			ans.append(a[-1] + 1)
		mi = float('inf')
		for i in ans:
			mi = min(mi, abs(i - s))
		print(mi)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	z = list(map(int, input().split()))
	for j in range(0, k + 1):
		if s + j <= n:
			if s + j not in z:
				print(j)
				break
		if s - j > 0:
			if s - j not in z:
				print(j)
				break


for i in range(int(input())):
	(n, c, o) = map(int, input().split())
	a = [*map(int, input().split())]
	i = 0
	while max(c - i, 1) in a and min(c + i, n) in a:
		i += 1
	print(i)


from math import *
t = int(input())
while t > 0:
	t = t - 1
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	res = int(10000000000.0)
	start = int(s - 1010)
	fin = int(s + 1010)
	for i in range(start, fin + 1):
		if i <= 0 or i > n:
			continue
		et = int(i)
		if et in a:
			continue
		r = abs(et - s)
		res = min(res, r)
	print(res)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	fl = {int(x) for x in input().split()}
	x = 0
	while True:
		if x + s not in fl or s - x not in fl if x + s <= n and s - x > 0 else x + s not in fl and s - x not in fl:
			break
		else:
			x += 1
	print(x)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	x = set(map(int, input().split()))
	(ans, a, b) = (0, s, s)
	while a in x and b in x:
		(a, ans, b) = (a - 1, ans + 1, b + 1)
		if a == 0:
			a = s
		if b == n + 1:
			b = s
	print(ans)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = range(max(1, s - 1001), min(n, s + 1001) + 1)
	a = set(a)
	b = set(list(map(int, input().split())))
	a = a - b
	a = list(a)
	a.sort()
	ans = 10 ** 5
	for i in a:
		ans = min(abs(i - s), ans)
	print(ans)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	for j in range(k + 1):
		if j + s < n + 1 and j + s not in a or (s - j > 0 and s - j not in a):
			print(j)
			break


x = int(input())
ans = []
for i in range(x):
	(n, s, k) = [int(x) for x in input().split()]
	closed = [int(x) for x in input().split()]
	closed.sort()
	if s in closed:
		left_p = s
		l_l = 0
		right_p = s
		r_l = 0
		while True:
			if left_p in closed and left_p > 0:
				left_p -= 1
				l_l += 1
			if right_p in closed and right_p < n + 1:
				right_p += 1
				r_l += 1
			if left_p not in closed and left_p != 0 or (right_p not in closed and right_p != n + 1):
				break
		if left_p == 0:
			ans.append(r_l)
		elif right_p == n + 1:
			ans.append(l_l)
		else:
			ans.append(min(l_l, r_l))
	else:
		ans.append(0)
for i in ans:
	print(i)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	a = 10 ** 10
	pb = []
	for i in range(s, n + 1):
		if i not in arr:
			a = min(a, abs(s - i))
			break
	for j in range(s, 0, -1):
		if j not in arr:
			a = min(a, abs(s - j))
			break
	print(a)


t = int(input())
for y in range(t):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	ans = 0
	d = dict()
	for i in range(k):
		d[a[i]] = 1
	while True:
		if s - ans > 0 and s - ans not in d:
			break
		if s + ans <= n and s + ans not in d:
			break
		ans += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = set(list(map(int, input().split())))
	ans1 = 0
	temp = s
	while True:
		if temp > n:
			ans1 = float('inf')
		if temp not in l:
			break
		else:
			ans1 += 1
			temp += 1
	ans2 = 0
	temp = s
	while True:
		if temp < 1:
			ans2 = float('inf')
		if temp not in l:
			break
		else:
			ans2 += 1
			temp -= 1
	print(min(ans1, ans2))


a = int(input())
for _ in range(a):
	(x, y, z) = map(int, input().split())
	li = list(map(int, input().split()))
	li = set(li)
	for i in range(x):
		if y + i <= x and y + i not in li:
			print(i)
			break
		elif y - i > 0 and y - i not in li:
			print(i)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	kk = dict()
	for i in range(0, len(a)):
		kk[a[i]] = 1
	ans = 1001
	for i in range(s, s + 1001):
		if i > n or i < 1:
			break
		if kk.get(i) == None:
			ans = min(ans, i - s)
			break
	for i in range(s, s - 1001, -1):
		if i > n or i < 1:
			break
		if kk.get(i) == None:
			ans = min(ans, s - i)
			break
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	x = y = s
	c1 = c2 = 0
	if s not in a:
		print('0')
	elif len(a) == n:
		print('0')
	else:
		while True:
			if x > 1:
				x = x - 1
				c1 += 1
			if y < n:
				y = y + 1
				c2 += 1
			if x not in a:
				print(c1)
				break
			elif y not in a:
				print(c2)
				break


r = lambda : map(int, input().split())
(t,) = r()
exec(t * 'n,s,k=r();a={*r()};i=0\nwhile{max(1,s-i),min(n,s+i)}<=a:i+=1\nprint(i);')


for _ in range(int(input())):
	(n, k, r) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	l = set(l)
	ans = 19999844654
	for i in range(k - 1005, k + 1002):
		if i > 0 and i <= n and (i not in l):
			ans = min(ans, abs(i - k))
	print(ans)


test = int(input())
while test != 0:
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for i in range(n + 1):
		if s + i not in arr and s + i <= n or (s - i not in arr and s - i > 0):
			print(i)
			break
	test -= 1


t = int(input())
for i in range(0, t):
	a = input()
	b = input()
	n = int(a.split(' ')[0])
	s = int(a.split(' ')[1])
	k = int(a.split(' ')[2])
	floors = []
	for f in b.split(' '):
		floors.append(int(f))
	nstF = s
	nstB = s
	while True:
		if nstF in floors:
			if nstF < n:
				nstF = nstF + 1
		else:
			break
		if nstB in floors:
			if nstB > 1:
				nstB = nstB - 1
		else:
			break
	dist = n
	if nstF not in floors:
		dist = nstF - s
	if nstB not in floors:
		d = s - nstB
		if d < dist:
			dist = d
	print(dist)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	if s in a:
		x = a.index(s)
		y = s - 1
		for i in range(x - 1, -1, -1):
			if a[i] != y:
				break
			y -= 1
		z = s + 1
		for i in range(x + 1, k, 1):
			if a[i] != z:
				break
			z += 1
		if y <= 0:
			print(z - s)
		elif z >= n:
			print(s - y)
		else:
			print(min(s - y, z - s))
	else:
		print(0)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	(n, s, k) = inputnums()
	a = inputlist()
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in a):
			print(i)
			break
		if s + i <= n and (not s + i in a):
			print(i)
			break


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = {}
	for x in list(map(int, input().split())):
		a[x] = 1
	z = 0
	while True:
		if s - z > 0:
			if not s - z in a:
				print(z)
				break
		if s + z <= n:
			if not s + z in a:
				print(z)
				break
		z += 1


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep=' ', end='\n'):
	print(*var, file=sys.stderr, end=end, sep=sep)
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(t,) = I()
while t:
	t -= 1
	(n, s, k) = I()
	a = set(I())
	x = s
	ans = INF
	while x >= 1:
		if x not in a:
			ans = min(ans, abs(s - x))
			break
		x -= 1
	x = s
	while x <= n:
		if x not in a:
			ans = min(ans, abs(s - x))
			break
		x += 1
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	fs = list(map(int, input().split()))
	pnt1 = s
	pnt2 = s
	L = False
	while pnt1 in fs and pnt2 in fs:
		if pnt1 > 1:
			pnt1 -= 1
		if pnt2 < n:
			pnt2 += 1
	if pnt1 not in fs:
		print(s - pnt1)
	else:
		print(pnt2 - s)


import math
t = int(input())
for _ in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	m = 10000000000000000
	for i in range(max(1, s - 1001), min(n + 1, s + 1001)):
		x = abs(i - s)
		if i not in l:
			m = min(m, x)
	print(m)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	ans = 10 ** 18
	for i in range(s - 1001, s + 1001):
		if 1 <= i <= n and i not in a:
			ans = min(ans, abs(s - i))
	print(ans)


s = int(input())
rs = 0
ls_rs = []
for i in range(s):
	(a, b, c) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	if b not in lst:
		rs = 0
	else:
		l = a - b
		k = b
		lp = b
		ds = b
		rs_up = rs_down = a ** 10
		while l:
			k += 1
			if k not in lst:
				rs_up = k
				break
			else:
				rs_up = a * 10
			l -= 1
		while lp:
			if lp not in lst:
				rs_down = lp
				break
			else:
				rs_down = a * 10
			lp -= 1
		rs = min(abs(-b + rs_up), abs(b - rs_down))
	ls_rs.append(rs)
for i in ls_rs:
	print(i)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	a = set(a)
	a1 = 10 ** 9 + 6
	a2 = 10 ** 9 + 6
	for i in range(0, n - s + 1):
		if s + i not in a:
			a1 = i
			break
	for i in range(0, s):
		if s - i not in a:
			a2 = i
			break
	print(min(a1, a2))


import sys
from fractions import gcd
cin = sys.stdin.buffer.readline
for _ in range(int(cin())):
	(n, s, k) = map(int, cin().split())
	a = list(map(int, cin().split()))
	ans = 1000000
	a.sort()
	ans = 0
	while True:
		if s + ans <= n:
			if s + ans not in a:
				break
		if s - ans > 0:
			if s - ans not in a:
				break
		ans += 1
	print(ans)


t = int(input())
for loop in range(t):
	(n, s, k) = map(int, input().split())
	ans = float('inf')
	p = list(map(int, input().split()))
	dic = {}
	for i in p:
		dic[i] = 1
	for i in range(n):
		if s - i > 0 and s - i not in dic or (s + i <= n and s + i not in dic):
			print(i)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	j = set()
	for i in range(k):
		j.add(a[i])
	for i in range(n):
		if s - i >= 1 and (not s - i in j):
			print(i)
			break
		if s + i <= n and (not s + i in j):
			print(i)
			break


for _ in range(int(input())):
	(n, s, k) = list((int(x) for x in input().split()))
	r = list((int(x) for x in input().split()))
	p = 0
	t1 = s + p
	t2 = s - p
	if t2 < 1:
		t2 = 1
	if t1 > n:
		t1 = n
	while t1 in r and t2 in r:
		p += 1
		t1 = s + p
		t2 = s - p
		if t2 < 1:
			t2 = 1
		if t1 > n:
			t1 = n
	print(p)


def read():
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, s, k, a)

def check_range(x, y, s, n):
	if x <= s <= y:
		down = x - 1
		up = y + 1
		if up > n:
			return s - down
		if down < 1:
			return up - s
		return min(up - s, s - down)
	return None

def solve(n, s, k, a):
	a.sort()
	x = -1
	y = -1
	for i in range(len(a)):
		if a[i] == a[i - 1] + 1:
			y = a[i]
		else:
			poss = check_range(x, y, s, n)
			if poss is not None:
				return poss
			else:
				x = a[i]
				y = a[i]
	poss = check_range(x, y, s, n)
	if poss is not None:
		return poss
	return 0
for i in range(int(input())):
	result = solve(*read())
	print(result)


t = int(input())
for _ in range(0, t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if s not in a:
		print(0)
	else:
		p = 0
		d = 0
		for i in range(s, n + 1):
			if i not in a:
				p = i - s
				break
		for i in range(s - 1, 0, -1):
			if i not in a:
				d = s - i
				break
		if p == 0:
			print(d)
		elif d == 0:
			print(p)
		else:
			print(min(p, d))


t = int(input())
for _ in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	arr = list(map(int, input().split()))
	arr.sort()
	dict = {}
	for i in range(k):
		dict[arr[i]] = 1
	if s not in arr:
		print(0)
		continue
	x = s - 1
	y = s + 1
	while True:
		if dict.get(x) == None and x > 0:
			print(abs(s - x))
			break
		elif dict.get(y) == None and y <= n:
			print(abs(s - y))
			break
		x -= 1
		y += 1


import sys

def f1(x):
	x = int(x)
	return [abs(s - x), x]
read = lambda : list(map(int, sys.stdin.readline().strip().split()))
for i in range(int(input())):
	(n, s, k) = read()
	broken = list(map(f1, sys.stdin.readline().strip().split()))
	broken.sort()
	dist = 0
	index = 0
	while index < k:
		s1 = set()
		s2 = set()
		if s > dist:
			s1.add(s - dist)
		if s + dist <= n:
			s1.add(s + dist)
		while index < k and dist == broken[index][0]:
			s2.add(broken[index][1])
			index += 1
		if s1 != s2:
			break
		dist += 1
	print(dist)


t = int(input())
while t:
	i = 0
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	while True:
		if s + i not in l and s + i <= n or (s - i not in l and s - i > 0):
			print(i)
			break
		i += 1
	t -= 1


for case in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	closed = [int(x) for x in input().split()]
	closed_set = set(closed)
	d_up = 9999999999
	d_down = 9999999999
	i = s
	while True:
		if i > n:
			break
		if i not in closed:
			d_up = i - s
			break
		i += 1
	i = s
	while True:
		if i < 1:
			break
		if i not in closed:
			d_down = s - i
			break
		i -= 1
	print(min(d_up, d_down))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for j in range(k + 1):
		if max(1, s - j) not in a or min(n, s + j) not in a:
			print(j)
			break


I = lambda : map(int, input().split())
for _ in range(int(input())):
	(n, s, _) = I()
	A = set(I())
	k = 0
	while True:
		if s - k > 0 and s - k not in A or (s + k <= n and s + k not in A):
			print(k)
			break
		k += 1


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def main():
	t = inin()
	for _ in range(t):
		(n, s, k) = cin()
		a = ain()
		s -= 1
		m = 100000000000000
		b = {}
		for i in a:
			b[i - 1] = 1
		x = max(0, s - 1001)
		y = min(n, s + 1001)
		for i in range(x, y):
			if i not in b:
				m = min(m, abs(i - s))
		print(m)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


from math import *
t = int(input())
while t > 0:
	a = [int(x) for x in input().split()]
	n = a[0]
	s = a[1]
	k = a[2]
	k_list = [int(x) for x in input().split()]
	for i in range(k + 1):
		if k_list.count(s + i) == 0 and s + i <= n:
			print(i)
			break
		if k_list.count(s - i) == 0 and s - i > 0:
			print(i)
			break
	t -= 1


import os
import sys
from io import BytesIO, IOBase

def solution(n, s, k, arr):
	if s not in arr:
		write(0)
		return
	arr.insert(0, 0)
	left = s - 1 if s - 1 >= 1 else None
	right = s + 1 if s + 1 <= n else None
	(res1, res2) = (float('inf'), float('inf'))
	if left:
		while left >= 1:
			if left not in arr:
				res1 = left
				break
			left -= 1
	if right:
		while right <= n:
			if right not in arr:
				res2 = right
				break
			right += 1
	write(min(abs(res1 - s), abs(res2 - s)))

def main():
	for _ in range(r_int()):
		(n, s, k) = r_array()
		arr = r_array()
		solution(n, s, k, arr)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())

def w_array(arr):
	for x in arr:
		write(x, end=' ')
	write()
main()


def solve(floors, floor, closed):
	above = below = floor
	while below > 0 or above <= floors:
		if below > 0 and below not in closed:
			return floor - below
		if above <= floors and above not in closed:
			return above - floor
		below -= 1
		above += 1
for T in range(int(input())):
	(n, s, k) = map(int, input().split())
	print(solve(n, s, set(map(int, input().split()))))


def inbounds(x, n):
	return x >= 1 and x <= n
t = int(input())
for case in range(t):
	(n, s, k) = [int(zax) for zax in input().split()]
	closed = [int(zax) for zax in input().split()]
	dic = {}
	for i in closed:
		dic[i] = True
	closest = 0
	best = 10 ** 10
	if s not in closed:
		print(0)
	else:
		for i in range(k + 1):
			if s + i not in dic and inbounds(s + i, n):
				best = min(best, i)
			if s - i not in dic and inbounds(s - i, n):
				best = min(best, i)
		print(best)


t = int(input())
for j in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 10 ** 10
	for i in range(s, n + 1):
		if i not in a:
			ans = min(ans, i - s)
			break
	for i in range(s, 0, -1):
		if i not in a:
			ans = min(ans, s - i)
			break
	print(ans)


t = int(input())
while t:
	(n, s, k) = map(int, input().split(' '))
	a = map(int, input().split(' '))
	a_s = set(a)
	fr = False
	for i in range(s, n + 1):
		if i not in a_s:
			fr = True
			break
	fl = False
	for j in range(s, 0, -1):
		if j not in a_s:
			fl = True
			break
	if fl and fr:
		print(min(i - s, s - j))
	elif fl:
		print(s - j)
	else:
		print(i - s)
	t -= 1


t = int(input())
for _ in range(t):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	dis = 0
	while dis <= n:
		if s + dis <= n:
			if s + dis not in a:
				print(dis)
				break
		if s - dis >= 0:
			if abs(s - dis) not in a and s - dis != 0:
				print(dis)
				break
		dis += 1


t = int(input())
for _ in range(t):
	(n, s, k) = list(map(int, input().rstrip().split()))
	a = list(map(int, input().rstrip().split()))
	d = {}
	for i in a:
		d[i] = 1
	ans = []
	for j in range(s, n + 1):
		if j not in d:
			ans.append(abs(j - s))
			break
	for k in range(s, 0, -1):
		if k not in d:
			ans.append(abs(k - s))
			break
	print(min(ans))


N = int(input())
for _ in range(N):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	if s not in a:
		print(0)
	else:
		pos = a.index(s)
		one = 1
		for i in range(pos, len(a) - 1):
			if a[i] + 1 == a[i + 1]:
				one = one + 1
			else:
				break
		else:
			if a[-1] == n:
				one = 1000
		two = 1
		for i in range(pos, 0, -1):
			if a[i] - 1 == a[i - 1]:
				two = two + 1
			else:
				break
		else:
			if a[0] == 1:
				two = 1000
		print(min(one, two))


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()

def sieve(x):
	a = [True] * (x + 1)
	sq = floor(sqrt(x))
	for i in range(3, sq + 1, 2):
		if a[i]:
			for j in range(i * i, x + 1, i):
				a[j] = False
	if x > 1:
		p = [2]
	else:
		p = []
	for i in range(3, x + 1, 2):
		if a[i]:
			p.append(i)
	return p
for _ in range(ii()):
	(n, s, k) = mi()
	a = li()
	a = set(a)
	for i in range(n):
		x = s - i
		y = s + i
		if x > 0 and x not in a or (y <= n and y not in a):
			print(i)
			break


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	foo = 0
	for i in range(n):
		t1 = s + foo
		t2 = s - foo
		if t1 <= n and t1 not in l or (t2 > 0 and t2 not in l):
			print(foo)
			break
		foo += 1


R = lambda : map(int, input().split())
(t,) = R()
for _ in [0] * t:
	(n, s, k) = R()
	print(min((abs(s - x) for x in {*range(max(1, s - 1001), min(n + 1, s + 1001))} - {*R()})))


def main():
	for _ in range(int(input())):
		(n, s, k) = map(int, input().split())
		m = dict.fromkeys(map(int, input().split()), True)
		stair_count = 0
		s1 = s2 = s
		while True:
			if s1 <= n and (not m.get(s1)):
				print(stair_count)
				break
			elif s2 > 0 and (not m.get(s2)):
				print(stair_count)
				break
			stair_count += 1
			s1 += 1
			s2 -= 1
main()


from itertools import count
for t in range(int(input())):
	(n, s, k) = (int(i) for i in input().split())
	a = set((int(i) for i in input().split()))
	print(next((i for i in count() if 1 <= s - i <= n and (not s - i in a) or (1 <= s + i <= n and (not s + i in a)))))


import sys
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	d = set(map(int, input().split()))
	i = 0
	while max(s - i, 1) in d and min(s + i, n) in d:
		i += 1
	print(i)


def solve():
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	m1 = 2 * 10 ** 9
	m2 = m1
	for i in range(s, n + 1):
		if i not in a:
			m1 = i - s
			break
	for i in range(s, 0, -1):
		if i not in a:
			m2 = s - i
			break
	print(min(m1, m2))
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for i in range(t):
	(n, s, k) = input().split(' ')
	(n, s, k) = (int(n), int(s), int(k))
	a = set(map(int, input().split(' ')))
	min_diff = 0
	while min_diff <= k:
		lower_f = s - min_diff
		upper_f = s + min_diff
		if lower_f >= 1 and lower_f not in a:
			print(min_diff)
			break
		if upper_f <= n and upper_f not in a:
			print(min_diff)
			break
		min_diff += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	dc = {}
	for i in a:
		dc[i] = 1
	u = 0
	d = 0
	fu = 0
	fd = 0
	curru = s
	while curru <= n:
		if curru not in dc:
			fu = 1
			break
		u += 1
		curru += 1
	currd = s
	while currd > 0:
		if currd not in dc:
			fd = 1
			break
		d += 1
		currd -= 1
	ans = 10 ** 20
	if fu:
		ans = min(ans, u)
	if fd:
		ans = min(ans, d)
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	w = set(map(int, input().split()))
	a = b = s
	while a in w and b in w:
		if a > 1:
			a -= 1
		if b < n:
			b += 1
	print(b - s if a in w else s - a if b in w else min(s - a, b - s))


t = int(input())
ans = []
for _ in range(t):
	(n, s, k) = map(int, input().split())
	klist = set(map(int, input().split()))
	for t in range(n):
		if s + t not in klist and 1 <= s + t <= n or (1 <= s - t <= n and s - t not in klist):
			ans.append(t)
			break
		else:
			continue
print(*ans, sep='\n')


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	c = list(map(int, input().split()))
	if s not in c:
		print(0)
	else:
		r = s + 1
		u = 0
		while r <= n:
			if r not in c:
				m1 = r - s
				u = 1
				break
			r += 1
		r1 = s - 1
		d = 0
		while r1 >= 1:
			if r1 not in c:
				m2 = s - r1
				d = 1
				break
			r1 -= 1
		if d == 0:
			print(m1)
		elif u == 0:
			print(m2)
		else:
			print(min(m1, m2))


from bisect import bisect_left
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = sorted(map(int, input().split()))
	pos = bisect_left(a, s)
	if pos >= k or a[pos] != s:
		print(0)
	else:
		pd = pos
		while pd > 0 and a[pd] == a[pd - 1] + 1:
			pd -= 1
		if a[pd] > 1:
			result = s - a[pd] + 1
		else:
			result = n
		pu = pos
		while pu < k - 1 and a[pu] == a[pu + 1] - 1:
			pu += 1
		if a[pu] < n:
			result = min(result, a[pu] + 1 - s)
		print(result)


from collections import defaultdict
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = defaultdict(int)
	for i in a:
		d[i] = 1
	mn = 2000000
	for i in range(max(1, s - 2000), min(n + 1, s + 2100)):
		if d[i] != 1:
			mn = min(mn, abs(s - i))
	print(mn)


t = int(input())
for q in range(t):
	(n, s, k) = map(int, input().split())
	a = [int(s) for s in input().split()]
	i = 0
	while True:
		if s + i < n + 1 and s + i not in a:
			print(i)
			break
		elif s - i > 0 and s - i not in a:
			print(i)
			break
		i += 1


for _ in range(int(input())):
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	chk = True
	l = 1
	if s not in a:
		print(0)
	else:
		while chk:
			s1 = s + l
			s2 = s - l
			if s1 not in a and s1 <= n:
				print(l)
				chk = False
			elif s2 not in a and s2 > 0:
				print(l)
				chk = False
			else:
				l += 1


t = int(input())
for i in range(0, t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	j = s
	ans = -1
	while j <= min(n, s + k):
		if not j in arr:
			ans = j
			break
		j += 1
	j = s
	ans2 = -1
	while j > 0:
		if not j in arr:
			ans2 = j
			break
		j -= 1
	if ans == -1:
		print(s - ans2)
		continue
	if ans2 == -1:
		print(ans - s)
		continue
	if s - ans2 < ans - s:
		print(s - ans2)
	else:
		print(ans - s)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	(n, s, k) = minput()
	a = linput()
	for i in range(k + 1):
		if s + i <= n and (not s + i in a):
			print(i)
			break
		if s - i >= 1 and (not s - i in a):
			print(i)
			break


test_cases = int(input())
for i in range(test_cases):
	(n, s, k) = [int(s) for s in input().split(' ')]
	k_string = [int(s) for s in input().split(' ')]
	count = 1
	tmp_s = s
	x = 0
	while True:
		tmp_s = tmp_s + count * x
		count = count * -1
		x = x + 1
		if tmp_s not in k_string and tmp_s >= 1 and (tmp_s <= n):
			print(abs(tmp_s - s))
			break


t = int(input())
for _ in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	a2 = set(a)
	ans = 0
	up = s
	down = s
	while True:
		if up <= n and up not in a2 or (down >= 1 and down not in a2):
			break
		else:
			ans += 1
			up += 1
			down -= 1
	print(ans)


import math
N_MAX = pow(10, 9)

def calc_step(case):
	up_cur = down_cur = case['s']
	while up_cur <= case['n'] or down_cur >= 1:
		if up_cur <= case['n']:
			if up_cur not in case['ck']:
				return up_cur - case['s']
			up_cur += 1
		if down_cur >= 1:
			if down_cur not in case['ck']:
				return case['s'] - down_cur
			down_cur -= 1
	return -1

def parse():
	test_case_num = int(input())
	cases = []
	if test_case_num > 1000:
		return cases
	for i in range(test_case_num):
		data = {}
		case_line_one = input()
		(n, s, k) = case_line_one.split(' ')
		n = int(n)
		s = int(s)
		k = int(k)
		if n < 2 or n > N_MAX or s > n or (s < 1) or (k < 1) or (k > min(n - 1, 1000)):
			return []
		case_line_two = input()
		closed_k = case_line_two.split(' ')
		closed_k = list(map(int, closed_k))
		for j in closed_k:
			if j > n or j < 1:
				return []
		data = {'n': n, 's': s, 'k': k, 'ck': set(closed_k)}
		cases.append(data)
	return cases
cases = parse()
for case in cases:
	print(calc_step(case))


vis = [False] * (2001 + 3)
t = int(input())
while t:
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 1005
	i = 0
	j = 0
	length = len(a)
	n = min(n + 1, s + 1001)
	flag = True
	for i in range(s, n):
		flag = True
		for j in range(0, length):
			if a[j] == i:
				flag = False
				break
		if flag:
			ans = min(i - s, ans)
			break
	k = max(0, k - 1002)
	for i in range(s, k, -1):
		flag = True
		for j in range(0, length):
			if a[j] == i:
				flag = False
				break
		if flag:
			ans = min(s - i, ans)
			break
	print(ans)
	t -= 1


t = int(input())
while t > 0:
	(n, s, k) = [int(ele) for ele in input().split()]
	lst = [int(ele) for ele in input().split()]
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in lst:
			print(i)
			break
		if s + i <= n and s + i not in lst:
			print(i)
			break
	t -= 1


import sys
r_input = sys.stdin.readline
t = int(r_input())
for _ in range(t):
	(n, s, k) = map(int, r_input().split())
	closed = list(map(int, r_input().split()))
	result = 0
	i = 0
	while True:
		a = s - i
		b = s + i
		flag = 0
		if a > 0:
			if a not in closed:
				print(result)
				break
		else:
			flag += 1
		if b <= n:
			if b not in closed:
				print(result)
				break
		else:
			flag += 1
		if flag == 2:
			print(0)
			break
		result += 1
		i += 1


t = int(input())
inputs = [(list(map(int, input().split(' '))), list(map(int, input().split(' ')))) for i in range(t)]
res = []
for i in range(len(inputs)):
	for j in range(0, inputs[i][0][2] + 1):
		if inputs[i][0][1] + j <= inputs[i][0][0] and (not inputs[i][0][1] + j in inputs[i][1]):
			print(j)
			break
		if inputs[i][0][1] - j >= 1 and (not inputs[i][0][1] - j in inputs[i][1]):
			print(j)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	m = 0
	M = 0
	if s not in a:
		print(0)
	else:
		for i in range(s, 0, -1):
			if i not in a:
				m = abs(s - i)
				break
		for i in range(s + 1, n + 1):
			if i not in a:
				M = abs(s - i)
				break
		if m == 0:
			print(M)
		elif M == 0:
			print(m)
		else:
			print(min(m, M))


t = int(input())
while t:
	t -= 1
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = set(a)
	x = float('inf')
	c = s
	if s not in a:
		print(0)
		continue
	while c > 1:
		if c - 1 in a:
			c -= 1
			continue
		x = min(x, abs(s - c + 1))
		break
	c = s
	while c < n:
		if c + 1 in a:
			c += 1
			continue
		x = min(x, abs(s - c - 1))
		break
	print(x)


import sys
t = int(sys.stdin.readline().rstrip())
for _ in range(t):
	(n, s, k) = list(map(int, sys.stdin.readline().rstrip().split(' ')))
	closed = set(map(int, sys.stdin.readline().rstrip().split(' ')))
	ss_u = 0
	ss_d = 0
	queue_u = [s]
	queue_d = [s]
	while True:
		if len(queue_u):
			temp = queue_u.pop()
			if temp not in closed:
				print(ss_u)
				break
			else:
				ss_u += 1
				if temp + 1 <= n:
					queue_u.append(temp + 1)
		if len(queue_d):
			temp = queue_d.pop()
			if temp not in closed:
				print(ss_d)
				break
			else:
				ss_d += 1
				if temp - 1 >= 1:
					queue_d.append(temp - 1)


def search(a, e):
	l = 0
	r = len(a) - 1
	x = (l + r) // 2
	while r - l > 1:
		if a[x] <= e:
			l = x
		else:
			r = x
		x = (l + r) // 2
	try:
		return (a[x], a[x + 1])
	except:
		return (a[x], a[x])

def main():
	for _ in range(int(input())):
		(n, s, k) = list(map(int, input().split()))
		a = set(map(int, input().split()))
		b = set(range(max(s - 1000, 1), min(s + 1000, n) + 1))
		c = b - a
		c = list(c)
		c.sort()
		t = search(c, s)
		print(min(abs(s - t[0]), abs(s - t[1])))
main()


t = int(input())
for _ in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	a = input().split()
	res = 0
	for x in range(0, k):
		l = int(s) - x
		r = int(s) + x
		if l in range(1, n + 1):
			if str(l) not in a:
				break
		if r in range(1, n + 1):
			if str(r) not in a:
				break
		res = res + 1
	print(res)


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	c = 0
	if s not in l:
		print('0')
	else:
		for i in range(0, k + 1):
			if s - i >= 1 and s - i not in l:
				print(i)
				break
			elif s + i <= n and s + i not in l:
				print(i)
				break


import sys
t = int(input())
for i in range(t):
	mine1 = sys.maxsize
	mine2 = sys.maxsize
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for i in range(s, 0, -1):
		if i not in arr:
			mine1 = s - i
			break
	for i in range(s + 1, n + 1):
		if i not in arr:
			mine2 = i - s
			break
	print(min(mine1, mine2))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	seti = set(l)
	ans = inf
	for i in range(s, min(s + 1001, n + 1)):
		if i not in seti:
			ans = min(abs(s - i), ans)
	for i in range(max(1, s - 1001), s + 1):
		if i not in seti:
			ans = min(abs(s - i), ans)
	print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	(grab_right, grab_left) = (100000000000.0, 100000000000.0)
	for i in range(s, n + 1):
		if i not in closed:
			grab_right = i
			break
	for i in range(s, 0, -1):
		if i not in closed:
			grab_left = i
			break
	print(min(abs(grab_left - s), abs(grab_right - s)))


from collections import defaultdict as dc
from heapq import *
import math
import bisect
from collections import deque as dq

def inp():
	p = int(input())
	return p

def line():
	p = list(map(int, input().split()))
	return p

def check(n, d, x):
	if x < 0:
		return 0
	return n >= x + math.ceil(d / (x + 1))
for _ in range(inp()):
	(n, s, k) = line()
	a = line()
	p = dc(int)
	for i in range(k):
		p[a[i]] = 1
	ans = float('inf')
	for i in range(s, n + 1, 1):
		if p[i] == 0:
			ans = min(ans, i - s)
			break
	for j in range(s, 0, -1):
		if p[j] == 0:
			ans = min(ans, s - j)
			break
	print(ans)


t = int(input())
for test in range(t):
	(n, s, k) = map(int, input().split())
	a = set()
	values = list(map(int, input().split()))
	for value in values:
		a.add(value)
	(res, up, down) = (n, s, s)
	while True:
		if up <= n and up not in a:
			res = up - s
			break
		if down >= 1 and down not in a:
			res = s - down
			break
		down -= 1
		up += 1
	print(res)


q = int(input())
for i in range(q):
	a = 10 ** 9
	b = 10 ** 9
	(n, s, k) = map(int, input().split())
	k_list = list(map(int, input().split()))
	if k_list.__contains__(s) == False:
		print(0)
	else:
		for j in range(1, n - s + 1):
			if k_list.__contains__(s + j) == False:
				a = j
				break
		for d in range(1, s + 1):
			if s - d <= 0:
				break
			if k_list.__contains__(s - d) == False:
				b = d
				break
		print(min(a, b))


num = int(input())
mas1 = [None] * num
mas2 = [None] * num
for i in range(num):
	mas1[i] = list(map(int, input().split()))
	mas2[i] = sorted(list(map(int, input().split())))
for i in range(num):
	cur1 = mas1[i]
	cur2 = mas2[i]
	a = cur1[1]
	b = a
	flag = True
	while flag:
		if a <= cur1[0] and a not in cur2 or (b >= 1 and b not in cur2):
			flag = False
			a -= 1
			b += 1
		a += 1
		b -= 1
	print(abs(cur1[1] - b))


t = int(input())
for j in range(t):
	(N, floor, wrong) = map(int, input().split())
	x = list(map(int, input().split()))
	for i in range(0, wrong + 1):
		if floor - i >= 1 and (not floor - i in x):
			print(i)
			break
		if floor + i <= N and (not floor + i in x):
			print(i)
			break


test = int(input())
for tes in range(test):
	(n, s, k) = list(map(int, input().split()))
	L = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s + i <= n and s + i not in L:
			print(i)
			break
		if s - i >= 1 and s - i not in L:
			print(i)
			break


from sys import stdin, maxsize
t = int(stdin.readline().strip())
for _ in range(t):
	(n, s, k) = map(int, stdin.readline().split())
	a = list(map(int, stdin.readline().split()))
	res = maxsize
	for i in range(n):
		if s + i <= n and s + i not in a:
			res = min(res, i)
			break
		if s - i > 0 and s - i not in a:
			res = min(res, i)
			break
	print(res)


def solve_case():
	global N, S, K
	(N, S, K) = map(int, input().split())
	A = set(map(int, input().split()))
	best = N
	low = max(S - K, 1)
	high = min(S + K, N)
	for floor in range(low, high + 1):
		if floor not in A:
			best = min(best, abs(S - floor))
	print(best)
tests = int(input())
for test in range(tests):
	solve_case()


import math
t = int(input())
for i in range(t):
	a = input()
	b = a.split()
	n = int(b[0])
	s = int(b[1])
	k = int(b[2])
	c = input()
	d = c.split()
	for j in range(k):
		d[j] = int(d[j])
	ans = math.inf
	nxt = s
	prev = s
	curr = s
	while True:
		kf = math.inf
		kb = math.inf
		if nxt not in d and nxt <= n:
			kf = nxt
		if prev not in d and prev > 0:
			kb = prev
		ans = min(abs(kf - s), abs(kb - s))
		if ans != math.inf:
			break
		nxt += 1
		prev -= 1
	print(ans)


def check(x):
	(l, r) = (0, k)
	while l < r:
		m = l + (r - l) // 2
		if arr[m] == x:
			return m
		elif arr[m] > x:
			r = m
		else:
			l = m + 1
	return -1
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	cnt = 0
	(d, u) = (s - cnt, s + cnt)
	while d >= 0 and u <= n:
		if check(d) == -1 or check(u) == -1:
			print(cnt)
			break
		else:
			cnt += 1
			if d > 1:
				d = s - cnt
			if u < n:
				u = s + cnt


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, s, k) = [int(item) for item in input().split()]
	a = [int(item) for item in input().split()]
	seta = set(a)
	low = max(1, s - k - 1)
	high = min(n + 1, s + k + 1)
	nearest = 10000
	for restrant in range(low, high):
		if restrant in seta:
			continue
		nearest = min(nearest, abs(restrant - s))
	print(nearest)


from operator import itemgetter
import sys
input = sys.stdin.readline
t = int(input())

def solve(n, s, k, a):
	k2 = k + 2
	for j in range(k2 + 1):
		if not ((s - j in a or s - j < 1) and (s + j in a or s + j > n)):
			print(j)
			break

def main():
	for i in range(t):
		(n, s, k) = map(int, input().split())
		a = set(map(int, input().split()))
		solve(n, s, k, a)
main()


t = int(input())
for i in range(t):
	a = list(map(int, input().split()))
	n = a[0]
	s = a[1]
	k = a[2]
	a = set(list(map(int, input().split())))
	j = 0
	while True:
		if s - j > 0 and s - j not in a:
			print(j)
			break
		if s + j <= n and s + j not in a:
			print(j)
			break
		j += 1


t = int(input())
for i in range(t):
	nsk = list(map(int, input().rstrip().split()))
	N = nsk[0]
	S = nsk[1]
	K = nsk[2]
	A = list(map(int, input().rstrip().split()))
	d = {}
	for i in A:
		d[i] = 1
	r = 0
	while 1:
		if S + r <= N and d.get(S + r, 0) == 0:
			break
		if S - r > 0 and d.get(S - r, 0) == 0:
			break
		r += 1
	print(r)


import sys
from math import *
input = sys.stdin.readline
t = int(input())
while t > 0:
	t -= 1
	(n, s, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	d = {}
	for i in a:
		d[i] = 1
	mini = 99999999999
	for i in range(s, 0, -1):
		if i != 0 and i not in d:
			mini = s - i
			break
	for i in range(s + 1, n + 1):
		if i not in d:
			mini = min(mini, i - s)
			break
	print(mini)


from sys import stdin, stdout
x = [line.rstrip() for line in stdin]
for i in range(int(x[0])):
	(n, s, k) = map(int, x[i * 2 + 1].split(' '))
	closed = list(map(int, x[i * 2 + 2].split()))
	num = 0
	while True:
		up = True
		down = True
		check = (s + num, s - num)
		for k in closed:
			if check[0] == k or check[0] > n:
				up = False
			if check[1] == k or check[1] < 1:
				down = False
		if up or down:
			print(num)
			break
		num += 1


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(n, s, k) = int_array()
	mset = set(int_array())
	x = INF
	for i in range(s, min(n + 1, s + 1100)):
		if i not in mset:
			if abs(i - s) < x:
				x = abs(i - s)
				break
	for i in range(s - 1, max(0, s - 1100), -1):
		if i not in mset:
			if abs(i - s) < x:
				x = abs(i - s)
				break
	print(x)


t = int(input())
for r in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		d[i] = 0
	ans1 = 9999999999
	ans2 = 9999999999
	for i in range(0, s):
		try:
			if d[s - i] == 0:
				pass
		except:
			ans1 = i
			break
	for i in range(0, n - s + 1):
		try:
			if d[s + i] == 0:
				pass
		except:
			ans2 = i
			break
	print(min(ans1, ans2))


def inp(dtype=str, strip=True):
	s = input()
	res = [dtype(p) for p in s.split()]
	res = res[0] if len(res) == 1 and strip else res
	return res

def problem1():

	def test(cur, steps):
		if 0 < cur <= n and cur not in a:
			return steps
		else:
			return -1
	t = inp(int)
	for _ in range(t):
		(n, s, k) = inp(int)
		a = inp(int, strip=False)
		a = set(a)
		res = -1
		steps = 0
		while res < 0:
			res = test(s + steps, steps)
			if res < 0:
				res = test(s - steps, steps)
			steps += 1
		print(res)

def problem2():
	pass

def problem3():
	pass

def problem4():
	pass

def problem5():
	pass
problem1()


T = int(input())
for i in range(0, T):
	l = list(map(int, input().split()))
	(n, s, k) = (l[0], l[1], l[2])
	closed = list(map(int, input().split()))
	flag = False
	ld = 10000000000
	rd = 10000000000
	if s not in closed:
		print(0)
		continue
	l = s - 1
	r = s + 1
	while not flag:
		if l not in closed and l > 0:
			ld = abs(l - s)
			flag = True
		if r not in closed and r <= n:
			rd = abs(r - s)
			flag = True
		l -= 1
		r += 1
	print(min(ld, rd))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = [*map(int, input().split())]
	dic = set(l)
	cnt = 0
	flag1 = False
	for i in range(s, n + 1):
		if i not in dic:
			flag1 = True
			break
		else:
			cnt += 1
	cnt1 = 0
	flag = False
	for i in range(s, 0, -1):
		if i not in dic:
			flag = True
			break
		else:
			cnt1 += 1
	if flag and flag1:
		print(min(cnt, cnt1))
	elif flag1:
		print(cnt)
	else:
		print(cnt1)


from collections import defaultdict
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	close = list(map(int, input().split()))
	out = 10 ** 10
	d = defaultdict(bool)
	for i in close:
		d[i] = True
	left = s
	right = s
	while right <= n or left > 0:
		if left > 0 and (not d[left]):
			out = abs(s - left)
			break
		if right <= n and (not d[right]):
			out = abs(s - right)
			break
		left = left - 1
		right = right + 1
	print(out)


def solve():
	(n, s, k) = map(int, input().split())
	arr = set(map(int, input().split()))
	if s not in arr:
		return 0
	else:
		mn = float('inf')
		for i in range(s + 1, n + 1):
			if i not in arr:
				mn = abs(i - s)
				break
		for j in range(s - 1, 0, -1):
			if j < 1:
				break
			elif j not in arr:
				mn = min(mn, abs(j - s))
				break
		return mn
for _ in range(int(input())):
	print(solve())


t = int(input())
while t:
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = 10000000000000000
	l.sort()
	ind = -1
	for i in range(k):
		if l[i] == s:
			ind = i
	if ind == -1:
		print(0)
	else:
		res = 100000000000000000
		for j in range(ind, k):
			if j + 1 < k and l[j + 1] - l[j] != 1:
				res = min(res, l[j] + 1 - s)
				break
		for j in range(ind, -1, -1):
			if j - 1 >= 0 and l[j] - l[j - 1] != 1:
				res = min(res, s - (l[j] - 1))
				break
		if l[-1] != n and l[0] != 1:
			res = min(res, abs(s - (l[0] - 1)), abs(l[-1] + 1 - s))
		elif l[-1] != n:
			res = min(res, abs(l[-1] + 1 - s))
		elif l[0] != 1:
			res = min(res, abs(s - (l[0] - 1)))
		print(res)
	t -= 1


def lb(c, e):
	l = 0
	r = len(c) - 1
	ans = -1
	while l <= r:
		mid = (l + r) // 2
		if c[mid] < e:
			l = mid + 1
		else:
			ans = mid
			r = mid - 1
	return ans

def ub(c, e):
	l = 0
	r = len(c) - 1
	ans = -1
	while l <= r:
		mid = (l + r) // 2
		if c[mid] > e:
			r = mid - 1
		else:
			ans = mid
			l = mid + 1
	return ans
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = set(a)
	if s not in a:
		print(0)
		continue
	a = set(a)
	l = s
	ct1 = 0
	while l > 1 and l in a:
		l -= 1
		ct1 += 1
	if l in a:
		ct1 = float('inf')
	r = s
	ct2 = 0
	while r < n and r in a:
		r += 1
		ct2 += 1
	if r in a:
		ct2 = float('inf')
	print(min(ct1, ct2))


t = int(input())
for i in range(0, t):
	min1 = 200000000
	min2 = 200000000
	(n, s, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if s not in a:
		print(0)
	else:
		for j in range(s + 1, n + 1):
			if j not in a:
				min1 = j - s
				break
		for p in range(s - 1, 0, -1):
			if p not in a:
				min2 = s - p
				break
		print(min(min1, min2))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	k = 0
	while 1:
		if s - k > 0 and s - k not in lst or (s + k <= n and s + k not in lst):
			print(k)
			break
		k += 1


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	al = list(map(int, input().split()))
	from collections import defaultdict
	dic = defaultdict(int)
	for a in al:
		dic[a] = 1
	for j in range(1001):
		if s + j <= n:
			if dic[s + j] == 0:
				print(j)
				break
		if s - j >= 1:
			if dic[s - j] == 0:
				print(j)
				break


from bisect import bisect_right as br, bisect_left as bl
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	ans1 = 0
	a = set(a)
	i = 0
	fl1 = 0
	while True:
		if s + i in a and s + i <= n:
			ans1 += 1
			i += 1
		else:
			if s + i <= n:
				fl1 = 1
			break
	ans2 = 0
	i = 0
	fl2 = 0
	while True:
		if s - i in a and s - i > 0:
			ans2 += 1
			i += 1
		else:
			if s - i > 0:
				fl2 = 1
			break
	if fl1 and fl2:
		print(min(ans1, ans2))
	elif fl1:
		print(ans1)
	else:
		print(ans2)


import math
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	band = set(map(int, input().split()))
	i = 0
	count = 0
	while s + i <= n or s - i > 0:
		if s + i <= n and s + i not in band:
			break
		if s - i > 0 and s - i not in band:
			break
		count += 1
		i += 1
	print(count)


t = int(input())
for tc in range(t):
	(n, s, k) = map(int, input().split())
	blocked = [int(z) for z in input().split()]
	for d in range(k + 1):
		if max(s - d, 1) not in blocked or min(s + d, n) not in blocked:
			print(d)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	A = set([*map(int, input().split())])
	A.add(0)
	A.add(n + 1)
	if not s in A:
		print(0)
	else:
		(a, b) = (s - 1, s + 1)
		while a in A and b in A:
			if a > 1:
				a -= 1
			if b < n:
				b += 1
		if not a in A:
			print(s - a)
		else:
			print(b - s)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	closed = set()
	for f in input().split():
		closed.add(int(f))
	is_closed = True
	for i in range(n):
		(up, down) = (s + i, s - i)
		if up in range(1, n + 1):
			is_closed = up in closed
		if down in range(1, n + 1):
			is_closed = is_closed and down in closed
		if not is_closed:
			print(i)
			break


t = int(input())
for h in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in a):
			print(i)
			break
		elif s + i <= n and (not s + i in a):
			print(i)
			break
	else:
		assert False


from pprint import pprint

def dp(s):
	if True:
		print(s)

def dpp(s):
	if True:
		pprint(s)
q = int(input())
for qq in range(q):
	res = -1
	(n, s, k) = map(int, input().split())
	dat = list(map(int, input().split()))
	res = 0
	while True:
		if s + res not in dat and s + res <= n:
			break
		if s - res not in dat and s - res >= 1:
			break
		res += 1
	print(res)


import math
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split(' '))
	closed = set(map(int, input().split(' ')))
	res = 0
	while (s + res > n or s + res in closed) and (s - res < 1 or s - res in closed):
		res += 1
	print(res)


from collections import Counter
for ti in range(int(input())):
	(n, s, k) = [int(x) for x in input().split()]
	kd = Counter([int(x) for x in input().split()])
	for fi in range(n):
		if s - fi >= 1 and (not kd.get(s - fi, 0)):
			print(fi)
			break
		if s + fi <= n and (not kd.get(s + fi, 0)):
			print(fi)
			break


import math
li = []
for _ in range(int(input())):
	(n, s, k) = list(map(int, input().rstrip().split()))
	a = list(map(int, input().rstrip().split()))
	for i in range(n):
		if s + i <= n and s + i not in a:
			print(i)
			break
		elif s - i > 0 and s - i not in a:
			print(i)
			break


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	A = [int(i) for i in input().split()]
	As = set(A)
	if s not in As:
		print(0)
	else:
		fl = True
		counter = 0
		while fl:
			counter += 1
			down = s - counter
			up = s + counter
			if down >= 1 and down not in As:
				fl = False
			elif up <= n and up not in As:
				fl = False
		print(counter)


def binarySearch(a):
	if K[0] == a or K[-1] == a:
		return True
	if K[0] > a or K[-1] < a:
		return False
	l = 0
	r = k - 1
	while l <= r:
		mid = l + (r - l) // 2
		if K[mid] == a:
			return True
		elif K[mid] < a:
			l = mid + 1
		else:
			r = mid - 1
	return False
for tests in range(int(input())):
	(n, s, k) = map(int, input().split())
	K = list(map(int, input().split()))
	K.sort()
	minF = n + 1
	for j in range(n):
		if s + j - 1 < n:
			if not binarySearch(s + j):
				minF = j
				break
		if s - j - 1 >= 0:
			if not binarySearch(s - j):
				minF = j
				break
		if s - j - 1 < 0 and s + j - 1 >= n:
			break
	print(minF)


tcases = int(input())
for i in range(tcases):
	x = [int(x) for x in input().split(' ')]
	(n, s, k) = x
	a = set([int(x) for x in input().split(' ')])
	i = 0
	s1 = s2 = s
	while True:
		if s + i <= n:
			s1 = s + i
			if s1 not in a:
				print(i)
				break
		if s - i >= 1:
			s2 = s - i
			if s2 not in a:
				print(i)
				break
		i += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	x1 = s
	x2 = s
	f = 0
	if (s in l) == False:
		print(0)
	else:
		while f == 0:
			x1 = x1 - 1
			x2 = x2 + 1
			if (x1 in l) == False:
				if x1 > 0:
					f = 1
			if (x2 in l) == False:
				if x2 <= n:
					f = 1
		d1 = abs(x2 - s)
		d2 = abs(x1 - s)
		print(max(d1, d2))


for nt in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	if s not in l:
		print(0)
		continue
	else:
		ind = l.index(s)
		if l[-1] == n:
			ans1 = 10000000000000000
		else:
			ans1 = l[-1] + 1
		for i in range(ind + 1, k):
			if l[i] != l[i - 1] + 1:
				ans1 = l[i - 1] + 1
				break
		if l[0] == 1:
			ans2 = 10000000000000000
		else:
			ans2 = l[0] - 1
		for i in range(ind - 1, -1, -1):
			if l[i] != l[i + 1] - 1:
				ans2 = l[i + 1] - 1
				break
		print(min(abs(ans2 - s), abs(ans1 - s)))


def check():
	[n, s, k] = list(map(int, input().split()))
	closedList = list(map(int, input().split()))
	for j in range(n):
		if s + j not in closedList and s + j <= n:
			print(j)
			return
		if s - j > 0 and s - j not in closedList:
			print(j)
			return
testcase = int(input())
for i in range(testcase):
	check()


t = int(input())
lst = []
for i in range(0, t):
	closed = []
	line = input()
	line = line.split()
	n = int(line[0])
	s = int(line[1])
	k = int(line[2])
	line = input()
	line = line.split()
	for i in range(0, k):
		closed.append(int(line[i]))
	lower = 0
	upper = 0
	flag = 0
	if s not in closed:
		lst.append(0)
	else:
		for j in range(s - 1, 0, -1):
			lower = lower + 1
			if j not in closed:
				flag = 1
				break
		for j in range(s + 1, n + 1):
			upper = upper + 1
			if j not in closed:
				if flag == 1:
					flag = 3
				else:
					flag = 2
				break
		if flag == 2:
			lower = upper
		elif flag == 3:
			lower = min(lower, upper)
		lst.append(lower)
for k in lst:
	print(k)


import copy
import sys
from collections import defaultdict, Counter

def input():
	return sys.stdin.readline()

def mapi():
	return map(int, input().split())

def maps():
	return map(str, input().split())

def print(arg, *argv, end=None):
	sys.stdout.write(str(arg))
	for i in argv:
		sys.stdout.write(' ' + str(i))
	sys.stdout.write(end) if end else sys.stdout.write('\n')

def GCD(x, y):
	return GCD(y, x % y) if y else x

def modPow(x, y, p):
	(res, x) = (1, x % p)
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		(y, x) = (y >> 1, x * x % p)
	return res

def modInv(s, mod):
	return modPow(s, mod - 2, mod)

def solve():
	t = 1
	t = int(input())
	while t:
		t -= 1
		(n, s, k) = mapi()
		a = list(mapi())
		cnt = 0
		res = float('inf')
		for i in range(s, n + 1):
			if i not in a:
				res = min(res, i - s)
				break
		for i in range(s, 0, -1):
			if i not in a:
				res = min(res, s - i)
				break
			if i - s > res:
				break
		print(res)
solve()


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().strip().split()))
	a = list(map(int, input().strip().split()))
	d = {}
	for item in a:
		d[item] = 1
	i = 0
	while i < 1001:
		if s - i >= 1 and s - i not in d:
			print(i)
			break
		elif s + i <= n and s + i not in d:
			print(i)
			break
		i += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	dic = {}
	dic[0] = True
	for i in arr:
		dic[i] = True
	(a, b) = (n + 1, n + 1)
	for i in range(s, n + 1):
		if i not in dic:
			a = i - s
			break
	for i in range(s, -1, -1):
		if i not in dic:
			b = s - i
			break
	print(min(a, b))


def solve(n, s, k, closed):
	counter = 0
	while True:
		_prev = s - counter if s - counter >= 1 else 1
		_next = s + counter if s + counter <= n else n
		if _prev not in closed or _next not in closed:
			print(counter)
			break
		counter += 1
cases = int(input())
for i in range(cases):
	(n, s, k) = [int(x) for x in input().split()]
	closed = [int(x) for x in input().split()]
	solve(n, s, k, closed)


import sys
t = int(input())
while t:
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	j = 0
	for i in range(k):
		if s >= l[i]:
			j = i
	(ans1, ans2) = (0, 0)
	(ch1, ch2) = (0, 0)
	s1 = s
	for i1 in range(j, -1, -1):
		if l[i1] != s1:
			ch1 = 1
			break
		elif i1 == 0 and l[i1] != 1:
			ans1 += 1
			ch1 = 1
			break
		else:
			s1 -= 1
			ans1 += 1
	for i1 in range(j, k):
		if l[i1] != s:
			ch2 = 1
			break
		elif i1 == k - 1 and l[i1] != n:
			ch2 = 1
			ans2 += 1
			break
		else:
			s += 1
			ans2 += 1
	if ch1 == 0:
		ans1 = n
	if ch2 == 0:
		ans2 = n
	print(min(ans1, ans2))
	t -= 1


test = int(input())
for i in range(test):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	for j in range(n):
		k1 = s + j
		if k1 > n:
			k1 = n
		k2 = s - j
		if k2 < 1:
			k2 = 1
		if k1 not in closed or k2 not in closed:
			break
	print(j)


from collections import defaultdict as dd
import sys
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	m = dd(int)
	a.sort()
	for i in range(k):
		m[a[i]] += 1
	(minr, minl) = (10 ** 10, 10 ** 10)
	for i in range(s, n + 1):
		if m[i] == 0:
			minr = i - s
			break
	for i in range(s, 0, -1):
		if m[i] == 0:
			minl = s - i
			break
	print(min(minr, minl))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	ans1 = 0
	a = set(a)
	i = 0
	fl1 = 0
	while True:
		if s + i in a and s + i <= n:
			ans1 += 1
			i += 1
		else:
			if s + i <= n:
				fl1 = 1
			break
	ans2 = 0
	i = 0
	fl2 = 0
	while True:
		if s - i in a and s - i > 0:
			ans2 += 1
			i += 1
		else:
			if s - i > 0:
				fl2 = 1
			break
	if fl1 and fl2:
		print(min(ans1, ans2))
	elif fl1:
		print(ans1)
	else:
		print(ans2)


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	omit = list(map(int, input().split()))
	for j in range(0, n + 2):
		if j + s <= n and j + s not in omit:
			print(j)
			break
		if j < s and s - j not in omit:
			print(j)
			break


a = int(input())
r = 0
d = list()
while r < a:
	k = 0
	b = [int(b) for b in input().split()]
	c = [int(c) for c in input().split()]
	if b[1] not in c:
		k = 0
	else:
		t = 1
		while t <= min(max(b[1], b[0] - b[1]), 1005):
			if b[1] + t not in c and b[1] + t < b[0] + 1:
				k = k + 1
				break
			elif b[1] - t not in c and b[1] - t > 0:
				k = k + 1
				break
			else:
				k = k + 1
			t = t + 1
	d.append(k)
	r = r + 1
r = 0
while r < a:
	print(d[r])
	r = r + 1


import sys, itertools, math
T = int(sys.stdin.readline())
for _ in range(T):
	(n, s, k) = [int(i) for i in sys.stdin.readline().strip().split(' ')]
	C = set([int(i) for i in sys.stdin.readline().strip().split(' ')])
	for d in range(1005):
		nd = s + d
		if nd <= n and nd not in C:
			print(d)
			break
		nd = s - d
		if nd >= 1 and nd not in C:
			print(d)
			break


for _ in range(int(input())):
	(n, a, k) = map(int, input().split())
	s = set(list(map(int, input().split())))
	ans = 0
	m = 0
	m = a
	while True:
		if a not in s or m not in s:
			break
		a = a + 1
		m = m - 1
		if m == 0:
			m = 1
		if a == n + 1:
			a = n
		ans += 1
	print(ans)


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	(N, S, K) = map(int, input().split())
	A = set(map(int, input().split()))
	Query.append((N, K, S, A))
for (N, K, S, A) in Query:
	c = 0
	while True:
		if not S + c in A and S + c <= N or (S - c > 0 and (not S - c in A)):
			break
		c += 1
	print(c)


import sys
import math
from collections import defaultdict, Counter
import bisect
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	for j in range(k + 1):
		if s - j not in a and s - j > 0 or (s + j not in a and s + j <= n):
			print(j)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a = set(a)
	for i in range(0, 1005):
		if 1 <= s + i <= n:
			if s + i not in a:
				print(i)
				break
		if 1 <= s - i <= n:
			if s - i not in a:
				print(i)
				break


t = int(input())
for query in range(t):
	nsk = input().split()
	n = int(nsk[0])
	s = int(nsk[1])
	k = int(nsk[2])
	closed2 = []
	closed = input().split()
	mylist = []
	for floor in range(k):
		closed[floor] = int(closed[floor])
	closed = sorted(closed)
	if closed.count(s) == 0:
		print(0)
	else:
		a = closed.index(s)
		for floor in range(a + 1, k):
			if closed[floor] != closed[floor - 1] + 1:
				closed[floor] = 0
		for floor in range(a - 1, -1, -1):
			if closed[floor] != closed[floor + 1] - 1:
				closed[floor] = 0
		for floor in range(k):
			if closed[floor] != 0:
				closed2.append(closed[floor])
		b = closed2.index(s)
		c = len(closed2)
		hi = False
		bye = False
		if closed2[0] != 1:
			hi = True
		if closed2[c - 1] != n:
			bye = True
		if b <= c - b - 1 and hi:
			print(b + 1)
		elif b >= c - b - 1 and bye:
			print(c - b)
		elif hi:
			print(b + 1)
		else:
			print(c - b)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	ss = map(int, input().split())
	ss = list(ss)
	R = s
	while R < n + 1:
		if R in ss:
			R = R + 1
		else:
			break
	L = s
	while L > 0:
		if L in ss:
			L = L - 1
		else:
			break
	if L < 1 and R > n:
		print(0)
	elif L < 1:
		print(R - s)
	elif R > n:
		print(s - L)
	else:
		print(min(s - L, R - s))


t = int(input())
while t:
	(n, s, k) = map(int, input().split())
	a = input().split()
	for i in range(k):
		a[i] = int(a[i])
	f1 = False
	f2 = False
	for i in range(s, n + 1):
		if i not in a:
			f1 = True
			break
	j = s
	while j:
		if j not in a:
			f2 = True
			break
		j -= 1
	if f1 and f2:
		print(min(i - s, s - j))
	elif f1:
		print(i - s)
	else:
		print(s - j)
	t -= 1


import operator as op
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import reduce
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def def_value():
	return 0

def time_lagbe(n, k, t):
	if n == 0:
		return 0
	if k > n:
		return t
	time1 = n // k * t + n % k * t
	return time1
t = inp()
for _ in range(t):
	(n, s, k) = invr()
	mark = defaultdict(def_value)
	colsed = inlist()
	for f in colsed:
		mark[f] = 1
	l = s
	r = s
	dist = 0
	while True:
		if l > 0 and mark[l] == 0:
			dist = abs(s - l)
			break
		if r <= n and mark[r] == 0:
			dist = abs(r - s)
			break
		l -= 1
		r += 1
	print(dist)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	A = set(map(int, input().split()))
	for i in range(1005):
		if s + i in A or s + i > n:
			True
		else:
			break
		if s - i in A or s - i < 1:
			True
		else:
			break
	print(i)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	f1 = 10 ** 10
	f2 = 10 ** 10
	for i in range(s, 0, -1):
		if i not in a:
			f1 = i
			break
	for i in range(s, n + 1):
		if i not in a:
			f2 = i
			break
	print(min(abs(f1 - s), abs(f2 - s)))


m = int(input())
for mm in range(m):
	(n, s, k) = map(int, input().split())
	closed_rest = set(map(int, input().split()))
	if s not in closed_rest:
		print(0)
	else:
		(r, l) = (s + 1, s - 1)
		while l > 0 or r <= n:
			if r not in closed_rest and r <= n:
				print(abs(s - r))
				break
			elif l not in closed_rest and l > 0:
				print(abs(s - l))
				break
			else:
				r += 1
				l -= 1


import sys
for _ in range(int(sys.stdin.readline())):
	(n, s, k) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	d = {}
	for i in a:
		d[i] = 1
	if d.get(s) == None:
		print(0)
	else:
		ans = 0
		for j in range(1, n + 1):
			(c, e) = (s - j, s + j)
			if c > 0 and d.get(c) == None:
				ans = j
				break
			if e <= n and d.get(e) == None:
				ans = j
				break
		print(ans)


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = dict()
	for i in map(int, input().split()):
		arr[i] = True
	i = 0
	while (s + i > n or s + i in arr) and (s - i < 1 or s - i in arr):
		i += 1
	print(i)


def Input():
	tem = input().split()
	ans = []
	for it in tem:
		ans.append(int(it))
	return ans
from collections import Counter
T = Input()[0]
for t in range(T):
	ma = set()
	(n, s, k) = Input()
	a = Input()
	for i in range(k):
		ma.add(a[i])
	if s not in ma:
		print(0)
		continue
	ans = 1
	(l, r) = (s - 1, s + 1)
	while True:
		if l > 0 and l not in ma or (r <= n and r not in ma):
			break
		ans += 1
		l -= 1
		r += 1
	print(ans)


import sys
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	l = list(map(int, sys.stdin.readline().split()))
	found = False
	p = -1
	while not found:
		p += 1
		(f1, f2) = (False, False)
		if s - p <= 0:
			f2 = True
		if s + p > n:
			f1 = True
		if s + p in l:
			f1 = True
		if s - p in l:
			f2 = True
		if not f1 or not f2:
			print(p)
			found = True


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	r = list(map(int, input().split()))
	acs = s
	c = 0
	while acs in r:
		acs -= 1
		c += 1
	if acs == 0:
		down = n
	else:
		down = c
	acs = s
	c = 0
	while acs in r:
		acs += 1
		c += 1
	if acs == n + 1:
		up = n
	else:
		up = c
	print(min(up, down))


n = int(input())
for _ in range(n):
	(n, s, k) = list(map(int, input().split(' ')))
	cls = list(map(int, input().split(' ')))
	i = 0
	while True:
		right = s + i
		if right <= n and right not in cls:
			print(i)
			break
		left = s - i
		if left > 0 and left not in cls:
			print(i)
			break
		i += 1


t = int(input(''))
rezultati = [0] * t
ponovitve = 0
a = [0] * 1000
n = 0
s = 0
k = 0
k1 = 0
k0 = 0
while ponovitve < t:
	info = input('')
	infoArray = info.split()
	n = int(infoArray[0])
	s = int(infoArray[1])
	k = int(infoArray[2])
	k1 = k - 1
	info = input('')
	infoArray = info.split()
	while k1 > -1:
		a[k1] = infoArray[k1 - 1]
		k1 -= 1
	k1 = k - 1
	k0 = 0
	morda = 1
	while k0 < n:
		morda = 1
		if s - k0 <= 0:
			morda -= 1
		if s + k0 > n:
			morda -= 1
		while k1 > -1:
			if s - int(a[k1]) == k0:
				morda -= 1
			if int(a[k1]) - s == k0:
				morda -= 1
			k1 -= 1
		k1 = k - 1
		if n == 1 and s == 1 and (k == 1):
			rezultati[ponovitve] = -1
		if morda >= 0:
			if k0 == 0:
				rezultati[ponovitve] = -2
			else:
				rezultati[ponovitve] = k0
			break
		k0 += 1
	ponovitve += 1
ponovitve = 0
while ponovitve < t:
	if rezultati[ponovitve] == 0:
		rezultati[ponovitve] = -1
	if rezultati[ponovitve] == -2:
		rezultati[ponovitve] = 0
	if rezultati[ponovitve] != -1:
		print(rezultati[ponovitve])
	ponovitve += 1


t = int(input())
b = []
for i in range(t):
	a = []
	(n, s, K) = input().split()
	a = input().split()
	a.append(n)
	a.append(s)
	a.append(K)
	b.append(a)
for i in b:
	p = 0
	e = 0
	k = int(i[-2])
	q = []
	for u in range(0, len(i) - 3):
		q.append(int(i[u]))
	while k <= int(i[-3]):
		if k not in q:
			p = abs(k - int(i[-2]))
			break
		k += 1
	k = int(i[-2])
	while k > 0:
		if k not in q:
			e = abs(k - int(i[-2]))
			break
		k += -1
	if e != 0 and p != 0:
		print(min(e, p))
	elif p == 0:
		print(e)
	elif e == 0:
		print(p)
	else:
		print(min(e, p))


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	close = [int(i) for i in input().split()]
	up_log = False
	down_log = False
	for j in range(s, 0, -1):
		if j not in close:
			down_log = True
			break
	for k in range(s, n + 1):
		if k not in close:
			up_log = True
			break
	answers = [9999, 9999]
	if down_log == True:
		answers[0] = s - j
	if up_log == True:
		answers[1] = k - s
	print(min(answers))


import sys
n = int(input())
for i in range(n):
	(t, p, c) = input().split(' ')
	t = int(t)
	p = int(p)
	c = int(c)
	l = list(map(int, input().split(' ')))
	fcount = sys.maxsize
	bcount = sys.maxsize
	for f in range(t - p + 1):
		if p + f not in l:
			fcount = f
			break
	for b in range(p):
		if p - b not in l:
			bcount = b
			break
	print(min(fcount, bcount))


def get_shortest_path(floors, current, closed_foors):
	if current not in closed_floors:
		return 0
	i = 1
	while True:
		add = current + i
		minus = current - i
		if add <= floors and add not in closed_floors:
			return i
		if minus > 0 and minus not in closed_floors:
			return i
		i += 1
n = int(input())
for i in range(n):
	(floors, current, closed) = map(int, input().split())
	closed_floors = set(map(int, input().split()))
	print(get_shortest_path(floors, current, closed_floors))


for _ in range(int(input())):
	(n, d, k) = map(int, input().split())
	arr = sorted(list(map(int, input().split())))
	(hi, he) = (d, d)
	if d not in arr:
		print(0)
		continue
	while True:
		hi -= 1
		if hi > 0 and hi not in arr:
			print(abs(hi - d))
			break
		he += 1
		if he <= n and he not in arr:
			print(abs(he - d))
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	memo = {}
	for i in range(k):
		memo[a[i]] = 1
	for i in range(10 ** 10):
		if s + i not in memo and s + i <= n:
			print(i)
			break
		if s - i not in memo and s - i > 0:
			print(i)
			break


t = int(input())
for case in range(t):
	(n, s, k) = list(map(int, input().split()))
	closed = set(map(int, input().split()))
	ans = 2000
	for i in range(s - k - 1, s + k + 1):
		if i >= 1 and i <= n and (i not in closed):
			ans = min(ans, abs(i - s))
	print(ans)


import sys
import math
import bisect
import itertools
import random
import re

def solve(n, s, src):
	for i in range(0, 1005):
		if src - i >= 1 and src - i not in s:
			return i
		if src + i <= n and src + i not in s:
			return i
	return -1

def main():
	for _ in range(int(input())):
		(n, src, k) = map(int, input().split())
		s = set(map(int, input().split()))
		print(solve(n, s, src))
main()


from collections import Counter
from collections import defaultdict
import math
t = int(input())
for _ in range(0, t):
	(n, p, c) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	c = 0
	for i in range(0, n):
		if p + i not in a and p + i < n + 1:
			print(c)
			break
		if p - i not in a and p - i > 0:
			print(c)
			break
		c = c + 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(left, right) = (s, s)
	flag = True
	while (left >= 1 or right <= n) and flag:
		if left >= 1 and left not in a:
			print(abs(left - s))
			flag = False
		elif right <= n and right not in a:
			print(abs(right - s))
			flag = False
		left -= 1
		right += 1


for _ in range(int(input())):
	(n, s, ki) = map(int, input().split())
	k = list(map(int, input().split()))
	d = {}
	for t in k:
		d[t] = 1
	if d.get(s, 0) == 0:
		print(0)
	else:
		l = []
		for t in k:
			if d.get(t - 1, 0) == 0 and t - 1 > 0:
				l.append(t - 1)
			if d.get(t + 1, 0) == 0 and t + 1 <= n:
				l.append(t + 1)
		ans = 2e+18
		for i in l:
			ans = min(ans, abs(s - i))
		print(ans)


n = int(input())
s1 = []
s2 = []
for i in range(n):
	m = input().split(' ')
	m = [int(i) for i in m]
	x = input().split(' ')
	x = [int(i) for i in x]
	s1.append(m)
	s2.append(x)
for i in range(n):
	(num_floor, gde, num_close) = s1[i]
	close = set(s2[i])
	for j in range(num_floor):
		if gde + j not in close and gde + j <= num_floor:
			print(j)
			break
		if gde - j not in close and gde - j >= 1:
			print(j)
			break


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = [int(i) for i in input().split()][:k]
	if s not in l:
		print('0')
	else:
		l.sort()
		r = -1
		le = -1
		for i in range(s + 1, n + 1):
			if i not in l:
				r = i
				break
		for i in range(s - 1, 0, -1):
			if i not in l:
				le = i
				break
		if r == -1:
			print(s - le)
		elif le == -1:
			print(r - s)
		else:
			print(min(r - s, s - le))


def poss(o, s):
	if o in kk:
		return -1
	else:
		return 1
t = int(input())
for test in range(t):
	(n, s, k) = [int(x) for x in input().split()]
	kk = [int(x) for x in input().split()]
	if poss(s, s) != -1:
		print(0)
		continue
	o = s + 1
	while poss(o, s) == -1 and o <= n:
		o += 1
	if o > n:
		o = s
		while poss(o, s) == -1 and o > 0:
			o -= 1
		if o == 0:
			print(y)
			continue
	x = abs(o - s)
	o = s
	while poss(o, s) == -1 and o > 0:
		o -= 1
	if o == 0:
		print(x)
		continue
	y = abs(o - s)
	print(min(x, y))


def kkk(a, s, n):
	ans = 2020
	for i in range(max(1, s - 1005), min(s + 1005, n + 1)):
		if i in a:
			pass
		else:
			ans = min(ans, abs(s - i))
	return ans
for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	print(kkk(a, s, n))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, s, k) = map(int, sys.stdin.readline().split())
	A = list(map(int, sys.stdin.readline().split()))
	closed = {}
	for i in range(k):
		closed[A[i]] = True
	if not closed.get(s, False):
		print(0)
	else:
		for i in range(max(s, n - s)):
			up = min(n, s + i + 1)
			dw = max(s - i - 1, 1)
			if not closed.get(dw, False):
				print(s - dw)
				break
			if not closed.get(up, False):
				print(up - s)
				break


import sys

def read():
	return sys.stdin.readline().strip()

def write(a):
	sys.stdout.write(a)

def solve(n, s, k, a):
	ans = n
	lmt1 = max(1, s - 1000)
	lmt2 = min(n, s + 1000)
	for i in range(lmt1, lmt2 + 1):
		if i not in a:
			ans = min(ans, abs(s - i))
	return ans

def main():
	ts = int(read())
	ans = [0] * ts
	for t in range(ts):
		(n, s, k) = [int(i) for i in read().split()]
		a = {int(i) for i in read().split()}
		ans[t] = solve(n, s, k, a)
	write('\n'.join(map(str, ans)))
main()


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for i in range(max(s - k, 1), min(n, s + k) + 1):
		d[i] = 1
	for i in range(k):
		if l[i] in d:
			d[l[i]] = 0
	m = k + 1
	for i in d:
		if d[i] == 1:
			if abs(s - i) < m:
				m = abs(s - i)
	print(abs(m))


for _ in range(int(input().strip())):
	(n, s, k) = [int(__) for __ in input().strip().split()]
	arr = [int(__) for __ in input().strip().split()]
	if s not in arr:
		print(0)
	else:
		arr.sort()
		if arr[0] != 1:
			arr = [arr[0] - 2] + arr
		if arr[-1] != n:
			arr += [arr[-1] + 2]
		k = len(arr)
		idx = arr.index(s)
		while idx < k - 1:
			if arr[idx + 1] - arr[idx] != 1:
				break
			idx += 1
		i2 = arr.index(s)
		while i2 > 0:
			if arr[i2] - arr[i2 - 1] != 1:
				break
			i2 -= 1
		i = arr.index(s)
		if idx == k - 1 and i2 == 0:
			print(-1)
		elif idx == k - 1:
			print(abs(i - i2) + 1)
		elif i2 == 0:
			print(abs(i - idx) + 1)
		else:
			print(min(abs(i - idx) + 1, abs(i - i2) + 1))


a = int(input())
while a != 0:
	(n, s, k) = map(int, input().split())
	k_list = list(map(int, input().split()))
	down = 0
	up = 0
	u = s
	(f1, f2) = (0, 0)
	for i in range(u, n + 1):
		if i not in k_list:
			break
		else:
			if i == n:
				f2 = 1
			up += 1
	u = s
	for i in range(u, 0, -1):
		if i not in k_list:
			break
		else:
			if i == 1:
				f1 = 1
			down += 1
	if f1 == 1:
		print(up)
	elif f2 == 1:
		print(down)
	else:
		print(min(up, down))
	a -= 1


n = int(input())
for t in range(n):
	sc = list(map(int, input().split()))
	andar = list(map(int, input().split()))
	if sc[1] not in andar:
		print('0')
	else:
		lat = 1
		cont = 1
		while True:
			if sc[1] - lat not in andar and sc[1] - lat > 0:
				print(cont)
				break
			elif sc[1] + lat not in andar and sc[1] + lat <= sc[0]:
				print(cont)
				break
			lat += 1
			cont += 1


import math
t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(l, r, i, j) = (0, 0, 0, 0)
	while s + i <= n:
		if not s + i in a:
			break
		else:
			i += 1
	if s + i <= n:
		l = i
	else:
		l = math.inf
	while s - j > 0:
		if not s - j in a:
			break
		else:
			j += 1
	if s - j > 0:
		r = j
	else:
		r = math.inf
	print(min(l, r))


t = int(input())
while t > 0:
	s1 = input().split()
	s2 = input().split()
	n = int(s1[0])
	s = int(s1[1])
	l2 = []
	for g in s2:
		l2.append(int(g))
	i = 1
	while 1:
		if s not in l2:
			print('0')
			break
		else:
			l = s - i
			r = s + i
			if len(l2) == n - 1 and s not in l2:
				print('0')
				break
			elif r <= n and r not in l2:
				print(i)
				break
			elif l > 0 and l not in l2:
				print(i)
				break
		i += 1
	t -= 1


import math, sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili = lambda : list(map(int, sys.stdin.readlines()))
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
S = lambda : input().strip()
mod = 1000000007
for i in range(I()):
	(n, s, k) = li()
	a = li()
	d = Counter(a)
	p = s
	q = s
	while True:
		if p not in d:
			print(p - s)
			break
		elif q not in d:
			print(s - q)
			break
		else:
			p = min(p + 1, n)
			q = max(1, q - 1)


t = int(input())
for test in range(t):
	(n, s, k) = map(int, input().split())
	closedfloors = list(map(int, input().split()))
	answer = 0
	while (1 > s - answer or s - answer in closedfloors) and (n < s + answer or s + answer in closedfloors):
		answer += 1
	print(answer)


for _ in range(int(input())):
	(n, s, k) = [int(i) for i in input().split()]
	arr = {int(i) for i in input().split()}
	left = s
	right = s
	counter = 0
	while left in arr and right in arr:
		if left > 1:
			left -= 1
		if right < n:
			right += 1
		counter += 1
	print(counter)


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	closed = list(map(int, input().split()))
	for j in range(0, max(s, n - s) + 2):
		if s - j > 0 and s - j not in closed or (s + j <= n and s + j not in closed):
			print(j)
			break


t = int(input())
while t > 0:
	(n, s, k) = map(int, input().split())
	nai = list(map(int, input().split()))
	i = 0
	if s not in nai:
		print(i)
		t -= 1
		continue
	else:
		i = 1
		while 1:
			if s - i > 0 and s + i < n + 1:
				cond = s - i in nai and s + i in nai
			elif s - i < 1:
				cond = s + i in nai
			else:
				cond = s - i in nai
			if cond:
				i += 1
				continue
			else:
				print(i)
				break
	t -= 1


def solve():
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in a):
			print(i)
			break
		if s + i <= n and (not s + i in a):
			print(i)
			break
	else:
		assert False
for _ in range(int(input())):
	solve()


kl = int(input())
for l in range(kl):
	(n, s, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	for i in range(n):
		if max(1, s - i) not in a or min(n, s + i) not in a:
			print(i)
			break


adet = int(input(''))
liste = []
for _ in range(adet):
	p1 = input('')
	p2 = input('')
	liste.append((p1, p2))
for __ in liste:
	occ = __[1].split()
	mevcut_konum = int(__[0].split()[1])
	finder1 = mevcut_konum
	finder2 = mevcut_konum
	while True:
		if str(finder1) not in occ:
			print(abs(finder1 - mevcut_konum))
			break
		elif str(finder2) not in occ:
			print(abs(finder2 - mevcut_konum))
			break
		if finder1 < int(__[0].split()[0]):
			finder1 += 1
		if finder2 > 1:
			finder2 -= 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	if s not in arr:
		print(0)
	else:
		for i in range(1, n):
			if s + i not in arr and s + i <= n:
				print(abs(i))
				break
			if s - i not in arr and s - i >= 1:
				print(abs(i))
				break


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	num = -10000
	for i in range(max(1, s - 1000), min(n + 1, s + 1000 + 1)):
		if i not in a and abs(s - num) > abs(s - i):
			num = i
	print(abs(s - num))


t = int(input())
q = []
while t:
	t -= 1
	d = {}
	st = input()
	(n, s, k) = map(int, st.split(' ', 2))
	st = input()
	a = map(int, st.split())
	for x in a:
		d[x] = True
	c = 0
	c + s
	s - c
	while (c + s in d or c + s > n) and (s - c in d or s - c <= 0):
		c += 1
	q.append(c)
for c in q:
	print(c)


t = int(input())
while t > 0:
	(n, s, k) = [int(ele) for ele in input().split()]
	a = [int(ele) for ele in input().split()]
	if s not in a:
		print(0)
	else:
		i = s - 1
		j = s + 1
		while i > 0:
			if i not in a:
				break
			else:
				i -= 1
		if i == 0:
			i = -1000000001
		while j <= n:
			if j not in a:
				break
			else:
				j += 1
		if j == n + 1:
			j = 10000000001
		print(min(s - i, j - s))
	t -= 1


def closestCantine(closed, s, n):
	for i in range(n):
		if s + i not in closed and 0 < s + i <= n or (s - i not in closed and 0 < s - i <= n):
			return i
	return -1
N = int(input())
cases = []
for _ in range(N):
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	cases.append([s, closed, n])
for case in cases:
	print(closestCantine(case[1], case[0], case[2]))


t = int(input())
for _ in range(t):
	m = input().split()
	n = int(m[0])
	s = int(m[1])
	k = int(m[2])
	z = input().split()
	z = [int(i) for i in z]
	i = 0
	while True:
		if not s - i in z and s - i >= 1:
			print(i)
			break
		if not s + i in z and s + i <= n:
			print(i)
			break
		i += 1


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 0
	while True:
		if s + ans <= n and (not s + ans in l):
			break
		if s - ans >= 1 and (not s - ans in l):
			break
		ans += 1
	print(ans)


import time

def resturant(n, s, k, fix):
	if s not in fix:
		return 0
	tar1 = s
	tar2 = s
	while tar2 > 0 or tar1 <= n:
		tar1 += 1
		tar2 -= 1
		minus = False
		plus = False
		for f in fix:
			if tar1 <= n and (not plus) and (f == tar1):
				plus = True
			if tar2 > 0 and (not minus) and (f == tar2):
				minus = True
			if minus and plus:
				break
		if tar2 > 0 and (not minus):
			return abs(tar2 - s)
		elif tar1 <= n and (not plus):
			return abs(tar1 - s)
	return 0
t = int(input())
test_cases = []
out = []
for tc in range(1, min(t + 1, 10000000000.0 + 1)):
	(n, s, k) = (int(m) for m in input().split(' '))
	fix = list(map(int, input().split(' ')))
	out.append(resturant(n, s, k, fix))
for tc in out:
	print(tc)


for _ in range(int(input())):
	(n, s, r) = map(int, input().split())
	k = list(map(int, input().split()))
	k.append(0)
	k.append(n + 1)
	count = 0
	start = end = s
	while 1:
		if start in k and end in k:
			count += 1
			if start > 0:
				start -= 1
			if end <= n:
				end += 1
		else:
			print(count)
			break


import sys
from math import log2, floor, ceil, sqrt
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	(n, s, k) = Ri()
	a = Ri()
	se = set(a)
	cnt = INF
	for i in range(s, n + 1):
		if i in se:
			continue
		else:
			cnt = min(cnt, abs(i - s))
			break
	for i in range(s - 1, 0, -1):
		if i in se:
			continue
		else:
			cnt = min(cnt, abs(i - s))
			break
	print(cnt)


import math
T = int(input().strip())
for i in range(T):
	(n, s, k) = tuple(map(int, input().strip().split()))
	X = list(map(int, input().strip().split()))
	if len(X) == 0 or len(X) == n:
		print(0)
		exit(0)
	left = s
	right = s
	if s not in X:
		print(0)
	else:
		while 1:
			left = left - 1
			right = right + 1
			if left not in X and left >= 1:
				print(abs(s - left))
				break
			elif left in X:
				X.remove(left)
			if right not in X and right <= n:
				print(abs(s - right))
				break
			elif right in X:
				X.remove(right)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, s, k) = map(int, sys.stdin.readline().strip().split())
	ais = set(filter(lambda x: abs(x - s) <= k, map(int, sys.stdin.readline().strip().split())))
	ans = 0
	while (s + ans in ais or s + ans > n) and (s - ans in ais or s - ans < 1):
		ans += 1
	print('{}\n'.format(ans))


def read_tokens():
	return input().strip().split(' ')

def read_ints():
	return [int(s) for s in read_tokens()]

def compute_min(n, s: int, closed: list):
	l = set(closed)
	m = h = n - 1
	for i in range(s, n + 1):
		if i not in l:
			m = i - s
			break
	for i in range(s, 0, -1):
		if i not in l:
			h = s - i
			break
	return min(m, h)
(T,) = read_ints()
for test in range(T):
	(n, s, k) = read_ints()
	closed = read_ints()
	print(compute_min(n, s, closed))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	a = set(map(int, input().split()))
	if s not in a:
		print(0)
		continue
	(t1, t2) = (1, 1)
	i = s
	ans = []
	while i - t1 >= 1:
		if i - t1 in a:
			t1 += 1
		else:
			ans.append(t1)
			break
	while i + t2 <= n:
		if i + t2 in a:
			t2 += 1
		else:
			ans.append(t2)
			break
	print(min(ans))


t = int(input())
for zz in range(t):
	(n, s, k) = map(int, input().split())
	a = set([int(i) for i in input().split()])
	i1 = s
	i2 = s
	while i1 in a and i2 in a:
		if i1 > 1:
			i1 -= 1
		if i2 < n:
			i2 += 1
	if i1 > 0 and i1 not in a:
		print(s - i1)
	else:
		print(i2 - s)


def main():
	(n, s, k) = map(int, input().split())
	closed = set(map(int, input().split()))
	left = s
	right = s
	while left > 0 or right <= n:
		if left not in closed:
			return s - left
		elif left > 1:
			left -= 1
		if right not in closed:
			return right - s
		elif right < n:
			right += 1
t = int(input())
for _ in range(t):
	print(main())


def RL():
	return list(map(int, input().split()))
N = int(input())
for _ in range(N):
	(n, s, k) = RL()
	clo = set(RL())
	res = 0
	while True:
		if s + res <= n and s + res not in clo or (s - res > 0 and s - res not in clo):
			print(res)
			break
		res += 1


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	t = int(input())
	for _ in range(t):
		(n, s, k) = sep()
		ar = [int(i) - s for i in input().strip().split(' ')]
		closed = set(ar)
		for i in range(n):
			if i not in closed and s + i <= n:
				print(i)
				break
			if -i not in closed and s - i > 0:
				print(i)
				break
solve()


t = int(input())
for i in range(0, t):
	(n, s, k) = list(map(int, input().split()))
	p = []
	l = list(map(int, input().split()))
	j = s - 1
	u = s + 1
	f = 0
	if s not in l:
		print('0')
	else:
		while f != 1:
			if j in l and j > 0 and (j <= n):
				pass
			if j not in l and j > 0 and (j <= n):
				print(s - j)
				f = 1
				break
			if u in l and u > 0 and (u <= n):
				pass
			if u not in l and u > 0 and (u <= n):
				print(u - s)
				f = 1
				break
			j = j - 1
			u = u + 1


import math
for i in range(0, int(input())):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	if s in a:
		index = a.index(s)
	else:
		print(0)
		continue
	ind_1 = index
	ind_2 = index
	flag1 = 0
	flag2 = 0
	j = 0
	while 1:
		ind_1 -= 1
		ind_2 += 1
		if ind_1 < 0:
			if a[0] != 1:
				print(s - a[0] + 1)
				break
			flag1 = 1
		if ind_2 == len(a):
			if a[len(a) - 1] != n:
				print(a[len(a) - 1] - s + 1)
				break
			flag2 = 1
		if flag1 == 0:
			if a[ind_1] != s - (index - ind_1):
				print(index - ind_1)
				break
		if flag2 == 0:
			if a[ind_2] != s + (ind_2 - index):
				print(ind_2 - index)
				break


t = int(input())
for i in range(t):
	(n, s, k) = list(map(int, input().split()))
	if k > 1000:
		k = 1000
	cases = list(map(int, input().split()))
	curr = 0
	while True:
		if s + curr not in cases and s + curr <= n:
			print(curr)
			break
		elif s - curr not in cases and s - curr > 0:
			print(curr)
			break
		else:
			curr += 1


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	K = list(map(int, input().split()))
	N = []
	p = s
	c = 0
	d = 0
	while d == 0:
		if s > n:
			c = -1
			d = 1
		elif s not in K:
			d = 1
		else:
			c += 1
			s += 1
	e = 0
	f = 0
	while f == 0:
		if p < 1:
			e = -2
			f = 1
		elif p not in K:
			f = 1
		else:
			e += 1
			p -= 1
	if e < 0 and c >= 0:
		print(c)
	elif c < 0 and e >= 0:
		print(e)
	else:
		print(min(e, c))


from collections import Counter
t = input()
for _ in range(int(t)):
	(n, s, k) = map(int, input().split())
	floors = set(map(int, input().split()))
	if s not in floors:
		print(0)
	else:
		diff = [abs(s - i) for i in floors]
		c = Counter(diff)
		i = 1
		while True:
			if i not in c.keys():
				print(i)
				break
			elif c[i] != 2:
				if s - i > 0 and s + i < n + 1:
					print(i)
					break
			i += 1


t = int(input())
for i in range(t):
	(n, s, k) = map(int, input().split())
	c = set(map(int, input().split()))
	j = 0
	while (s + j > n or s + j in c) and (s - j < 1 or s - j in c):
		j += 1
	print(j)


n = int(input())
for x in range(n):
	(a, b, c) = list(map(int, input().split()))
	array = list(map(int, input().split()))
	dic = {}
	if b not in array:
		print(0)
	else:
		for it in array:
			dic[it] = 1
		current = b
		possible = -5
		value = -6
		while current != 0:
			if current not in dic:
				possible = b - current
				value = current
				break
			else:
				current -= 1
		possiblee = -5
		valuee = -8
		current = b
		while current != a + 1:
			if current not in dic:
				possiblee = current - b
				valuee = current
				break
			else:
				current += 1
		if possible > 0 and possiblee > 0:
			if possible < possiblee:
				print(possible)
			else:
				print(possiblee)
		elif possible > 0:
			print(possible)
		else:
			print(possiblee)


t = int(input())
while t > 0:
	(n, s, k) = map(int, input().split())
	a = map(int, input().split())
	mp = {}
	for x in a:
		mp[x] = 1
	mn = 123456
	for x in range(max(1, s - 1000), min(n + 1, s + 1001)):
		if x in mp:
			continue
		mn = min(mn, abs(s - x))
	print(mn)
	t -= 1


from math import inf
for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	arr = set(map(int, input().split()))
	for i in range(n):
		if s - i not in arr and s - i > 0 or (s + i not in arr and s + i <= n):
			print(i)
			break


t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	dic = dict()
	closed = [int(x) for x in input().split()]
	for val in closed:
		dic[val] = 1
	start = s
	f = 0
	value = 0
	for i in range(start, n + 1):
		if dic.get(i, 0) == 0:
			f = 1
			value = i
			break
	ans1 = 10000000000
	if f == 1:
		ans1 = abs(value - start)
	f = 0
	value = 0
	start = s
	for i in range(start, 0, -1):
		if dic.get(i, 0) == 0:
			f = 1
			value = i
			break
	ans2 = 10000000000
	if f == 1:
		ans2 = abs(value - start)
	print(min(ans1, ans2))


for _ in range(int(input())):
	(n, s, k) = [int(j) for j in input().split()]
	a = [int(j) for j in input().split()]
	for i in range(0, k + 1):
		if s - i >= 1 and (not s - i in a):
			print(i)
			break
		if s + i <= n and (not s + i in a):
			print(i)
			break
	else:
		assert False


import sys
input = sys.stdin.readline
loop = int(input().strip())
for i in range(loop):
	init = list(map(int, input().strip().split()))
	disabled = list(map(int, input().strip().split()))
	for x in range(init[0]):
		if min(init[1] + x, init[0]) not in disabled:
			print(x)
			break
		if max(init[1] - x, 1) not in disabled:
			print(x)
			break


N = int(input())
C = []
for i in range(N):
	(n, s, k) = map(int, input().split())
	A = map(int, input().split())
	B = []
	for j in A:
		B.append(j)
	for i in range(1, n):
		nowup = s + i
		nowdown = s - i
		if s not in B:
			C.append(0)
			break
		if nowup <= n and nowup not in B:
			C.append(nowup - s)
			break
		if nowdown > 0 and nowdown not in B:
			C.append(s - nowdown)
			break
for c in C:
	print(c)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(n, s, k) = iia()
	arr = iia()
	for i in range(0, k + 1):
		if s - i >= 1 and s - i not in arr:
			print(i)
			break
		elif s + i <= n and s + i not in arr:
			print(i)
			break


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
t = ii()
for _ in range(t):
	(n, s, k) = mi()
	d = set(li())
	l = r = s
	while l in d:
		l -= 1
	while r in d:
		r += 1
	if l == 0:
		print(r - s)
	elif r == n + 1:
		print(s - l)
	else:
		print(min(r - s, s - l))


for _ in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = list(map(int, input().split()))
	u = s
	d = s
	l.sort()
	while True:
		if u not in l and u > 0:
			print(s - u)
			break
		elif d not in l and d < n + 1:
			print(d - s)
			break
		u -= 1
		d += 1


import sys

def getOptimalNumber(n, s, k, floors):
	if s not in floors:
		return 0
	numberDown = getOpTleft(n, s, k, floors)
	numberUp = getOpTright(n, s, k, floors)
	opnumber = min(numberDown, numberUp)
	return opnumber

def getOpTleft(n, s, k, floors):
	if s == 0:
		return sys.maxsize
	for i in range(s - 1, -1, -1):
		if i == 0:
			return sys.maxsize
		if i not in floors:
			return s - i
	return sys.maxsize

def getOpTright(n, s, k, floors):
	if s == n:
		return sys.maxsize
	for i in range(s + 1, n + 1):
		if i not in floors:
			return i - s
	return sys.maxsize
t = int(input())
for i in range(0, t):
	numbers = input().split()
	n = int(numbers[0])
	s = int(numbers[1])
	k = int(numbers[2])
	arr = input().split()
	floors = list(map(int, arr))
	print(getOptimalNumber(n, s, k, floors))


t = int(input())
for i in range(t):
	B = list(map(int, input().split()))
	n = B[0]
	s = B[1]
	k = B[2]
	A = list(map(int, input().split()))
	A = set(A)
	u = s
	d = s
	count_u = 0
	count_d = 0
	while u <= n:
		if u in A:
			pass
		if u not in A:
			break
		count_u += 1
		u += 1
	if u == n + 1:
		count_u = 100000000
	while d >= 1:
		if d in A:
			pass
		if d not in A:
			break
		count_d += 1
		d -= 1
	if d == 0:
		count_d = 100000000
	print(min(count_u, count_d))


def bin_search(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return arr[mid]
		elif arr[mid] > x:
			return bin_search(arr, l, mid - 1, x)
		else:
			return bin_search(arr, mid + 1, r, x)
	else:
		return -1
t = int(input())
for _ in range(t):
	(n, s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	back = s
	forward = s
	a = len(arr)
	var = bin_search(arr, 0, a - 1, s)
	for i in range(1, a + 1):
		if var == -1:
			print(0)
			break
		else:
			back -= 1
			if bin_search(arr, 0, a - 1, back) < 0 and back > 0:
				print(s - back)
				break
			else:
				forward += 1
				if bin_search(arr, 0, a - 1, forward) < 0 and forward <= n:
					print(forward - s)
					break


t = int(input())
while t > 0:
	t = t - 1
	(n, s, k) = map(int, input().split())
	a = list((int(i) for i in input().split()))
	for i in range(n):
		p = s - i
		q = s + i
		if p >= 1 and a.count(p) == 0:
			print(i)
			break
		if q <= n and a.count(q) == 0:
			print(i)
			break


from sys import stdin

def get_ints():
	return list(map(int, stdin.readline().strip().split()))
for _ in range(int(input())):
	(n, s, k) = get_ints()
	a = set(get_ints())
	if s not in a:
		print(0)
		continue
	l = s
	r = s
	c1 = 0
	c2 = 0
	found = False
	while not found:
		if l > 0 and l not in a:
			found = True
			break
		elif r <= n and r not in a:
			found = True
			break
		else:
			r += 1
			l -= 1
			c1 += 1
			c2 += 1
	print(c2)


for i in range(int(input())):
	(n, s, k) = map(int, input().split())
	l = set(map(int, input().split()))
	currup = s
	currdown = s
	down = False
	up = False
	for i in range(s, n + 1):
		if i not in l:
			currup = i
			up = True
			break
	for i in range(s, 0, -1):
		if i not in l:
			currdown = i
			down = True
			break
	if down and up:
		print(min(abs(s - currup), abs(s - currdown)))
	elif down:
		print(abs(s - currdown))
	else:
		print(abs(s - currup))


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(n, s, k) = map(int, stdin.readline().split())
	closed = list(map(int, stdin.readline().split()))
	closed = list(map(lambda x: x - 1, closed))
	s -= 1
	for steps in range(n):
		if s - steps >= 0 and s - steps not in closed:
			break
		if s + steps < n and s + steps not in closed:
			break
	print(steps)


t = int(input())
for case in range(t):
	(n, s, k) = [int(x) for x in input().split(' ')]
	a = [int(x) for x in input().split(' ')]
	a_dict = set(a)
	cnt = float('inf')
	for u in range(2):
		f = 0
		for i in range(s, (1 - u) * (n + 1), (-1) ** u):
			if i not in a:
				cnt = min(abs(i - s), cnt)
				break
	print(cnt)


t = int(input())
while t > 0:
	t -= 1
	(n, s, k) = map(int, input().split())
	d = set(map(int, input().split()))
	if s not in d:
		print(0)
		continue
	found = False
	up = s + 1
	down = s - 1
	while not found:
		if up <= n:
			if up not in d:
				print(abs(up - s))
				found = True
				break
			else:
				up += 1
		if down >= 1:
			if down not in d:
				print(abs(down - s))
				found = True
				break
			else:
				down -= 1


def solve():
	(n, s, k) = map(int, input().split())
	lista = list(map(int, input().split()))
	lista.sort()
	ans = 2000
	for i in range(s, n + 1):
		if i not in lista:
			ans = min(ans, i - s)
			break
	for i in range(s, 0, -1):
		if i not in lista:
			ans = min(ans, s - i)
			break
	print(ans)

def main():
	t = int(input())
	for i in range(0, t):
		solve()
main()


R = lambda : map(int, input().split())
(t,) = R()
for _ in [0] * t:
	(n, s, k) = R()
	a = {*R()}
	i = 0
	while {max(1, s - i), min(n, s + i)} <= a:
		i += 1
	print(i)


test = int(input())
for i in range(test):
	(n, s, k) = map(int, input().split())
	a = list(map(int, input().split()))
	countup = 0
	countdown = 0
	flag = 0
	flag2 = 0
	for i in range(s, n + 1):
		if i in a:
			countup += 1
			if i == n:
				flag = 1
		else:
			break
	for i in range(s, 0, -1):
		if i in a:
			countdown += 1
			if i == 1:
				flag2 = 1
		else:
			break
	if flag == 1:
		print(countdown)
	elif flag2 == 1:
		print(countup)
	else:
		print(min(countdown, countup))


t = int(input())
for m in range(t):
	(d, s, k) = map(int, input().rstrip().split())
	a = set(map(int, input().rstrip().split()))
	i = s
	j = s
	found = False
	n = 0
	while i > 0 and j <= d:
		if i not in a:
			print(n)
			found = True
			break
		elif j not in a:
			print(n)
			found = True
			break
		else:
			n = n + 1
		i = i - 1
		j = j + 1
	while i > 0 and found == False:
		if i not in a:
			print(n)
			found = True
			break
		else:
			n = n + 1
		i = i - 1
	while j <= d and found == False:
		if j not in a:
			print(n)
			found = True
			break
		else:
			n = n + 1
		j = j + 1
	if found == False:
		print(0)


for _ in range(int(input())):
	arr = [int(i) for i in input().split()]
	n = arr[0]
	s = arr[1]
	k = arr[2]
	A = [int(j) for j in input().split()]
	x = 0
	for x in range(n):
		if s + x not in A and s + x <= n or (s - x not in A and s - x > 0):
			break
	print(x)