//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                else if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    int n;
    int s;
    int k;
    cin>>n>>s>>k;
    vector<int>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    for (int m = 0; m < INF; ++m) {
        if (s+m<=n){
            if (vector_finder(closed,s+m)) continue;
            else return m;
        }
        if (s-m >=1){

            if (vector_finder(closed,s-m)) continue;
            else return m;
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    int n;
    int s;
    int k;
    cin>>n>>s>>k;
    vector<int>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 0; i < k; ++i) {
        if ( s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(closed[st]-ans >= 1 || closed[st]+ans <=n) {
                if ((st+ans >k ) && (s+ans <=n )) return ans;
                if(st-ans <0 &&s-ans >=1) return ans;
                if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                else if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    int n;
    int s;
    int k;
    cin>>n>>s>>k;
    vector<int>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    for (int i = s; i <= n; ++i) {

        if (vector_finder(closed,i)) continue;
        else return i-s;
    }
    for (int j = s; j >=1 ; --j) {
        if (vector_finder(closed,j)) continue;
        else return s-j;
    }
}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( closed[i-1] <= s && s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >= k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                else if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( closed[i-1] <= s && s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >= k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if(st + ans < k){
                    if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                }
                if(st -ans >= 0){
                    if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                }
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int vector_finder(std::vector<int> vec, int number) {
    auto itr = std::find(vec.begin(), vec.end(), number);
    size_t index = std::distance( vec.begin(), itr );
    if (index != vec.size()) { // 発見できたとき
        return 1;
    }
    else { // 発見できなかったとき
        return 0;
    }
}

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( closed[i-1] <= s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >= k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                else if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( closed[i-1] <= s && s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >= k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if(st+ans < k){
                    if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                }
                if(st -ans > 0){
                    if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                }
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


//
// Created on 2020/01/19.
//

//#include <bits/stdc++.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;
using gragh = vector<vector<int>>;

int solve(){
    ll n;
    ll s;
    ll k;
    cin>>n>>s>>k;
    vector<ll>closed(k);
    for (int l = 0; l < k; ++l) {
        cin>>closed[l];
    }
    sort(all(closed));
    if(k==1){
        if(s!=closed[0])return 0;
        else return 1;
    }
    for (int i = 1; i < k; ++i) {
        if ( closed[i-1] <= s && s <= closed[i]){
            if(closed[i-1] != s && closed[i] != s) return 0;
            int st;
            if (closed[i-1] == s) st = i-1;
            else st = i;
            int ans = 1;
            while(true) {
                if ((st+ans >= k ) && (s+ans <=n )) return ans;
                if(st-ans <0 && s-ans >=1) return ans;
                if(st+ans >=k){
                    if (s+ans != closed[st+ans] && (s+ans <=n )) return ans;
                }
                if(s-ans>= 1){
                    if(s-ans != closed[st-ans]&& ( s-ans >=1)) return ans;
                }
                ans ++;
            }
        }
    }

}

int main() {
    int ans;
    cin>>ans;
    for (int i = 0; i < ans; ++i) {
        cout<<solve()<<endl;
    }

}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		set<int>se;
		int n , s, k;
		cin >> n >> s >> k;
		for(int j=0; j<k; j++){
			int a;
			cin >> a;
			se.insert(a);
		}
		int ans = INT_MAX;
		for(int j=1; j<=s; j++){
			if(se.find(j) == se.end()){
				ans  = min(ans , s - j);
			}
		}

		for(int j=s; j <=n; j++){
			if(se.find(j) == se.end()){
				ans  = min(ans , j - s);
			}
		}

		cout << ans << endl;;
	}

	return 0;

}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		set<int>se;
		int n , s, k;
		cin >> n >> s >> k;
		for(int j=0; j<k; j++){
			int a;
			cin >> a;
			se.insert(a);
		}
		int ans = INT_MAX;
		for(int j=s; j>=1; j--){
			if(se.find(j) == se.end()){
				ans  = min(ans , s - j);
				break;
			}
		}

		for(int j=s; j <=n; j++){
			if(se.find(j) == se.end()){
				ans  = min(ans , j - s);
				break;
			}
		}

		cout << ans << endl;;
	}

	return 0;

}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		long long n , s , k;
		cin >> n >> s >> k;

		bool arr[n+1];
		memset(arr,true,sizeof(arr));

		long long ans = LONG_LONG_MAX;
		for(long long j=1; j<=k; j++){
			long long b;
			cin >> b;
			arr[b] = false;
		}
		for(long long j=1; j<=n; j++){
			if(arr[j]){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		long long n , s , k;
		cin >> n >> s >> k;
		s--;
		k = min(n-1, 1000);
		long long arr[n];
		for(long long j=0; j<n; j++)
			arr[j] = 1;

		long long ans =LONG_LONG_MAX;
		for(long long j=1; j<=k; j++){
			long long b;
			cin >> b;
			arr[b-1] = 0;
		}
		for(long long j=0; j<n; j++){
			if(arr[j] == 1){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		long long n , s , k;
		cin >> n >> s >> k;
		long long cek = 1000;
		k = min(n-1,cek);
		s--;
		long long arr[n];
		for(long long j=0; j<n; j++)
			arr[j] = 1;

		long long ans =LONG_LONG_MAX;
		for(long long j=1; j<=k; j++){
			long long b;
			cin >> b;
			arr[b-1] = 0;
		}
		for(long long j=0; j<n; j++){
			if(arr[j] == 1){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		int64_t n , s , k;
		cin >> n >> s >> k;
		s--;

		bool arr[n];
		memset(arr,true,sizeof(arr));

		int64_t ans = INT_MAX;
		for(int64_t j=0; j<k; j++){
			int b;
			cin >> b;
			arr[b-1] = false;
		}
		for(int64_t j=0; j<n; j++){
			if(arr[j]){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		int n , s , k;
		cin >> n >> s >> k;
		s--;
		int arr[n];
		for(int j=0; j<n; j++)
			arr[j] = 1;

		int ans =INT_MAX;
		for(int j=1; j<=k; j++){
			int b;
			cin >> b;
			arr[b-1] = 0;
		}
		for(int j=0; j<n; j++){
			if(arr[j] == 1){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		long long n , s , k;
		cin >> n >> s >> k;
		s--;
		long long arr[n];
		for(long long j=0; j<n; j++)
			arr[j] = 1;

		long long ans =LONG_LONG_MAX;
		for(long long j=1; j<=k; j++){
			long long b;
			cin >> b;
			arr[b-1] = 0;
		}
		for(long long j=0; j<n; j++){
			if(arr[j] == 1){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cout << MOD << endl;
	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		int n , s , k;
		cin >> n >> s >> k;
		s--;
		int arr[n];
		memset(arr,1,sizeof(arr));

		int ans =INT_MAX;
		for(int j=1; j<=k; j++){
			int b;
			cin >> b;
			arr[b-1] = 0;
		}
		for(int j=0; j<n; j++){
			if(arr[j] == 1){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define endl "\n"

const int MOD = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		long long n , s , k;
		cin >> n >> s >> k;

		bool arr[n+1];
		memset(arr,true,sizeof(arr));

		long long ans = LONG_LONG_MAX;
		for(long long j=1; j<=k; j++){
			int b;
			cin >> b;
			arr[b] = false;
		}
		for(long long j=1; j<=n; j++){
			if(arr[j]){
				ans = min(ans, abs(j - s));
			}
		}

		cout << ans << "\n";
	}

	return 0;
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                          ll x, f=0, ans=0;

                            unordered_set<ll>st;

                          st.clear();

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

                               st.insert(x);
                         }

                         if(st.find(s)==st.end())
                         {
                             cout<<0<<endl;

                                continue;
                         }

                          for(i=1;i<=n/2;i++)
                        {
                            ans++;

                            if(st.find(s-i)==st.end() && (s-i) >=1 || st.find(s+i)==st.end() && (s+i)<=n)
                            {
                            //    ans++;

                                  cout<<ans<<endl;

                                    break;
                            }
                        }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   st.clear();

                   for(i=1;i<=n;i++)
                      st.insert(i);

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

                             st.erase(x);
                     }

                     if(st.find(s)!=st.end() || st.size()==0)
                     {
                         cout<<0<<endl;
                           continue;
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               if(check!=0)
                              cout<<min(it-s,s-check)<<endl;

                               else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   for(i=1;i<=n;i++)
                      st.insert(i);

                  ll a[k];

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

                             st.erase(x);
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               if(check!=0)
                              cout<<min(it-s,s-check)<<endl;

                               else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

                     if(!f)
                       cout<<0<<endl;

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                   ll dp[n];

                   for(i=1;i<=n;i++)
                      dp[i]=1;

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

                             dp[x]=0;
                     }

                     ll ans1=1e10,ans2=1e10,f=0, ans=1e10;

                     for(i=1;i<=n;i++)
                     {
                         if(dp[i])
                         {
                             if(i==s)
                              {
                                  f=1;
                                  ans=0;
                                    break;
                              }

                              if(i>s)
                              {
                                 ans1=min(ans1,(i-s));
                              }

                              if(i<s)
                               ans2=min(ans2,(s-i));
                         }
                     }

                     cout<<min(ans1,min(ans,ans2))<<endl;
            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   for(i=1;i<=n;i++)
                      st.insert(i);

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

                             st.erase(x);
                     }

                     if(st.find(s)!=st.end() || st.size()==0)
                     {
                         cout<<0<<endl;
                           continue;
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               if(check!=0)
                              cout<<min(it-s,s-check)<<endl;

                               else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   for(i=1;i<=n;i++)
                      st.insert(i);

                  ll a[k];

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

                             st.erase(x);
                     }

                     if(st.find(s)!=st.end())
                     {
                         cout<<0<<endl;
                           continue;
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               if(check!=0)
                              cout<<min(it-s,s-check)<<endl;

                               else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                          ll x, f=0, ans=0;

                            unordered_set<ll>st;

                          st.clear();

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

                               st.insert(x);
                         }

                         if(st.find(s)==st.end())
                         {
                             cout<<0<<endl;

                                continue;
                         }

                          for(i=1;i<=n;i++)
                        {
                            ans++;

                            if((st.find(s-i)==st.end() && (s-i) >=1 )||( st.find(s+i)==st.end() && (s+i)<=n))
                            {
                            //    ans++;

                                  cout<<ans<<endl;

                                    break;
                            }
                        }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   for(i=1;i<=n;i++)
                      st.insert(i);

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

                             st.erase(x);
                     }

                     if(st.find(s)!=st.end() || st.size()==0)
                     {
                         cout<<0<<endl;
                           continue;
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               //if(check!=0)
                              //cout<<min(it-s,s-check)<<endl;

                              // else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                  set<ll>st;

                   st.clear();

                   for(i=1;i<=n;i++)
                      st.insert(i);

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

                             st.erase(x);
                     }

                     if(st.find(s)!=st.end() || st.size()==0)
                     {
                         cout<<0<<endl;
                           continue;
                     }

                     ll f=0, check=0;

                     for(auto it : st)
                     {
                         if(it>s)
                         {
                             f=1;

                               if(check!=0)
                              cout<<min(it-s,s-check)<<endl;

                               else
                                  cout<<(it-s)<<endl;

                              break;
                         }

                         if(it<s)
                          check=it;
                     }

                     if(!f)
                       cout<<(s-check)<<endl;

            }
}


/*
                                                          GHOSTRIDER
*/

#include<bits/stdc++.h>
#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define all(x) x.begin(),x.end()
#define p pair<ll,ll>
#define in insert
#define endl '\n'
#define PI  (double)(3.14159265358979323846264338327950)
using namespace std;
typedef long long ll;

 const ll mod = 1e9+7;

 ll i,j;

 ll const size = 1e5;

  ll power(ll x, ll y)
{
    ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = res*x;

        y = y>>1;
        x = x*x;
    }
    return res;
}

  ll modd(string num, ll a)
{

    int res = 0;

    for (int i = 0; i < num.length(); i++)
         res = (res*10 + (int)num[i] - '0') %a;

    return res;
}

 int check_xor(int n)
 {
     if((n%4)==0)
      return n;

      else if((n%4) == 1)
       return 1;

       else if((n%4) == 2)
        return n+1;

        else
          return 0;
 }

vector<ll> sieve(ll n)
 {
     vector<bool>prime(n+1,true);

     prime[0] = false;
     prime[1] = false;

     ll range = sqrt(n);

      for(i=2;i<=range; i++)
      {
          if(prime[i])
          {
              for(j=i*2; j<=n;j=j+i)
              {
                  prime[j] = false;
              }
          }
      }

       vector<ll>ans;

       for(i=0;i<n;i++)
       {
           if(prime[i])
            ans.pb(i);
       }

      return ans;
 }

 bool iszero(ll n)
 {
     return n == 0;
 }

 vector<ll>sieve_range( ll s, ll e)
 {
     vector<ll>v1 = sieve(s);
     vector<ll>v2 = sieve(e);

     vector<ll>ans(e-s);

     set_difference(v2.begin(), v2.end(),v1.begin(), v1.end(), ans.begin() );

       auto it = remove_if(ans.begin(), ans.end(), iszero);

     ans.resize(it-ans.begin());

     return ans;
 }

int main()
{
    Fast;

        ll t;
          cin>>t;

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

                   unordered_set<ll>st;

                     st.clear();

                       for(i=1;i<=n;i++)
                          st.insert(i);

                          ll x;

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

                               st.erase(x);
                         }

                         ll ans=n+1;

                         for(auto it : st)
                         {
                            ans=min(ans,abs(it-s));
                         }

                     cout<<ans<<endl;
            }
}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  cin>>n>>m>>k;
  ll ans ;
  int check[n+1];
  fr(i,1,n+1)
  check[i]=1;
  fr(i,0,k){
    cin>>a[i];
    check[a[i]] = 0;

  }
  // fr(i,1,7)
  // cout<<check[i]<<' ';
  fr(i,m,n+1)
    if(check[i]){
      ans = abs(i-m);
      break;
    }
  for(i=m;i>=1;i--)
    if(check[i]){
      ans = min(abs(i-m),ans);
      break;
    }
  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;
fast

	cin>>n;
	ld ans = 0;
	for(i=n;i>0;i--){
		ans = ans + 1LL/(ld)i;
	}
	cout << setprecision(12) << fixed<<ans;

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];
int check[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  mem(check,0);
  cin>>n>>m>>k;
  ll ans = 0;

  fr(i,0,k)
  {
    cin>>a[i];
    check[abs(a[i] - m)]++;
  }
  fr(i,0,k)
  {
    if(check[i]>1){
      ans++;
      continue;
    }
    if(check[i]==1 && i==0){
      ans++;
      continue;
    }
    if(check[i]>0 && i>=m)
      ans++;
    if(check[i]==1 && i<m){
      ans = i;
      break;
    }
    if(check[i]==0){
      ans = i;
      break;
    }

  }

  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  cin>>n>>m>>k;
  ll ans = INT_MIN ;
  int check[n+1];
  fr(i,1,n+1)
  check[i]=1;
  fr(i,0,k){
    cin>>a[i];
    check[a[i]] = 0;

  }
  // fr(i,73,79)
  // cout<<check[i]<<' ';
  fr(i,m,n+1)
    if(check[i]){
      ans = abs(i-m);
      break;
    }
  for(i=m;i>=1;i--)
    if(check[i]){
      ans = min(abs(i-m),ans);
      break;
    }
  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];
int check[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  mem(check,0);
  cin>>n>>m>>k;
  ll ans = 0;

  fr(i,0,k)
  {
    cin>>a[i];
    check[abs(a[i] - m)]++;
  }
  fr(i,0,k)
  {
    if(check[i]>1){
      //ans++;
      continue;
    }
    if(check[i]==1 && i==0){
      //ans++;
      continue;
    }
    if(check[i]>0 &&( i>=m || i>=(n-m))){
      //ans++;
      continue;
    }
    if(check[i]==1 && (i<m|| i<(n-m))){
      ans = i;
      break;
    }
    if(check[i]==0){
      ans = i;
      break;
    }

  }

  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  cin>>n>>m>>k;
  ll ans = INT_MAX ;
  int check[n+1];
  fr(i,1,n+1)
  check[i]=1;
  fr(i,0,k){
    cin>>a[i];
    check[a[i]] = 0;

  }
  fr(i,m,n+1)
    if(check[i]){
      ans = abs(i-m);
      break;
    }
  for(i=m;i>=1;i--)
    if(check[i]){
      ans = min(abs(i-m),ans);
      break;
    }
  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

//ll check[2][MAX];
ll a[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output_3.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;
fast

  testcase
  {
    cin>>n>>m>>k;
    ll ans = INT_MAX;
    map<ll, ll> mp;
    fr(i,0,k)
    {
      cin>>a[i];
      mp[a[i]]++;
    }
    fr(i,m,n+1)
    {
      if(mp.find(i) == mp.end())
      {
        ans = min(ans,i-m);
        break;
      }
    }
    for(i=m;i>0;i--)
    {
      if(mp.find(i) == mp.end())
      {
        ans = min(ans, m-i);
        break;
      }
    }
    cout<<ans;
    cout<<khtm;
  }

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


/*
Author-KUSHAGRA SINGH
Birla Institute of Technology Mesra
CS K18
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//#define mp make_pair
#define pb emplace_back
#define vii vector<int>
#define vll vector<ll>
#define vpll vector< pair <ll,ll> >
#define pll pair <ll,ll>
#define pq priority_queue< int >
#define khtm "\n"
#define F first
#define S second
#define fr(i,a,n) for(i=a;i<n;i++)
#define mem(arr,l) memset(arr,l,sizeof arr)
#define ALL(x) x.begin(), x.end()
#define frr(i,a,n) for(i=n-1;i>=a;i--)
#define mod 1000000007
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define testcase ll T; cin>>T; while(T--)
const int MAX = 1e6 +2;
const int inf = (int) 1e9;
/*
important builtin functions
__gcd(value1, value2)
__builtin_ffs(x)
  Number of leading zeroes: __builtin_clz(x)
  Number of trailing zeroes : __builtin_ctz(x)
  Number of set bits: __builtin_popcount(x)
*/

ll i,j,k,l,m,r,n;

ll a[MAX];

int main()
{

  #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//cout << setprecision(15) << fixed;

fast

testcase{
  cin>>n>>m>>k;
  ll ans = INT_MIN ;
  int check[n+1];
  fr(i,1,n+1)
  check[i]=1;
  fr(i,0,k){
    cin>>a[i];
    check[a[i]] = 0;

  }
  fr(i,73,79)
  cout<<check[i]<<' ';
  fr(i,m,n+1)
    if(check[i]){
      ans = abs(i-m);
      break;
    }
  for(i=m;i>=1;i--)
    if(check[i]){
      ans = min(abs(i-m),ans);
      break;
    }
  cout<<ans<<khtm;
}

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
    #endif

   return 0;

}


#include <bits/stdc++.h>

using namespace std;

struct Floor
{
	int pos;
	bool closed;
	Floor(int _x)
	{
		pos = _x;
		closed = 0;
	}
	Floor()
	{
		closed = 0;
	}
};

int t, n, s, k;

bool cmp(Floor x, Floor y)
{
	return x.closed < y.closed || (x.closed == y.closed && x.pos < y.pos);
}

int main()
{
	for (cin >> t; t > 0; t--)
	{
		cin >> n >> s >> k;
		vector<Floor> A(n+1);
		for (int i = 1; i <= k; i++)
		{
			int temp;
			cin >> temp;
			A[temp].closed = 1;
		}
		for (int i = 1; i <= n; i++) A[i].pos = i;
		sort(A.begin()+1, A.begin()+1+n, cmp);
		Floor up = *upper_bound(A.begin()+1, A.begin()+1+n-k, Floor(s), cmp);
		Floor low = *lower_bound(A.begin()+1, A.begin()+1+n-k, Floor(s), cmp);
		cout << min(abs(s - low.pos), abs(s - up.pos)) << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int q, n, k, s, A[1001];

bool inRange(int x, int l, int r)
{
	return l <= r && x < r && x > l;
}

int distance(int x, int l, int r)
{
	return x <= l ? l-x : x-r;
}

int main()
{
	cin >> q;
	for (; q > 0; q--)
	{
		cin >> n >> s >> k;
		for (int i = 1; i <= k; i++) cin >> A[i];
		A[0] = 0;
		A[k] = n+1;
		sort(A, A+k);
		int res = INT_MAX;
		for (int i = 1; i <= k+1; i++)
		{
			if (inRange(s, A[i-1], A[i]))
			{
				res = 0;
				break;
			}
			if (A[i]-A[i-1] > 1) res = min(distance(s, A[i-1], A[i])+1, res);
		}
		cout << res << endl;
	}

	return 0;
}


#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>
#include <functional>

using namespace std;

#define int long long

const int inf = 1e17 + 239;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int> (m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    if (n == 1) {
        int best = inf;
        for (int i = 0; i + 1 < m; i++) {
            best = min(best, abs(a[0][i] - a[0][i + 1]));
        }
        cout << best;
        return 0;
    }
    vector<vector<int>> dlt(n, vector<int> (n, -inf));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int best = inf;
            for (int k = 0; k < m; k++) {
                best = min(best, abs(a[i][k] - a[j][k]));
            }
            dlt[i][j] = best;
        }
    }
    vector<vector<vector<int>>> dp(1 << n, vector<vector<int>> (n, vector<int> (n)));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j) {
                int mask = (1 << j) | (1 << i);
                dp[mask][i][j] = dlt[i][j];
            }
        }
    }
    for (int mask = 0; mask < (1 << n); mask++) {
        if (__builtin_popcountll(mask) < 2) {
            continue;
        }
        for (int st = 0; st < n; st++) {
            for (int en = 0; en < n; en++) {
                if (!((mask >> st) & 1)) {
                    continue;
                }
                if (!((mask >> en) & 1)) {
                    continue;
                }
                if (st != en) {
                    for (int nx = 0; nx < n; nx++) {
                        if (st != nx && en != nx) {
                            int nmask = mask | (1 << nx);
                            auto cur = min(dp[mask][st][en], dlt[en][nx]);
                            dp[nmask][st][nx] = max(dp[nmask][st][nx], cur);
                        }
                    }
                }
            }
        }
    }
    int best = -inf;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j) {
                auto cur = dp[(1 << n) - 1][i][j];
                for (int k = 0; k + 1 < m; k++) {
                    cur = min(cur, abs(a[i][k] - a[j][k + 1]));
                }
                best = max(best, cur);
            }
        }
    }
    cout << best;
}


#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <stack>
#include <cassert>
#include <queue>
#include <deque>
#include <climits>
#include <cstring>
#include <random>
#include <bitset>
#include <functional>

using namespace std;

const int inf = 1e9 + 239;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int> (m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    if (n == 1) {
        int best = inf;
        for (int i = 0; i + 1 < m; i++) {
            best = min(best, abs(a[0][i] - a[0][i + 1]));
        }
        cout << best;
        return 0;
    }
    vector<vector<int>> dlt(n, vector<int> (n, -inf));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int best = inf;
            for (int k = 0; k < m; k++) {
                best = min(best, abs(a[i][k] - a[j][k]));
            }
            dlt[i][j] = best;
        }
    }
    vector<vector<vector<int>>> dp(1 << n, vector<vector<int>> (n, vector<int> (n)));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j) {
                int mask = (1 << j) | (1 << i);
                dp[mask][i][j] = dlt[i][j];
            }
        }
    }
    for (int mask = 0; mask < (1 << n); mask++) {
        if (__builtin_popcount(mask) < 2) {
            continue;
        }
        for (int st = 0; st < n; st++) {
            for (int en = 0; en < n; en++) {
                if (st != en) {
                    for (int nx = 0; nx < n; nx++) {
                        if (st != nx && en != nx) {
                            int nmask = mask | (1 << nx);
                            auto cur = min(dp[mask][st][en], dlt[en][nx]);
                            dp[nmask][st][nx] = max(dp[nmask][st][nx], cur);
                        }
                    }
                }
            }
        }
    }
    int best = -inf;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != j) {
                auto cur = dp[(1 << n) - 1][i][j];
                for (int k = 0; k + 1 < m; k++) {
                    cur = min(cur, abs(a[i][k] - a[j][k + 1]));
                }
                best = max(best, cur);
            }
        }
    }
    cout << best;
}


