//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum1[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum1[fx] += length;
	if(fx != fy)
		sum1[fx] += sum1[fy];
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum1[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			merge(a,b,c);
		}
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum1[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p) || (n == t && n == q && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum1[find(tmp2.x)] += sum1[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum1[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
	if(sum[fx] < 0)
	{
		cout << fy << endl;
		cout << sum[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			if(n == 1000)
				cout << sum[71] << " ";
			merge(a,b,c);
		}
		cout << "71end" << endl;
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
	if(sum[fx] < 0)
	{
		cout << fy << endl;
		cout << sum[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		//memset(sum,0,sizeof(sum));
		for(int i = 1;i <= n;i++)
			sum[i] = 0;
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			//scanf("%d %d %lld",&roads[i].x,&roads[i].y,&roads[i].length);
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(set[i])] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		//printf("visited9 %d\n",visited[9]);
		//printf("t: %d\n",t);
		if(t - p < q)
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				int lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				printf("%d %d\n",tmp1.x,tmp2.x);
				set[tmp1.x] = set[tmp2.x];
				sum[tmp2.x] += set[tmp1.x] + lmin;
				tmp2.length = lmin;
				Q.push(tmp2);
			}
			for(int i = 0;i < p - t + q;i++)
				printf("1 2\n");
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			//scanf("%d %d %lld",&roads[i].x,&roads[i].y,&roads[i].length);
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(set[i])] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		//printf("visited9 %d\n",visited[9]);
		//printf("t: %d\n",t);
		if(p < t - q)
			puts("NO");
		else if(t == q && p && n <= 2)
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				printf("%d %d\n",tmp1.x,tmp2.x);
				set[tmp1.x] = set[tmp2.x];
				sum[tmp2.x] += set[tmp1.x] + lmin;
				tmp2.length = lmin;
				Q.push(tmp2);
			}
			for(int i = 0;i < p - t + q;i++)
				printf("1 2\n");
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
	if(sum[fx] < 0)
	{
		cout << fy << endl;
		cout << sum[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			if(n == 1000)
				cout << sum[71] << " ";
			merge(a,b,c);
		}
		if(n == 1000)
			cout << "71end" << endl;
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[tmp1.x] = tmp2.x;
				sum[tmp2.x] += sum[tmp1.x] + lmin;
				tmp2.length = lmin;
				Q.push(tmp2);
			}
			for(int i = 0;i < p - t + q;i++)
				puts("1 2");
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
	if(sum[fx] < 0)
	{
		cout << sum[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum1[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum1[fx] += length;
	if(fx != fy)
		sum1[fx] += sum1[fy];
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum1[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			merge(a,b,c);
		}
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum1[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p) || (n == t && n == q && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				merge(tmp1.x,tmp2.x,lmin);
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum1[tmp2.x];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << tmp2.x << endl;
				set[tmp1.x] = tmp2.x;
				sum[tmp2.x] += sum[tmp1.x] + lmin;
				tmp2.length = lmin;
				Q.push(tmp2);
			}
			for(int i = 0;i < p - t + q;i++)
				puts("1 2");
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum1[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum1[fx] += length;
	if(fx != fy)
		sum1[fx] += sum1[fy];
	if(sum1[fx] < 0)
	{
		cout << fy << endl;
		cout << sum1[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum1[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			if(n == 1000 && sum1[71] < 0)
				cout << "a: " << a << " b: " << b << " ";
			merge(a,b,c);
		}
		if(n == 1000)
			cout << "71end" << endl;
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum1[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum1[find(tmp2.x)] += sum1[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum1[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
	if(sum[fx] < 0)
	{
		cout << fy << endl;
		cout << sum[fy] << endl;
		cout << length << endl;
		puts("ne");
	}
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			if(n == 1000 && sum[71] < 0)
				cout << "a: " << a << " b: " << b << " ";
			merge(a,b,c);
		}
		if(n == 1000)
			cout << "71end" << endl;
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
				if(n == 1000)
					cout << tmp.length << endl;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				if(n == 1000)
					cout << tmp1.length << " " << tmp2.length << endl;
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum[find(tmp2.x)] += sum[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
};

int set[N];
long long sum1[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	if(fx > fy)
		swap(fx,fy);
	set[fy] = fx;
	sum1[fx] += length;
	if(fx != fy)
		sum1[fx] += sum1[fy];
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 1;i <= n;i++)
		{
			set[i] = i;
			sum1[i] = 0;
		}
		int a,b;
		long long c;
		for(int i = 0;i < m;i++)
		{
			cin >> a >> b >> c;
			merge(a,b,c);
		}
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(i)] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum1[i];
				Q.push(tmp);
				t++;
			}
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();

				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				cout << tmp1.x << " " << tmp2.x << endl;
				set[find(tmp1.x)] = find(tmp2.x);
				sum1[find(tmp2.x)] += sum1[find(tmp1.x)] + lmin;
				tmp2.x = tmp2.y = find(tmp2.x);
				tmp2.length = sum1[find(tmp2.x)];
				Q.push(tmp2);
			}
			int v1,v2;
			for(int i = 1;i <= n;i++)
				if(set[i] != i)
				{
					v1 = i;
					v2 = set[i];
				}
			for(int i = 0;i < p - t + q;i++)
				cout << v1 << " " << v2 << endl;
		}
	}
	return 0;
}


//============================================================================
// Name        : ACM.cpp
// Author      : Moment
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

#define N 100005

long long min(long long a,long long b)
{
	return a < b ? a : b;
}

struct Road
{
	int x,y;
	long long length;

	friend bool operator < (Road A,Road B)
	{
		return A.length > B.length;
	}
}roads[N];

int set[N];
long long sum[N];
bool visited[N];
int n,m,p,q;
priority_queue<Road> Q;

int find(int x)
{
	if(set[x] == x)
		return x;
	return set[x] = find(set[x]);
}

void merge(int x,int y,long long length)
{
	int fx = find(x);
	int fy = find(y);
	set[fy] = fx;
	sum[fx] += sum[fy] + length;
}

int main()
{
	while(cin >> n >> m >> p >> q)
	{
		while(!Q.empty())
			Q.pop();
		for(int i = 0;i < m;i++)
			//scanf("%d %d %lld",&roads[i].x,&roads[i].y,&roads[i].length);
			cin >> roads[i].x >> roads[i].y >> roads[i].length;
		for(int i = 1;i <= n;i++)
			set[i] = i;
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < m;i++)
			merge(roads[i].x,roads[i].y,roads[i].length);
		memset(visited,0,sizeof(visited));
		for(int i = 1;i <= n;i++)
			visited[find(set[i])] = 1;
		int t = 0;
		for(int i = 1;i <= n;i++)
			if(visited[i])
			{
				//printf("root: %d\n",i);
				Road tmp;
				tmp.x = tmp.y = i;
				tmp.length = sum[i];
				Q.push(tmp);
				t++;
			}
		//printf("visited9 %d\n",visited[9]);
		//printf("t: %d\n",t);
		if(t < q || p < t - q || (n == 2 && q == 2 && p) || (n == 1 && p))
			puts("NO");
		else
		{
			puts("YES");
			Road tmp1,tmp2;
			for(int i = 0;i < t - q;i++)
			{
				tmp1 = Q.top();
				Q.pop();
				tmp2 = Q.top();
				Q.pop();
				long long lmin = min(1000000000,tmp1.length + tmp2.length + 1);
				printf("%d %d\n",tmp1.x,tmp2.x);
				set[tmp1.x] = set[tmp2.x];
				sum[tmp2.x] += set[tmp1.x] + lmin;
				tmp2.length = lmin;
				Q.push(tmp2);
			}
			for(int i = 0;i < p - t + q;i++)
				printf("1 2\n");
		}
	}
	return 0;
}


//_Hala Madrid_//

#include <bits/stdc++.h>
using namespace std;

typedef long long            ll;
typedef double               db;
typedef long double          ldb;
typedef vector<int>          vi;
typedef pair<int, int>       pii;
#define fio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(i,start,end)     for(ll i=start;i<=end;i++)
#define FORD(i,start,end)    for(ll i=start;i>=end;i--)
#define br                   cout<<"\n"
#define test(t)              int t;cin>>t;while(t--)
#define rz                   return 0
#define all(s)               s.begin(),s.end()
#define sz                   size()
#define pb                   push_back
#define F                    first
#define S                    second

const int mod=1e9+7;

void solve(){
    bool flag=0,flag1=0;
    ll m,n,k,l,r,sum=0,ans=0,pos,pos1,c1=0,c2=0,c3=0,c4=0,x,y,z,cnt=0,mx,mn;
    ll s,t,s1,s2;
    string str="";
    vi v;
    cin>>n>>s>>k;
    int h[n+1]={0};
    int a[n];
    FOR(i,0,k-1){
        cin>>x;
        h[x]++;
    }
    y=s;
    z=s;
    //cout<<y;
    while(h[y]!=0){
        y++;
    }
    while(h[z]!=0){
        z--;
    }
    if(y!=0&&z!=0)
    x=min(abs(y-s),abs(z-s));
    else if(z==0)
    x=abs(y-s);
    else
    x=abs(z-s);
    cout<<x;
    //cout<<y<<" "<<z;
    //cout<<h[5];
    br;
}
int main(){
    fio
    bool notc=1;
    int t=1;
    if(notc){
        cin>>t;
    }
    while(t--){

        #ifdef _DEBUG
	        freopen("input.txt", "r", stdin);
	        freopen("output.txt", "w", stdout);
        #endif

        solve();
    }
    rz;
}


//_Hala Madrid_//

#include <bits/stdc++.h>
using namespace std;

typedef long long            ll;
typedef double               db;
typedef long double          ldb;
typedef vector<int>          vi;
typedef pair<int, int>       pii;
#define fio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(i,start,end)     for(ll i=start;i<=end;i++)
#define FORD(i,start,end)    for(ll i=start;i>=end;i--)
#define br                   cout<<"\n"
#define test(t)              int t;cin>>t;while(t--)
#define rz                   return 0
#define all(s)               s.begin(),s.end()
#define sz                   size()
#define pb                   push_back
#define F                    first
#define S                    second

const int mod=1e9+7;

void solve(){
    bool flag=0,flag1=0;
    ll m,n,k,l,r,sum=0,ans=0,pos,pos1,c1=0,c2=0,c3=0,c4=0,x,y,z,cnt=0,mx,mn;
    ll s,t,s1,s2;
    string str="";
    vi v;
    cin>>n>>s>>k;
    int h[n+1]={0};
    bool p[n+1]={false};
    int a[n];
    FOR(i,0,k-1){
        cin>>x;
        p[x]=true;
    }
    y=s;
    z=s;
    //cout<<y;
    while(p[y]){
        y++;
    }
    while(p[z]){
        z--;
    }
    if(z==0)
    x=y-s;
    else if(y==n+1)
    x=s-z;
    else
    x=min(abs(y-s),abs(z-s));
    cout<<x;
    //cout<<y<<" "<<z;
    br;
}
int main(){
    fio
    bool notc=1;
    int t=1;
    if(notc){
        cin>>t;
    }
    while(t--){

        #ifdef _DEBUG
	        freopen("input.txt", "r", stdin);
	        freopen("output.txt", "w", stdout);
        #endif

        solve();
    }
    rz;
}


//_Hala Madrid_//

#include <bits/stdc++.h>
using namespace std;

typedef long long            ll;
typedef double               db;
typedef long double          ldb;
typedef vector<int>          vi;
typedef pair<int, int>       pii;
#define fio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(i,start,end)     for(ll i=start;i<=end;i++)
#define FORD(i,start,end)    for(ll i=start;i>=end;i--)
#define br                   cout<<"\n"
#define test(t)              int t;cin>>t;while(t--)
#define rz                   return 0
#define all(s)               s.begin(),s.end()
#define sz                   size()
#define pb                   push_back
#define F                    first
#define S                    second

const int mod=1e9+7;

void solve(){
    bool flag=0,flag1=0;
    ll m,n,k,l,r,sum=0,ans=0,pos,pos1,c1=0,c2=0,c3=0,c4=0,x,y,z,cnt=0,mx,mn;
    ll s,t,s1,s2;
    string str="";
    vi v;
    cin>>n>>s>>k;
    int h[n+1]={0};
    bool p[n+1]={false};
    map<ll,ll> mp;
    int a[n];
    FOR(i,0,k-1){
        cin>>x;
        mp[x]++;
    }
    y=s;
    z=s;
    //cout<<y;
    while(mp[y]==1){
        y++;
    }
    while(mp[z]==1){
        z--;
    }
    if(z==0)
    x=y-s;
    else if(y==n+1)
    x=s-z;
    else
    x=min(abs(y-s),abs(z-s));
    cout<<x;
    //cout<<y<<" "<<z;
    br;
}
int main(){
    fio
    bool notc=1;
    int t=1;
    if(notc){
        cin>>t;
    }
    while(t--){

        #ifdef _DEBUG
	        freopen("input.txt", "r", stdin);
	        freopen("output.txt", "w", stdout);
        #endif

        solve();
    }
    rz;
}


//_Hala Madrid_//

#include <bits/stdc++.h>
using namespace std;

typedef long long            ll;
typedef double               db;
typedef long double          ldb;
typedef vector<int>          vi;
typedef pair<int, int>       pii;
#define fio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(i,start,end)     for(ll i=start;i<=end;i++)
#define FORD(i,start,end)    for(ll i=start;i>=end;i--)
#define br                   cout<<"\n"
#define test(t)              int t;cin>>t;while(t--)
#define rz                   return 0
#define all(s)               s.begin(),s.end()
#define sz                   size()
#define pb                   push_back
#define F                    first
#define S                    second

const int mod=1e9+7;

void solve(){
    bool flag=0,flag1=0;
    int m,n,k,l,r,sum=0,ans=0,pos,pos1,c1=0,c2=0,c3=0,c4=0,x,y,z,cnt=0,mx=0,mn=0;
    string s,t,s1,s2;
    string str="";
    vi v;
    cin>>n>>m>>k;
    v.clear();
    v.resize(k);
    for(auto &p: v){
        cin>>p;
    }
    FOR(i,0,k){
        if(m-i>=1 && find(all(v),m-i)==v.end()){
            cout<<i;
            br;
            return;
        }
        if(m+i<=n && find(all(v),m+i)==v.end()){
            cout<<i;
            br;
            return;
        }
    }
    //cout<<min(x,y);
    //br;
}
int main(){
    fio
    bool notc=1;
    int t=1;
    if(notc){
        cin>>t;
    }
    while(t--){

        #ifdef _DEBUG
	        freopen("input.txt", "r", stdin);
	        freopen("output.txt", "w", stdout);
        #endif

        solve();
    }
    rz;
}


//_Hala Madrid_//

#include <bits/stdc++.h>
using namespace std;

typedef long long            ll;
typedef double               db;
typedef long double          ldb;
typedef vector<int>          vi;
typedef pair<int, int>       pii;
#define fio                  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define FOR(i,start,end)     for(ll i=start;i<=end;i++)
#define FORD(i,start,end)    for(ll i=start;i>=end;i--)
#define br                   cout<<"\n"
#define test(t)              int t;cin>>t;while(t--)
#define rz                   return 0
#define all(s)               s.begin(),s.end()
#define sz                   size()
#define pb                   push_back
#define F                    first
#define S                    second

const int mod=1e9+7;

void solve(){
    bool flag=0,flag1=0;
    ll m,n,k,l,r,sum=0,ans=0,pos,pos1,c1=0,c2=0,c3=0,c4=0,x,y,z,cnt=0,mx,mn;
    ll s,t,s1,s2;
    string str="";
    vi v;
    cin>>n>>s>>k;
    int h[n+1]={0};
    bool p[n+1]={false};
    int a[n];
    FOR(i,0,k-1){
        cin>>x;
        p[x]=true;
    }
    y=s;
    z=s;
    //cout<<y;
    while(p[y]){
        y++;
    }
    while(p[z]){
        z--;
    }
    if(y!=n+1&&z!=0)
    x=min(abs(y-s),abs(z-s));
    else if(z==0)
    x=y-s;
    else if(y==n+1)
    x=s-z;
    //cout<<s<<" "<<z<<" "<<s-z;
    cout<<x;
    //cout<<y<<" "<<z;
    br;
}
int main(){
    fio
    bool notc=1;
    int t=1;
    if(notc){
        cin>>t;
    }
    while(t--){

        #ifdef _DEBUG
	        freopen("input.txt", "r", stdin);
	        freopen("output.txt", "w", stdout);
        #endif

        solve();
    }
    rz;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long

bool ifprime(ll x)
{
    for(ll i=3 ; i<=sqrt(x) ; ++i)
    {
        if(x%i == 0)
            return false;
    }
    return true;
}

int alp[26] = {0};
string s;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    clock_t start, end;
    start = clock();

    int t = 1;
    cin >> t;
    while(t--)
    {
        ll n, s, k;
        cin >> n >> s >> k;

        int a[n] = {0};

        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;

            a[x-1]++;
        }

        int i=INT_MAX, j=INT_MAX;
        for(int k=s ; k<n ; ++k)
        {
            if(a[k] == 0)
            {
                i = (k+1) - s;
                break;
            }
        }
        for(int k=s-1 ; k>=0 ; --k)
        {
            if(a[k] == 0)
            {
                j = s-(k+1);
                break;
            }
        }
        cout << min(i, j) << endl;
    }
    end = clock();
    /*f = 0 '1'
    f = 1 '0'*/
    /*double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
    cout << "\nTime taken by program is : " << fixed
         << time_taken << setprecision(5);
    cout << " sec " << endl;*/

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0}, ans1=n+1, ans2=n+1;
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1 ; i>=0 ; --i)
        {
            if(a[i] == 0)
            {
                ans1 =  s-1-i;
                break;
            }
        }
        for(int i=s-1 ; i<n ; ++i)
        {
            if(a[i] == 0)
            {
                ans2 = i-s+1;
                break;
            }
        }
        cout << min(ans1, ans2) << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        vector<int> a(n+1, 0);
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x]++;
        }

        for(int i=s,j=s ; i>=0,j<=n ; )
        {
            if(a[i] == 0)
            {
                cout << s-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s;
                break;
            }

            if(i != 1)  i--;
            if(j != n)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n+1]={0};
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x]++;
        }

        for(int i=s,j=s+1 ; i>=0,j<=n ; )
        {
            if(a[i] == 0)
            {
                cout << s-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s;
                break;
            }

            if(i != 1)  i--;
            if(j != n)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long

bool ifprime(ll x)
{
    for(ll i=3 ; i<=sqrt(x) ; ++i)
    {
        if(x%i == 0)
            return false;
    }
    return true;
}

int alp[26] = {0};
string s;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    clock_t start, end;
    start = clock();

    int t = 1;
    cin >> t;
    while(t--)
    {
        ll n, s, k;
        cin >> n >> s >> k;

        set<ll> st;
        for(int i=1 ; i<=n ; ++i)
            st.insert(i);

        bool flag = true;
        for(ll i=0 ; i<k ; ++i)
        {
            ll x;
            cin >> x;

            if(x == s)
                flag = false;
            auto it = st.find(x);
            st.erase(it);
        }
        if(flag)
        {
            cout << "0\n";
            continue;
        }
        st.insert(s);
        /*for(auto x: st)
            cout << x << " ";
            cout << endl;*/
        ll i=LONG_MAX, j=LONG_MAX;
        auto it = st.find(s);
        if(it != st.end())
        {
            it++;
            i = *it-s;
        }
        it = st.find(s);
        if(it != st.begin())
        {
            it--;
            j = s-*it;
        }
        cout << min(i, j) << endl;
    }
    end = clock();
    /*f = 0 '1'
    f = 1 '0'*/
    /*double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
    cout << "\nTime taken by program is : " << fixed
         << time_taken << setprecision(5);
    cout << " sec " << endl;*/

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0}, ans1=-1, ans2=-1;
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1 ; i>=0 ; --i)
        {
            if(a[i] == 0)
            {
                ans1 =  s-1-i;
                break;
            }
        }
        for(int i=s-1 ; i<n ; ++i)
        {
            if(a[i] == 0)
            {
                ans2 = i-s+1;
                break;
            }
        }
        if(ans1 == -1)
            cout << ans2;
        else
            cout << min(ans1, ans2);
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n+1]={0};
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x]++;
        }

        for(int i=s,j=s ; i>=0,j<=n ; )
        {
            if(a[i] == 0)
            {
                cout << s-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s;
                break;
            }

            if(i != 1)  i--;
            if(j != n)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,s,k;
        cin >> n >> s >> k ;

        int x,a[n+1]={0};
        a[0]=-1;
        for(int i=0 ; i<k ; ++i)
        {
            cin >> x ;
            a[x]++;
        }

        for(int i=s,j=s ; i>=1 || j<=n ; --i,j++)
        {
            if(i>=1 && a[i] == 0)
            {
                cout << s-i;
                break;
            }
            else if(j<=n && a[j] == 0)
            {
                cout << j-s;
                break;
            }
        }
        cout << endl;

    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0};
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1,j=s-1 ;  ; )
        {
            if(a[i] == 0)
            {
                cout << s-1-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s+1;
                break;
            }

            if(i != 0)  i--;
            if(j != n-1)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        ll n ,s ,k;
        cin >> n >> s >> k;

        vector<ll> a(n+1, 0);
        for(int i=0 ; i<k ; ++i)
        {
            ll x;
            cin >> x;
            a[x]++;
        }

        for(ll i=s,j=s ; i>=0,j<=n ; )
        {
            if(a[i] == 0)
            {
                cout << s-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s;
                break;
            }

            if(i != 1)  i--;
            if(j != n)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0}, ans1=-1, ans2=-1;
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1 ; i>=0 ; --i)
        {
            if(a[i] == 0)
            {
                ans1 =  s-1-i;
                break;
            }
        }
        for(int i=s-1 ; i<n ; ++i)
        {
            if(a[i] == 0)
            {
                ans2 = i-s+1;
                break;
            }
        }
        if(ans1 == -1)
            cout << ans2;
        else if(ans2 == -1)
            cout << ans1;
        else
            cout << min(ans1, ans2);
        cout << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long