#include <bits/stdc++.h>
#pragma region template
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rfor(i, m, n) for (int i = (m); i >= (n); --i)
#define unless(c) if (!(c))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define range_it(a, l, r) (a).begin() + (l), (a).begin() + (r)

using namespace std;
using ll = long long;
using LD = long double;
using VB = vector<bool>;
using VVB = vector<VB>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using VD = vector<LD>;
using PII = pair<int, int>;
using VP = vector<PII>;
using PLL = pair<ll, ll>;
using VPL = vector<PLL>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQS = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1e9;
constexpr long long inf_ll = 1e18, MOD = 1000000007;
constexpr long double PI = 3.14159265358979323846, EPS = 1e-12;

// --- input --- //
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
class Input {
	static int gc() {
		return getchar_unlocked();
	}
	template <class T> static void i(T& v) {
		cin >> v;
	}
	static void i(char& v) {
		while (isspace(v = gc()))
			;
	}
	static void i(bool& v) {
		v = in<char>() != '0';
	}
	static void i(string& v) {
		v.clear();
		char c;
		for (i(c); !isspace(c); c = gc())
			v += c;
	}
	static void i(int& v) {
		bool neg = false;
		v = 0;
		char c;
		i(c);
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc())
			v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void i(long long& v) {
		bool neg = false;
		v = 0;
		char c;
		i(c);
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c); c = gc())
			v = v * 10 + (c - '0');
		if (neg) v = -v;
	}
	static void i(double& v) {
		double dp = 1;
		bool neg = false, adp = false;
		v = 0;
		char c;
		i(c);
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.')
				adp = true;
			else if (adp)
				v += (c - '0') * (dp *= 0.1);
			else
				v = v * 10 + (c - '0');
		}
		if (neg) v = -v;
	}
	static void i(long double& v) {
		long double dp = 1;
		bool neg = false, adp = false;
		v = 0;
		char c;
		i(c);
		if (c == '-') {
			neg = true;
			c = gc();
		}
		for (; isdigit(c) || c == '.'; c = gc()) {
			if (c == '.')
				adp = true;
			else if (adp)
				v += (c - '0') * (dp *= 0.1);
			else
				v = v * 10 + (c - '0');
		}
		if (neg) v = -v;
	}
	template <class T, class U> static void i(pair<T, U>& v) {
		i(v.first);
		i(v.second);
	}
	template <class T> static void i(vector<T>& v) {
		for (auto& e : v)
			i(e);
	}
	template <size_t N = 0, class T> static void input_tuple(T& v) {
		if constexpr (N < tuple_size_v<T>) {
			i(get<N>(v));
			input_tuple<N + 1>(v);
		}
	}
	template <class... T> static void i(tuple<T...>& v) {
		input_tuple(v);
	}
	struct InputV {
		int n, m;
		InputV(int _n) : n(_n), m(0) {}
		InputV(const pair<int, int>& nm) : n(nm.first), m(nm.second) {}
		template <class T> operator vector<T>() {
			vector<T> v(n);
			i(v);
			return v;
		}
		template <class T> operator vector<vector<T>>() {
			vector<vector<T>> v(n, vector<T>(m));
			i(v);
			return v;
		}
	};

public:
	static string read_line() {
		string v;
		char c;
		for (i(c); c != '\n' && c != '\0'; c = gc())
			v += c;
		return v;
	}
	template <class T> static T in() {
		T v;
		i(v);
		return v;
	}
	template <class T> operator T() const {
		return in<T>();
	}
	int operator--(int) const {
		return in<int>() - 1;
	}
	InputV operator[](int n) const {
		return InputV(n);
	}
	InputV operator[](const pair<int, int>& n) const {
		return InputV(n);
	}
	void operator()() const {}
	template <class H, class... T> void operator()(H&& h, T&&... t) const {
		i(h);
		operator()(forward<T>(t)...);
	}

private:
	template <template <class...> class, class...> struct Multiple;
	template <template <class...> class V, class Head, class... Tail> struct Multiple<V, Head, Tail...> {
		template <class... Args> using vec = V<vector<Head>, Args...>;
		using type = typename Multiple<vec, Tail...>::type;
	};
	template <template <class...> class V> struct Multiple<V> { using type = V<>; };
	template <class... T> using multiple_t = typename Multiple<tuple, T...>::type;
	template <size_t N = 0, class T> void in_multiple(T& t) const {
		if constexpr (N < tuple_size_v<T>) {
			auto& vec = get<N>(t);
			using V = typename remove_reference_t<decltype(vec)>::value_type;
			vec.push_back(in<V>());
			in_multiple<N + 1>(t);
		}
	}