bool ifprime(ll x)
{
    for(ll i=3 ; i<=sqrt(x) ; ++i)
    {
        if(x%i == 0)
            return false;
    }
    return true;
}

int alp[26] = {0};
string s;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    clock_t start, end;
    start = clock();

    int t = 1;
    cin >> t;
    while(t--)
    {
        ll n, s, k;
        cin >> n >> s >> k;

        set<ll> st;

        for(int i=0 ; i<k ; ++i)
        {
            ll x;
            cin >> x;

            st.insert(x);
        }

        ll x = LONG_MAX, y = LONG_MAX;
        for(int i=s ; i<=n ; ++i)
        {
            if(st.find(i) == st.end())
            {
                x = i-s;
                break;
            }
        }
        for(int i=s-1; i>=1 ; --i)
        {
            if(st.find(i) == st.end())
            {
                y = s-i;
                break;
            }
        }
        cout << min(x, y) << endl;
    }
    end = clock();
    /*f = 0 '1'
    f = 1 '0'*/
    /*double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
    cout << "\nTime taken by program is : " << fixed
         << time_taken << setprecision(5);
    cout << " sec " << endl;*/

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0}, ans1=-1, ans2=-1;
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1 ; i>=0 ; --i)
        {
            if(a[i] == 0)
            {
                ans1 =  s-1-i;
                break;
            }
        }
        for(int i=s-1 ; i<n ; ++i)
        {
            if(a[i] == 0)
            {
                ans2 = i-s+1;
                break;
            }
        }
        if(ans1 = -1)
            cout << ans2;
        else
            cout << min(ans1, ans2);
        cout << endl;
    }
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin >> t;

    while(t--)
    {
        int n ,s ,k;
        cin >> n >> s >> k;

        int a[n]={0};
        for(int i=0 ; i<k ; ++i)
        {
            int x;
            cin >> x;
            a[x-1]++;
        }

        for(int i=s-1,j=s-1 ; i>=0,j<n ; )
        {
            if(a[i] == 0)
            {
                cout << s-1-i;
                break;
            }
            if(a[j] == 0)
            {
                cout << j-s+1;
                break;
            }

            if(i != 0)  i--;
            if(j != n-1)  j++;
        }
        cout << endl;
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std ;
int a[1002] ;
int main()
{
    long long t,n,s,k,m,xu,xd,x,j ;
    cin >> t ;
    while (t){
        x=0;
        cin >> n >> s >> k;
        for (int i = 0; i < k; i++) {
            cin >> a[i] ;
        }
        sort (a,a+k) ;
        m=upper_bound(a,a+k,s)-a;
        if (a[m-1]==s){
            j=m-1;
            xd=xu=10000000;
            for (int i = a[m-1]; i <= a[k-1]+(n>a[-1]); i++) {
                if (a[j]!=i){
                    xu=i-a[m-1] ;
                    break ;
                }
                else{
                    j++;
                }
            }
            j=m-1;
            for (int i = a[m-1]; i >= a[0]; i--) {
                if (a[j]!=i){
                    xd=a[m-1]-i ;
                    break ;
                }
                if(a[j]==i){
                    j--;
                }
            }
            x=min(xu,xd);
        }
        cout << x <<"\n";
        t--;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std ;
int a[1002] ;
int main()
{
    long long t,n,s,k,m,xu,xd,x,j ;
    cin >> t ;
    while (t){
        x=0;
        cin >> n >> s >> k;
        for (int i = 0; i < k; i++) {
            cin >> a[i] ;
        }
        sort (a,a+k) ;
        m=upper_bound(a,a+k,s)-a;
        if (a[m-1]==s){
            j=m-1;
            for (int i = a[m-1]; i <= a[k-1]+1; i++) {
                if (a[j]!=i){
                    xu=i-a[m-1] ;
                    break ;
                }
                else{
                    j++;
                }
            }
            j=m-1;
            for (int i = a[m-1]; i >= a[0]; i--) {
                if (a[j]!=i){
                    xd=a[m-1]-i ;
                    break ;
                }
                if(a[j]==i){
                    j--;
                }
            }
            x=min(xu,xd);
        }
        cout << x <<"\n";
        t--;
    }

    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    for(i=0;i<=k-2;i++)
      {  if(a[i]!=(a[i+1]-1))
          {
            num=min(abs(c-(a[i+1]-1)),abs(c-(a[i]+1)));
          //  cout<<"num "<<num<<e;
            m=min(m,num);
            //cout<<"m"<<m<<e;
            flag=0;
          }
      }

     {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<n)
         m=min(m,abs(a[k-1]+1-c));
     }
    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int binary(ll a[],ll l,ll r,ll num,ll n){
    ll a2,a1;
    while(l<=r){
      // cout<<l<<r<<e;
      ll  m=(l+r)/2;
        if(a[m]==num){
            return 0;
        }
        if(a[m]>num){
            r=m-1;
        }
        else
           l=m+1;
    }
    //cout<<l<<r<<e;
    if(l<n&&l>=0)
      a1=abs(num-a[l]);
    if(r>=0&&r<n)
      a2=abs(num-a[r]);
    return min(a1,a2);
    //return min((if(l>=0)?abs(num-a[l]);:INT_MIN);,(if(r>=0)?abs(num-a[r]);:INT_MIN;));
}
int main()

{
    int t;
    cin>>t;
    while(t--){
        ll n,c,k,a[MAX]={0},ans,i,presense[MAX]={0},j,b[MAX];
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    {cin>>a[i];
     presense[a[i]]=1;
    }
    j=0;
    for(i=1;i<=n;i++)
      { if(presense[i]==0)
         {b[j]=i;j++;
         }
      }
    //   for(i=0;i<j;i++)
    //    cout<<b[i]<<" ";
    ans=binary(b,0,j-1,c,n);

    cout<<ans<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int binary(ll a[],ll l,ll r,ll num){
    while(l<=r){
     //  cout<<l<<r<<e;
      ll  m=(l+r)/2;
        if(a[m]==num){
            return 0;
        }
        if(a[m]>num){
            r=m-1;
        }
        else
           l=m+1;
    }
    //cout<<l<<r<<e;
    return abs(num-a[l]);
    //return min((if(l>=0)?abs(num-a[l]);:INT_MIN);,(if(r>=0)?abs(num-a[r]);:INT_MIN;));
}
int main()

{
    int t;
    cin>>t;
    while(t--){
        ll n,c,k,a[MAX]={0},ans,i,presense[MAX]={0},j,b[MAX];
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    {cin>>a[i];
     presense[a[i]]=1;
    }
    j=0;
    for(i=1;i<=n;i++)
      { if(presense[i]==0)
         {b[j]=i;j++;
         }
      }
    //   for(i=0;i<j;i++)
    //   cout<<b[i]<<" ";
    if(j!=0)
    ans=binary(b,0,j-1,c);
    else
    ans=0;
    cout<<ans<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)

int main()

{
    int t;
    cin>>t;
    while(t--){
        ll n,c,k,a[MAX]={0},ans,i,presense[MAX]={0},j,m=INT_MAX;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    {cin>>a[i];
     presense[a[i]]=1;
    }
    j=0;
    if(n-1>1000)
     n=1000;
    for(i=1;i<=n;i++)
      { if(presense[i]==0)
          m=min(m,abs(c-i));
      }
    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)

int main()

{
    int t;
    cin>>t;
    while(t--){
        ll n,c,k,a[MAX]={0},ans,i,presense[MAX]={0},j,m=INT_MAX;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    {cin>>a[i];
     presense[a[i]]=1;
    }
    j=0;
    for(i=1;i<=n;i++)
      { if(presense[i]==0)
          m=min(m,abs(c-i));
      }
    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num,ans1,ans2;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    num=c;
    while(num>1){
        if(binary_search(a,a+k,num)==1){
            num--;
        }
        else
        { ans1=num;
          break;
        }
    }
    num=c;
    while(num<1000000000){
        if(binary_search(a,a+k,num)==1){
            num++;
        }
        else
        { ans2=num;
          break;
        }
    }
    m=min(ans1,ans2);
      {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<1000000000)
         m=min(m,abs(a[k-1]+1-c));
     }

    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    // for(i=0;i<k;i++)
    // cout<<a[i]<<" ";
    for(i=0;i<=k-2;i++)
      {  if(a[i]!=(a[i+1]-1))
          {
            num=min(abs(c-(a[i+1]-1)),abs(c-(a[i]+1)));
            m=min(m,num);
            flag=0;
          }
      }

     {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<1000000000)
         m=min(m,abs(a[k-1]+1-c));
     }
    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num,ans1,ans2,flag2=0,flag1=0;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    num=c;
    while(num>1){
        if(binary_search(a,a+k,num)==1){
            num--;
        }
        else
        { ans1=num;
          flag1=1;
          break;
        }
    }
   // cout<<"num "<<num<<e;
    num=c;
    while(num<n){
        if(binary_search(a,a+k,num)==1){
            num++;
        }
        else
        { ans2=num;
          flag2=1;
          break;
        }

    }
    if(flag1==1)
    m=min(m,abs(c-ans1));
    abs(c-ans1);
    if(flag2==1)
    m=min(m,abs(c-ans2));
      {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<n)
         m=min(m,abs(a[k-1]+1-c));
      }

    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    // for(i=0;i<k;i++)
    // cout<<a[i]<<" ";
    for(i=0;i<=k-2;i++)
      {  if(a[i]!=(a[i+1]-1))
          {
            num=min(abs(c-(a[i+1]-1)),abs(c-(a[i]+1)));
            m=min(m,num);
            flag=0;
          }
      }

     {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<100000000)
         m=min(m,abs(a[k-1]+1-c));
     }
    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1,num,ans1,ans2,flag2=0,flag1=0;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    num=c;
    while(num>1){
        if(binary_search(a,a+k,num)==1){
            num--;
        }
        else
        { ans1=num;
          flag1=1;
          break;
        }
    }
   // cout<<"num "<<num<<e;
    num=c;
    while(num<n){
        if(binary_search(a,a+k,num)==1){
            num++;
        }
        else
        { ans2=num;
          flag2=1;
          break;
        }

    }
   // cout<<"num "<<num<<e;
    if(flag1==1)
    m=min(m,abs(c-ans1));
    abs(c-ans1);
    if(flag2==1)
    m=min(m,abs(c-ans2));
   // cout<<"m "<<m<<e;
      {   if(a[0]>1)
         m=min(m,abs(a[0]-1-c));
         if(a[k-1]<n)
         m=min(m,abs(a[k-1]+1-c));
      }

    cout<<m<<e;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define print(a,n) for(int i=1;i<=n;i++) {cout<<a[i]<<" ";} cout<<"\n";
#define e '\n'
#define pb push_back
#define f  first
#define s  second
#define MAX 100006
#define p 1000000007
#define PI 3.141592653589793
#define mk make_pair
#define MEM(a,b) memset(a,(b),sizeof(a))
typedef pair<ll,ll> pii;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<ll, ll> pll;
#define rep(i,n) for(i=0;i<n;i++)
#define fu(i,a,n) for(i=a;i<=n;i++)
#define fd(i,n,a) for(i=n;i>=a;i--)
#define gi(n) scanf("%d",&n)
#define gl(n) scanf("%lld",&n)
#define pl(n) printf("%lld",n)
#define pi(n) printf("%d",n)
int main()
{
    int t;
    cin>>t;
    while(t--){
    ll n,c,k,a[MAX]={0},ans,i,m=INT_MAX,flag=1;
    cin>>n>>c>>k;
    for(i=0;i<k;i++)
    cin>>a[i];
    sort(a,a+k);
    if(k==1)
      {if(a[0]==c) cout<<"1"<<e;
       else cout<<"0"<<e;
       continue;
      }
    for(i=0;i<=k-2;i++)
      {  if(a[i]!=(a[i+1]-1))
          {
            m=min(abs(c-(a[i+1]-1)),abs(c-(a[i]+1)));
            flag=0;
          }
      }
    if(flag==1)
    m=min(min(m,abs(a[0]-1-c)),abs(a[k-1]+1-c));
    cout<<m<<e;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[10000000],fr[1000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
			mp[ar[i]]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll fr[1000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[100000000],fr[100000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
			mp[ar[i]]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x,mn1=1e9,mn2=1e9;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = s+1; i <= n; i++)
		{
			if (mp[i] == 0)
			{
				mn1 = min(mn1, abs(s - i));
			}
		}
		for (int i = s; i >= 1; i--)
		{
			if (mp[i] == 0)
			{
				mn2 = min(mn2, abs(s - i));
			}
		}
		cout << min(mn1,mn2 )<< endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x,mn1=1e9,mn2=1e9;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = s; i <= n; i++)
		{
			if (mp[i] == 0)
			{
				mn1 =  abs(s - i);
			}
		}
		for (int i = s; i >= 1; i--)
		{
			if (mp[i] == 0)
			{
				mn2 = abs(s - i);
			}
		}
		cout << min(mn1,mn2 )<< endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		vector<ll> fr(n+1,0);
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x,mn1=1e9,mn2=1e9;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = s; i <= n; i++)
		{
			if (mp[i] == 0)
			{
				mn1 = min(mn1, abs(s - i));
			}
		}
		for (int i = s+1; i >= 1; i--)
		{
			if (mp[i] == 0)
			{
				mn2 = min(mn2, abs(s - i));
			}
		}
		cout << min(mn1,mn2 )<< endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[1000000],fr[1000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;
			ll y = i;
			for (int j = 0; j < x; j++)
			{
				if (y == ar[j])
				{
					tt = 1;
					break;
				}
			}
			if (tt)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x,mn1=1e9,mn2=1e9;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = s; i <= n; i++)
		{
			if (mp[i] == 0)
			{
				mn1 = min(mn1, abs(s - i));
			}
		}
		for (int i = s; i >= 1; i--)
		{
			if (mp[i] == 0)
			{
				mn2 = min(mn2, abs(s - i));
			}
		}
		cout << min(mn1,mn2 )<< endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[10000000],fr[10000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
			mp[ar[i]]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x,mn1=1e9,mn2=1e9;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = s; i <= n; i++)
		{
			if (mp[i] == 0)
			{
				mn1 = abs(s - i); break;
			}
		}
		for (int i = s; i >= 1; i--)
		{
			if (mp[i] == 0)
			{
				mn2 = abs(s - i); break;
			}
		}
		cout << min(mn1,mn2 )<< endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll fr[10000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			ll r;
			cin >> r;
			mp[r]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[1000000],fr[1000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
			mp[ar[i]]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<sstream>
#include<set>
#include<assert.h>
#define all(x) x.begin(),x.end()
#define PI 3.14159265359
#define endl "\n"
#define ll long long
using namespace std;
void Ali()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll ar[10000000],fr[100000000];
int main()
{
	Ali();
	ll t; cin >> t; while (t--){
		ll n, s, x;
		cin >> n >> s >> x;
		map <ll, ll>mp;
		for (int i = 0; i < x; i++)
		{
			cin >> ar[i];
			mp[ar[i]]++;
		}
		for (int i = 1; i <= n; i++)
		{
			ll tt = 0;

			if (mp[i]==1)
			{
				fr[i] = 1;
			}
			else
				fr[i] = 0;
		}
	/*	for (int i = 1; i <= n; i++)
		{
			cout << fr[i] << " ";
		}*/
		ll ind, w = 1e9;
		for (int i = 1; i <= n; i++)
		{
			if (fr[i] == 0)
			{
				ind = i;
				w = min(w, abs(s - i));
			}
		}
		cout << w << endl;
	}
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
map<ll,ll>marks;
int main(){
	speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		marks.clear();
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
bool marks[10000];
int main(){
	speed;
	int t,n,s,k,i;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		memset(marks,false,sizeof marks);
		int ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=true;
		}
		if(marks[s]==false){
			cout<<0<<endl;
		}
		else {
			int r,l;
			r = s;
			l = s;
			while(marks[r]==true and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==true and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
//map<ll,ll>marks;
bool marks[10000];
int main(){
	speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		//marks.clear();
		memset(marks,false,sizeof marks);
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
bool marks[10000];
int main(){
	speed;
	int t,n,s,k,i;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		memset(marks,false,sizeof marks);
		int ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=true;
		}
		if(marks[s]==false){
			cout<<0<<endl;
		}
		else {
			int r,l;
			r = s;
			l = s;
			while(marks[r]==true){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==true and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
int main(){
	//speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		ll marks[10000];
		memset(marks,0,sizeof marks);
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
map<ll,ll>marks;
int main(){
	//speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		marks.clear();
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
map<ll,ll>marks;
int main(){
	speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		marks.clear();

		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
int main(){
	speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		ll marks[k];
		memset(marks,0,sizeof marks);
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=1;
		}
		if(marks[s]==0){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==1 and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==1 and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define 	ll long long int
#define 	pb push_back
#define 	mx 2147483648
#define 	mn -2147483648
#define 	speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double pi = acos(-1.0);
const ll inf = 1e17 + 7;
int main(){
	speed;
	ll t,n,s,k,i,r,l;
	cin>>t;
	while(t--){
		cin>>n>>s>>k;
		ll marks[k];
		memset(marks,false,sizeof marks);
		ll ar[k];
		for(i=0;i<k;i++){
			cin>>ar[i];
			marks[ar[i]]=true;
		}
		if(marks[s]==false){
			cout<<0<<endl;
		}
		else {

			r = s;
			l = s;
			while(marks[r]==true and r<n){
				r++;
				//cout<<r<<endl;
			}
			while(marks[l]==true and l!=0){
				l--;
			}
			if(l==0){
				cout<<r-s<<endl;
			}
			else if(r>=n){
				cout<<s-l<<endl;
			}
			else
			cout<<min(r-s,s-l)<<endl;
		}
	}

	return 0;
}