public:
	template <class... T> auto multiple(int H) const {
		multiple_t<T...> res;
		while (H--)
			in_multiple(res);
		return res;
	}
} in;
#define input(T) Input::in<T>()
#define INT input(int)
#define LL input(long long)
#define STR input(string)
#define inputs(T, ...) \
	T __VA_ARGS__;     \
	in(__VA_ARGS__)
#define ini(...) inputs(int, __VA_ARGS__)
#define inl(...) inputs(long long, __VA_ARGS__)
#define ins(...) inputs(string, __VA_ARGS__)

// --- output --- //
struct BoolStr {
	const char *t, *f;
	BoolStr(const char* _t, const char* _f) : t(_t), f(_f) {}
} Yes("Yes", "No"), yes("yes", "no"), YES("YES", "NO"), Int("1", "0");
struct DivStr {
	const char *d, *l;
	DivStr(const char* _d, const char* _l) : d(_d), l(_l) {}
} spc(" ", "\n"), no_spc("", "\n"), end_line("\n", "\n"), comma(",", "\n"), no_endl(" ", "");
class Output {
	BoolStr B{Yes};
	DivStr D{spc};
	void p(int v) const {
		char buf[12]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void p(long long v) const {
		char buf[21]{};
		if (auto [ptr, e] = to_chars(begin(buf), end(buf), v); e == errc{}) {
			fwrite(buf, sizeof(char), ptr - buf, stdout);
		} else {
			assert(false);
		}
	}
	void p(bool v) const {
		p(v ? B.t : B.f);
	}
	void p(char v) const {
		putchar_unlocked(v);
	}
	void p(const char* v) const {
		fwrite_unlocked(v, 1, strlen(v), stdout);
	}
	void p(double v) const {
		printf("%.20f", v);
	}
	void p(long double v) const {
		printf("%.20Lf", v);
	}
	template <class T> void p(const T& v) const {
		cout << v;
	}
	template <class T, class U> void p(const pair<T, U>& v) const {
		p(v.first);
		p(D.d);
		p(v.second);
	}
	template <class T> void p(const vector<T>& v) const {
		rep(i, sz(v)) {
			if (i) p(D.d);
			p(v[i]);
		}
	}
	template <class T> void p(const vector<vector<T>>& v) const {
		rep(i, sz(v)) {
			if (i) p(D.l);
			p(v[i]);
		}
	}

public:
	Output& operator()() {
		p(D.l);
		return *this;
	}
	template <class H> Output& operator()(H&& h) {
		p(h);
		p(D.l);
		return *this;
	}
	template <class H, class... T> Output& operator()(H&& h, T&&... t) {
		p(h);
		p(D.d);
		return operator()(forward<T>(t)...);
	}
	template <class It> Output& range(const It& l, const It& r) {
		for (It i = l; i != r; i++) {
			if (i != l) p(D.d);
			p(*i);
		}
		p(D.l);
		return *this;
	}
	template <class T> Output& range(const T& a) {
		range(a.begin(), a.end());
		return *this;
	}
	template <class... T> void exit(T&&... t) {
		operator()(forward<T>(t)...);
		std::exit(EXIT_SUCCESS);
	}
	Output& flush() {
		fflush_unlocked(stdout);
		return *this;
	}
	Output& set(const BoolStr& b) {
		B = b;
		return *this;
	}
	Output& set(const DivStr& d) {
		D = d;
		return *this;
	}
	Output& set(const char* t, const char* f) {
		B = BoolStr(t, f);
		return *this;
	}
} out;

// --- step --- //
template <class T> struct Step {
	class It {
		T a, b, c;

	public:
		constexpr It() : a(T()), b(T()), c(T()) {}
		constexpr It(T _b, T _c, T _s) : a(_b), b(_c), c(_s) {}
		constexpr It& operator++() {
			--b;
			a += c;
			return *this;
		}
		constexpr It operator++(int) {
			It tmp = *this;
			--b;
			a += c;
			return tmp;
		}
		constexpr const T& operator*() const {
			return a;
		}
		constexpr const T* operator->() const {
			return &a;
		}
		constexpr bool operator==(const It& i) const {
			return b == i.b;
		}
		constexpr bool operator!=(const It& i) const {
			return !(b == i.b);
		}
		constexpr T start() const {
			return a;
		}
		constexpr T size() const {
			return b;
		}
		constexpr T step() const {
			return c;
		}
	};
	constexpr Step(T b, T c, T s) : be(b, c, s) {}
	constexpr It begin() const {
		return be;
	}
	constexpr It end() const {
		return en;
	}
	constexpr T start() const {
		return be.start();
	}
	constexpr T size() const {
		return be.size();
	}
	constexpr T step() const {
		return be.step();
	}
	constexpr T sum() const {
		return start() * size() + step() * (size() * (size() - 1) / 2);
	}
	operator vector<T>() const {
		return to_a();
	}
	auto to_a() const {
		vector<T> res;
		res.reserve(size());
		for (auto i : *this) {
			res.push_back(i);
		}
		return res;
	}
	using value_type = T;
	using iterator = It;

private:
	It be, en;
};
template <class T> inline constexpr auto step(T a) {
	return Step<T>(0, a, 1);
}
template <class T> inline constexpr auto step(T a, T b) {
	return Step<T>(a, b - a, 1);
}
template <class T> inline constexpr auto step(T a, T b, T c) {
	return Step<T>(a, a < b ? (b - a - 1) / c + 1 : 0, c);
}

// --- Ruby --- //
namespace Ruby {
	template <class F> struct Callable {
		F func;
		Callable(const F& f) : func(f) {}
	};
	template <class T, class F> auto operator|(const T& v, const Callable<F>& c) {
		return c.func(v);
	}

	struct Sort_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				sort(begin(v), end(v), f);
				return v;
			});
		}
		template <class T> friend auto operator|(T v, [[maybe_unused]] const Sort_impl& c) {
			sort(begin(v), end(v));
			return v;
		}
	} Sort;
	struct SortBy_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				sort(begin(v), end(v), [&](const auto& i, const auto& j) {
					return f(i) < f(j);
				});
				return v;
			});
		}
	} SortBy;
	struct RSort_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				sort(rbegin(v), rend(v), f);
				return v;
			});
		}
		template <class T> friend auto operator|(T v, [[maybe_unused]] const RSort_impl& c) {
			sort(rbegin(v), rend(v));
			return v;
		}
	} RSort;
	struct RSortBy_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				sort(begin(v), end(v), [&](const auto& i, const auto& j) {
					return f(i) > f(j);
				});
				return v;
			});
		}
	} RSortBy;
	struct Reverse_impl {
		template <class T> friend auto operator|(T v, const Reverse_impl& c) {
			reverse(begin(v), end(v));
			return v;
		}
	} Reverse;
	struct Unique_impl {
		template <class T> friend auto operator|(T v, const Unique_impl& c) {
			v.erase(unique(begin(v), end(v), end(v)));
			return v;
		}
	} Unique;
	struct Uniq_impl {
		template <class T> friend auto operator|(T v, const Uniq_impl& c) {
			sort(begin(v), end(v));
			v.erase(unique(begin(v), end(v), end(v)));
			return v;
		}
	} Uniq;
	struct Rotate_impl {
		auto operator()(int&& left) {
			return Callable([&](auto v) {
				int s = static_cast<int>(size(v));
				assert(-s <= left && left <= s);
				if (0 <= left) {
					rotate(begin(v), begin(v) + left, end(v));
				} else {
					rotate(begin(v), end(v) + left, end(v));
				}
				return v;
			});
		}
	} Rotate;
	struct Max_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				return *max_element(begin(v), end(v), f);
			});
		}
		template <class T> friend auto operator|(T v, const Max_impl& c) {
			return *max_element(begin(v), end(v));
		}
	} Max;
	struct Min_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				return *min_element(begin(v), end(v), f);
			});
		}
		template <class T> friend auto operator|(T v, const Min_impl& c) {
			return *min_element(begin(v), end(v));
		}
	} Min;
	struct MaxPos_impl {
		template <class T> friend auto operator|(T v, const MaxPos_impl& c) {
			return max_element(begin(v), end(v)) - begin(v);
		}
	} MaxPos;
	struct MinPos_impl {
		template <class T> friend auto operator|(T v, const MinPos_impl& c) {
			return min_element(begin(v), end(v)) - begin(v);
		}
	} MinPos;
	struct MaxBy_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				auto max_it = begin(v);
				auto max_val = f(*max_it);
				for (auto it = next(begin(v)); it != end(v); ++it) {
					if (auto val = f(*it); max_val < val) {
						max_it = it;
						max_val = val;
					}
				}
				return *max_it;
			});
		}
	} MaxBy;
	struct MinBy_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				auto min_it = begin(v);
				auto min_val = f(*min_it);
				for (auto it = next(begin(v)); it != end(v); ++it) {
					if (auto val = f(*it); min_val > val) {
						min_it = it;
						min_val = val;
					}
				}
				return *min_it;
			});
		}
	} MinBy;
	struct MaxOf_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				auto max_val = f(*begin(v));
				for (auto it = next(begin(v)); it != end(v); ++it) {
					if (auto val = f(*it); max_val < val) {
						max_val = val;
					}
				}
				return max_val;
			});
		}
	} MaxOf;
	struct MinOf_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				auto min_val = f(*begin(v));
				for (auto it = next(begin(v)); it != end(v); ++it) {
					if (auto val = f(*it); min_val > val) {
						min_val = val;
					}
				}
				return min_val;
			});
		}
	} MinOf;
	struct Count_impl {
		template <class V> auto operator()(const V& val) {
			return Callable([&](auto v) {
				return count(begin(v), end(v), val);
			});
		}
	} Count;
	struct CountIf_impl {
		template <class F> auto operator()(const F& f) {
			return Callable([&](auto v) {
				return count_if(begin(v), end(v), f);
			});
		}
	} CountIf;
	struct Index_impl {
		template <class V> auto operator()(const V& val) {
			return Callable([&](auto v) -> optional<int> {
				auto res = find(begin(v), end(v), val);
				return res != end(v) ? optional(res - begin(v)) : nullopt;
			});
		}
	} Index;
	struct IndexIf_impl {
		template <class F> auto operator()(const F& f) {
			return Callable([&](auto v) -> optional<int> {
				auto res = find_if(begin(v), end(v), f);
				return res != end(v) ? optional(res - begin(v)) : nullopt;
			});
		}
	} IndexIf;
	struct FindIf_impl {
		template <class F> auto operator()(const F& f) {
			return Callable([&](auto v) -> optional<typename decltype(v)::value_type> {
				auto res = find_if(begin(v), end(v), f);
				return res != end(v) ? optional(*res) : nullopt;
			});
		}
	} FindIf;
	struct Sum_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				return accumulate(next(begin(v)), end(v), f(*begin(v)), [&](const auto& a, const auto& b) {
					return a + f(b);
				});
			});
		}
		template <class T> friend auto operator|(T v, const Sum_impl& c) {
			return accumulate(begin(v), end(v), typename T::value_type());
		}
	} Sum;
	struct Includes {
		template <class V> auto operator()(const V& val) {
			return Callable([&](auto v) {
				return find(begin(v), end(v), val) != end(v);
			});
		}
	} Includes;
	struct IncludesIf_impl {
		template <class F> auto operator()(const F& f) {
			return Callable([&](auto v) {
				return find_if(begin(v), end(v), f) != end(v);
			});
		}
	} IncludesIf;
	struct RemoveIf_impl {
		template <class F> auto operator()(const F& f) {
			return Callable([&](auto v) {
				v.erase(remove_if(begin(v), end(v), f), end(v));
				return v;
			});
		}
	} RemoveIf;
	struct Each_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				for (const auto& i : v) {
					f(i);
				}
			});
		}
	} Each;
	struct Select_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				using value_type = typename decltype(v)::value_type;
				vector<value_type> res;
				for (const auto& i : v) {
					if (f(i)) res.push_back(i);
				}
				return res;
			});
		}
	} Select;
	struct Map_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				using result_type = invoke_result_t<F, typename decltype(v)::value_type>;
				vector<result_type> res;
				res.reserve(size(v));
				for (const auto& i : v) {
					res.push_back(f(i));
				}
				return res;
			});
		}
	} Map;
	struct Indexed_impl {
		template <class T> friend auto operator|(const T& v, Indexed_impl& c) {
			using value_type = typename T::value_type;
			vector<pair<value_type, int>> res;
			res.reserve(size(v));
			int index = 0;
			for (const auto& i : v) {
				res.emplace_back(i, index++);
			}
			return res;
		}
	} Indexed;
	struct AllOf_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				for (const auto& i : v) {
					if (!f(i)) return false;
				}
				return true;
			});
		}
	} AllOf;
	struct AnyOf_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				for (const auto& i : v) {
					if (f(i)) return true;
				}
				return false;
			});
		}
	} AnyOf;
	struct NoneOf_impl {
		template <class F> auto operator()(F&& f) {
			return Callable([&](auto v) {
				for (const auto& i : v) {
					if (f(i)) return false;
				}
				return true;
			});
		}
	} NoneOf;

	template <class T> auto operator*(const vector<T>& a, size_t n) {
		T res;
		for (size_t i = 0; i < n; ++i) {
			res.insert(res.end(), a.begin(), a.end());
		}
		return res;
	}
	auto operator*(string a, size_t n) {
		string res;
		for (size_t i = 0; i < n; ++i) {
			res += a;
		}
		return res;
	}
	template <class T, class U> auto& operator<<(vector<T>& a, const U& b) {
		a.insert(a.end(), all(b));
		return a;
	}
	template <class T> auto& operator<<(string& a, const T& b) {
		a.insert(a.end(), all(b));
		return a;
	}
	template <class T, class U> auto operator+(vector<T> a, const U& b) {
		a << b;
		return a;
	}
	template <class T> auto operator+(string a, const T& b) {
		a << b;
		return a;
	}
}  // namespace Ruby
using namespace Ruby;

// --- functions --- //
template <class T, class U> inline int Lower(const T& a, const U& v) {
	return lower_bound(all(a), v) - a.begin();
}
template <class T, class U> inline int Upper(const T& a, const U& v) {
	return upper_bound(all(a), v) - a.begin();
}
template <class T> inline auto Slice(const T& v, size_t i, size_t len) {
	return i < v.size() ? T(v.begin() + i, v.begin() + min(i + len, v.size())) : T();
}
template <class T> inline T Ceil(T n, T m) {
	return (n + m - 1) / m;
}
template <class T> inline T Ceil2(T n, T m) {
	return Ceil(n, m) * m;
}
template <class T> inline T Tri(T n) {
	return (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
}
template <class T> inline T nC2(T n) {
	return (n & 1) ? (n - 1) / 2 * n : n / 2 * (n - 1);
}
template <class T> inline T Mid(const T& l, const T& r) {
	return l + (r - l) / 2;
}
template <class T> inline bool chmax(T& a, const T& b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> inline bool chmin(T& a, const T& b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> inline bool inRange(const T& v, const T& min, const T& max) {
	return min <= v && v < max;
}
template <class T> inline bool isSquere(T n) {
	T s = sqrt(n);
	return s * s == n || (s + 1) * (s + 1) == n;
}
template <class T = long long> inline T BIT(int b) {
	return T(1) << b;
}
template <class T, class U = typename T::value_type> inline U Gcdv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), gcd<U, U>);
}
template <class T, class U = typename T::value_type> inline U Lcmv(const T& v) {
	return accumulate(next(v.begin()), v.end(), U(*v.begin()), lcm<U, U>);
}
template <class T> inline T Pow(T a, T n) {
	T r = 1;
	while (n > 0) {
		if (n & 1) r *= a;
		a *= a;
		n /= 2;
	}
	return r;
}
template <class T> inline T Powmod(T a, T n, T m = MOD) {
	T r = 1;
	while (n > 0) {
		if (n & 1)
			r = r * a % m, n--;
		else
			a = a * a % m, n /= 2;
	}
	return r;
}

// --- dump --- //
#if __has_include("dump.hpp")
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
#pragma endregion

// ---------------------------------------------------------------- //

int main() {
	ini(n, m);
	VVL a = in[{n, m}];

	VVL diff(n, VL(n, inf_ll)), diff2(n, VL(n, inf_ll));
	rep(i, n) rep(j, n) {
		rep(k, m) {
			chmin(diff[i][j], abs(a[i][k] - a[j][k]));
		}
		rep(k, m - 1) {
			chmin(diff2[i][j], abs(a[i][k] - a[j][k + 1]));
		}
	}
	dump(diff, diff2);

	ll ans = 0;
	rep(first, n) {
		if (first == 2) {
			// break point
		}
		VVL dp(BIT(n), VL(n, -1));
		dp[BIT(first)][first] = inf_ll;
		auto rec = [&](auto&& f, int bit, int last) {
			assert(bit & BIT(last));
			auto& res = dp[bit][last];
			if (int pop = __builtin_popcount(bit); pop == 0) {
				assert(false);
				return res;
			} else if (pop == 1) {
				assert(last == first);
				return res;
			} else if (res != -1) {
				return res;
			} else {
				res = 0;
				int bit2 = bit & ~BIT(last);
				rep(i, n) if (bit2 & BIT(i)) {
					if ((pop > 2 && i != first) || (pop == 2 && i == first)) {
						chmax(res, min(f(f, bit2, i), diff[i][last]));
					}
				}
				return res;
			}
		};

		rep(last, n) {
			if (n == 1 || first != last) {
				chmax(ans, min(rec(rec, BIT(n) - 1, last), diff2[last][first]));
			}
		}
		dump(first, dp);
	}
	out(ans);
}