#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;
ll vet[105];

bool valid(ll v){
    for(int i = 0; i < n; i++){
        ll aux = abs(vet[i] - v);
        if(aux > k) return false;
    }
    return true;
}

int main(){
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 0; i < n; i++) cin >> vet[i];
        ll l = 0, r = 1e9;
        ll res = -1;
        while(l <= r){
            ll mid = l + (r - l) / 2;
            if(valid(mid)){
                res = max(res, mid);
                l = mid + 1;
            }
            else r = mid - 1;
        }
        cout << res << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
int a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		int mn = 1e9 , mx = 0 , res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
			mn = min( mn , a[i] ) ;
			mx = max( mn , a[i] ) ;
		}
//		cout << "mn la " << mn << endl ;
		for ( int i = abs( - mn - y ) ; i >= 0 ; i-- ){
			if ( abs( mn - i ) <= y && abs( mx - i ) <= y ){
				res = i ;
				break ;
			}
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
ll a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		ll res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
		}
		sort ( a , a + x ) ;
		ll mn = a[0] , mx = a[x-1] ;
//		cout << "mn la " << mn << " " << "max la " << mx <<  endl ;
		if ( mx - ( mn + y ) <= y ){
			res = mn + y ;
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
int a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		int mn = 1e9 , mx = 0 , res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
			mn = min( mn , a[i] ) ;
			mx = max( mn , a[i ]) ;
		}
//		cout << "mn la " << mn << endl ;
		for ( int i = abs( mn + y ) ; i >= 1 ; i-- ){
			if ( abs( mn - i ) <= y && abs( mx - i ) <= y ){
				res = i ;
				break ;
			}
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
int a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		int mn = 1e9 , mx = 0 , res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
			mn = min( mn , a[i] ) ;
			mx = max( mx , a[i] ) ;
		}
//		cout << "mn la " << mn << " " << "max la " << mx <<  endl ;
		for ( int i = mn + y ; i >= 1 ; i-- ){
			if ( abs( mn - i ) <= y && abs( mx - i ) <= y ){
				res = i ;
				break ;
			}
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
int a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		int mn = 1e9 , mx = 0 , res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
			mn = min( mn , a[i] ) ;
			mx = max( mn , a[i] ) ;
		}
//		cout << "mn la " << mn << endl ;
		for ( int i = 1e9 ; i >= 0 ; i-- ){
			if ( abs( mn - i ) <= y && abs( mx - i ) <= y ){
				res = i ;
				break ;
			}
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
#define maxn 1007
#define ll long long int
#define _Goodbye_ return 0;
#define space(a,b) cout << a << " " << b << endl;
#define pb push_back
#define is insert
#define _BIGBANG_ int main
#define Friendly ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
int a[maxn] , b[maxn] ;
_BIGBANG_(){
	Friendly
	int t ;
	cin >> t ;
	while ( t-- ){
		int x , y ;
		cin >> x >> y ;
		int mn = 1e9 , mx = 0 , res = 0 ;
		for ( int i = 0 ; i < x ; i++ ){
			cin >> a[i] ;
			mn = min( mn , a[i] ) ;
			mx = max( mn , a[i] ) ;
		}
//		cout << "mn la " << mn << endl ;
		for ( int i = 1e8 ; i >= 0 ; i-- ){
			if ( abs( mn - i ) <= y && abs( mx - i ) <= y ){
				res = i ;
				break ;
			}
		}
//		cout << "res la " << " " << res << endl ;
		if ( res == 0 ){
			cout << "-1" << endl ;
		}
		else{
			cout << res << endl ;
		}
	}

	_Goodbye_
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x+k;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=2){
				for(long long j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x;
		long long l=100000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(long long j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x;
		long long l=100000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(long long j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=2){
				for(long long j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(int a[],int n,int k,int r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		int n,k;
		cin>>n>>k;
		int a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		int s=0;
		int l=100000000;
		int ret=-1;
		while(1){
			int mid=s+(l-s)/2;
			if(l-s<=3){
				if(check(a,n,k,s)&&ret<=s)ret=s;
				if(check(a,n,k,s+1)&&ret<=s+1)ret=s+1;
				if(check(a,n,k,s+2)&&ret<=s+2)ret=s+2;
				if(check(a,n,k,s+3)&&ret<=s+3)ret=s+3;
				//if(check(a,n,k,s+3)&&ret<=s+4)ret=s+3;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			int mid=(l+s)/2;
			if(l-s<=10{
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x+k;//chchch
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=2){
				for(long long j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x+k;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(long long j=s;j<s+10;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x+k;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=3){
				for(long long j=s;j<s+3;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=x+k;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=10){
				for(long long j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(int a[],int n,int k,int r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		int n,k;
		cin>>n>>k;
		int a[200];
		int x=1000000000;
		int y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		int s=x+k;//chchch
		int l=10000000001;
		int ret=-1;
		while(1){
			int mid=(l+s)/2;
			if(l-s<=2){
				for(int j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			int mid=(l+s)/2;
			if(l-s<=10){
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=0;
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=2){
				for(long long j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			int mid=s+(l-s)/2;
			if(l-s<=10){
				for(int j=s;j<s+11;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(int a[],int n,int k,int r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		int n,k;
		cin>>n>>k;
		int a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		int s=0;
		int l=100;
		int ret=-1;
		while(1){
			int mid=s+(l-s)/2;
			if(l-s<=3){
				if(check(a,n,k,s)&&ret<=s)ret=s;
				if(check(a,n,k,s+1)&&ret<=s+1)ret=s+1;
				if(check(a,n,k,s+2)&&ret<=s)ret=s+2;
				if(check(a,n,k,s+3)&&ret<=s+3)ret=s+3;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(abs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		for(int i=0;i<n;i++)
			cin>>a[i];
		long long s=0;
		long long l=100000001;
		long long ret=-1;
		while(1){
			int mid=s+(l-s)/2;
			if(l-s<=10){
				for(int j=s;j<s+1001;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
bool check(long long a[],long long n,long long k,long long r){
	for(int i=0;i<n;i++)
		if(llabs(a[i]-r)>k)return false;
	return true;
}
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int q;
	cin>>q;
	for(int t=0;t<q;t++){
		long long n,k;
		cin>>n>>k;
		long long a[200];
		long long x=1000000000;
		long long y=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
			x=min(x,a[i]);
			y=max(y,a[i]);
		}
		long long s=0;//chchch
		long long l=10000000001;
		long long ret=-1;
		while(1){
			long long mid=(l+s)/2;
			if(l-s<=2){
				for(long long j=s;j<s+2;j++)
				if(check(a,n,k,j)&&ret<=j)ret=j;
				break;
			}
			if(check(a,n,k,mid)){
				s=mid;
			}else{
				l=mid;
			}
			//cout<<s<<" "<<l<<endl;

		}
		cout<<ret<<endl;

	}

    return 0;
}


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

typedef long long ll;
typedef long double ld;
# define forn for(ll i = 0;i < n; i++)
# define pb push_back
# define sethas(set,x) (set.find(x) != set.end())
# define fill_matrix(matrix,size,value) (fill(*matrix, *matrix + size, value));
# define maxll(a,b) ((a > b) ? a : b)
# define minll(a,b) ((a > b) ? b : a)
# define triangle(n) (n*(n+1)/2)
# define PI 3.14159265
# define ceildiv(x,y) ((x + y - 1) / y)
//cout << fixed << setprecision(10)
//string(n,c)

void solve () {
	ll n,k;
	cin >> n >> k;
	vector<ll> ins;
	forn {
		ll temp;
		cin >> temp;
		ins.push_back(temp);
	}

	ll ans = INT_MAX;
	ll check = INT_MIN;
	forn {
		ans = min (ans, ins[i]+(k));
		check = max(check, ins[i]-k);
	}
	if (check > ans) {
		cout << -1 << endl;
		return;
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
	ll n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		solve();
	}
}


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

typedef long long ll;
typedef long double ld;
# define forn for(ll i = 0;i < n; i++)
# define pb push_back
# define sethas(set,x) (set.find(x) != set.end())
# define fill_matrix(matrix,size,value) (fill(*matrix, *matrix + size, value));
# define maxll(a,b) ((a > b) ? a : b)
# define minll(a,b) ((a > b) ? b : a)
# define triangle(n) (n*(n+1)/2)
# define PI 3.14159265
# define ceildiv(x,y) ((x + y - 1) / y)
//cout << fixed << setprecision(10)
//string(n,c)

void solve () {
	ll n,k;
	cin >> n >> k;
	vector<ll> ins;
	forn {
		ll temp;
		cin >> temp;
		ins.push_back(temp);
	}
	for (int i = 1; i < n; i++) {
		if (ins[i] - ins[i-1] > (2*k)) {
			cout << -1 << endl;
			return;
		}
	}
	ll ans = INT_MAX;
	forn {
		ans = min (ans, ins[i]+(k));
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
	ll n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		solve();
	}
}


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

typedef long long ll;
typedef long double ld;
# define forn for(ll i = 0;i < n; i++)
# define pb push_back
# define sethas(set,x) (set.find(x) != set.end())
# define fill_matrix(matrix,size,value) (fill(*matrix, *matrix + size, value));
# define maxll(a,b) ((a > b) ? a : b)
# define minll(a,b) ((a > b) ? b : a)
# define triangle(n) (n*(n+1)/2)
# define PI 3.14159265
# define ceildiv(x,y) ((x + y - 1) / y)
//cout << fixed << setprecision(10)
//string(n,c)

void solve () {
	ll n,k;
	cin >> n >> k;
	vector<ll> ins;
	forn {
		ll temp;
		cin >> temp;
		ins.push_back(temp);
	}
	sort(ins.begin(), ins.end());
	for (int i = 1; i < n; i++) {
		if (ins[i] - ins[i-1] > (2*k)) {
			cout << -1 << endl;
			return;
		}
	}
	ll ans = INT_MAX;
	forn {
		ans = min (ans, ins[i]+(k));
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
	ll n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		solve();
	}
}


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

typedef long long ll;
typedef long double ld;
# define forn for(ll i = 0;i < n; i++)
# define pb push_back
# define sethas(set,x) (set.find(x) != set.end())
# define fill_matrix(matrix,size,value) (fill(*matrix, *matrix + size, value));
# define maxll(a,b) ((a > b) ? a : b)
# define minll(a,b) ((a > b) ? b : a)
# define triangle(n) (n*(n+1)/2)
# define PI 3.14159265
# define ceildiv(x,y) ((x + y - 1) / y)
//cout << fixed << setprecision(10)
//string(n,c)

void solve () {
	ll n,k;
	cin >> n >> k;
	vector<ll> ins;
	forn {
		ll temp;
		cin >> temp;
		ins.push_back(temp);
	}
	sort(ins.begin(), ins.end());
	for (int i = 1; i < n; i++) {
		if (ins[i] - ins[i-1] >= (2*k)) {
			cout << -1 << endl;
			return;
		}
	}
	ll ans = INT_MAX;
	forn {
		ans = min (ans, ins[i]+(k));
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
	ll n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		solve();
	}
}


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

typedef long long ll;
typedef long double ld;
# define forn for(ll i = 0;i < n; i++)
# define pb push_back
# define sethas(set,x) (set.find(x) != set.end())
# define fill_matrix(matrix,size,value) (fill(*matrix, *matrix + size, value));
# define maxll(a,b) ((a > b) ? a : b)
# define minll(a,b) ((a > b) ? b : a)
# define triangle(n) (n*(n+1)/2)
# define PI 3.14159265
# define ceildiv(x,y) ((x + y - 1) / y)
//cout << fixed << setprecision(10)
//string(n,c)

void solve () {
	ll n,k;
	cin >> n >> k;
	vector<ll> ins;
	forn {
		ll temp;
		cin >> temp;
		ins.push_back(temp);
	}
	for (int i = 1; i < n; i++) {
		if (ins[i] - ins[i-1] > (2*k)) {
			cout << -1 << endl;
		}
	}
	ll ans = INT_MAX;
	forn {
		ans = min (ans, ins[i]+(k));
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(0);
	ll n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		solve();
	}
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    long long i,j,k,n,m;
    long long t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<long long>a(n);
        long long mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        i=k;
        if(mn+i>=mx){
            cout<<mn+i<<endl;
            continue;
        }
        i=k;
        long long x,y;
        long long m=INT_MIN;
        if(mn+k<mx){
            i=0;
            while(mx-i!=mn+k && i<=k){
                if(mx-i==mn+k){
                    m=mx-i;
                    break;
                }
                i++;
            }
        } else {
            m=mn+k;
        }
        for(i=mx;i>=mn;i--){
            if(mn+k>=i && mx-k<=i){
                m=i;
                break;
            }
        }
        if(m==INT_MIN){
            cout<<-1<<endl;
            continue;
        } else {
            cout<<m<<endl;
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    long long i,j,k,n,m;
    long long t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<long long>a(n);
        long long mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        if(mn+k<mx-k){
            cout<<-1<<endl;
        } else {
            cout<<mn+k<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    ll i,j,k,n,m;
    ll t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<ll>a(n);
        ll mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        i=k;
        m=INT_MIN;
        while(i>=0){
            if(mn+i==mx-i){
                m=i;
                break;
            } else {
                i--;
            }
        }
        if(mn+k>=mx){
            m=k;
        }
        if(m==INT_MIN){
            cout<<-1<<endl;
            continue;
        }
        cout<<mn+m<<endl;
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    ll i,j,k,n,m;
    ll t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<ll>a(n);
        ll mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        if(mn+k<mx-k){
            cout<<-1<<endl;
            continue;
        }
        i=k;
        m=INT_MIN;
        while(i>=0){
            if(mn+i==mx-i){
                m=i;
                break;
            } else {
                i--;
            }
        }
        if(mn+k>=mx){
            m=k;
        }
        if(m==INT_MIN){
            cout<<-1<<endl;
            continue;
        }
        cout<<mn+m<<endl;
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    ll i,j,k,n,m;
    ll t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<ll>a(n);
        ll mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        if(mn+k<mx-k){
            cout<<-1<<endl;
            continue;
        }
        i=k;
        while(i>=0){
            if(mn+i==mx-i){
                m=i;
                break;
            } else {
                i--;
            }
        }
        if(mn+k>=mx){
            m=k;
        }
        cout<<mn+m<<endl;
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stack>
#define ll long long
#define db double
#define str string
#define ch char
#define pb push_back
#define FOR(a,n) for(i=0;i<n;i++){	cin>>a[i];}
#define sortVector(a) sort(a.begin(),a.end())
#define reverseSortVector(a) sort(a.rbegin(),a.rend())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"

using namespace std;

ll choose(ll n , ll m){   // (n c k)
	ll ans=0;
	ll num=1;
	ll den=1;
	for(ll i=0;i<m;i++){
		num*=(n-i);
		num=num/(i+1);
	}
	return num;
}

ll isPrime(ll n)
	        {

	           if(n==0 || n==1)
	           return 0;
	            if (n%2 == 0)
	                return 2;

	            if(n%3==0)
	                return 3;

	            for (ll i=5; i*i<=n; i=i+6)
	            {
	                if (n%i == 0)
	                   return i;
	                if (n%(i+2) == 0)
	                   return i+2;
	            }

	            return n;
	        }

bool check(vector<ll>a, ll mxelement){
    ll i,j,k;
    for(i=0;i<a.size();i++){
        if(a[i]==mxelement){
            j=i;
            break;
        }
    }
    //cout << " j " << j << " " << mxelement << endl;
    for(i=j+1;i<a.size()-1;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    if(a[a.size()-1]>a[0]){
        return false;
    }
    for(i=0;i<j;i++){
        if(a[i]>a[i+1]){
            return false;
        }
    }
    return true;
}

ll digits(ll n){
    ll c=0;
    while(n!=0){
        c++;
        n/=10;
    }
    return c;
}

ll gcd(ll a, ll b){
    if(b==0){
        return a;
    } else {
        gcd(b,a%b);
    }
}

struct node{
    ll data;
    node* left;
    node* right;
};

node* getNewNode(ll data){
    node* newNode = new node();
    newNode->data=data;
    newNode->left=newNode->right=NULL;
    return newNode;
}

node* Insert(node* root, ll data){
    if(root==NULL){
        root->data=data;
        return root;
    } else if(root->data>=data){
        root->left=Insert(root->left,data);
    } else {
        root->right=Insert(root->right,data);
    }
    return root;
}

bool searchTree(node* root, ll data){
    if(data==root->data){
        return true;
    } else if(root==NULL){
        return false;
    } else if(root->data>data){
        searchTree(root->left,data);
    } else {
        searchTree(root->right,data);
    }
}

bool isDiv(ll n){
    ll sum=0;
    while(n>0){
        sum+=n%10;
        n/=10;
    }
    if(sum%4==0){
        return true;
    }
    return false;
}

int main(){
    long long i,j,k,n,m;
    long long t;
    cin>>t;
    while(t--){
        cin>>n>>k;
        vector<long long>a(n);
        long long mn=LONG_LONG_MAX,mx=LONG_LONG_MIN;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        for(i=0;i<n;i++){
            if(a[i]<mn){
                mn=a[i];
            }
            if(a[i]>mx){
                mx=a[i];
            }
        }
        i=k;
        if(mn+i>=mx){
            cout<<mn+i<<endl;
            continue;
        }
        i=k;
        long long x,y;
        long long m=INT_MIN;
        x=mn,y=mx;
        mn=mn-k,mx=mx+k;
        for(i=mx;i>=mn;i--){
            if(x+k>=i && y-k<=i){
                m=i;
                break;
            }
        }
        if(m==INT_MIN){
            cout<<-1<<endl;
            continue;
        } else {
            cout<<m<<endl;
            continue;
        }
    }
    return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z,ami=0;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b && v[i]>0)
        {
            ami++;
            w=v[i];
            break;
        }
    }
    ll l=b-z;
if(ami>0) {
        ll d=abs((w+l)-v[n-1]);
if(d<=b)
    cout<<w+l<<endl;
    else
        cout<<-1<<endl;
}
    else
        cout<<-1<<endl;
    v.clear();
}

return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z,ami=0;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b && v[i]>0)
        {
            ami++;
            w=v[i];
            break;
        }
    }
    ll l=b-z;
if(ami>0)
    cout<<w+l<<endl;
    else
        cout<<-1<<endl;
    v.clear();
}

return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b)
        {
            w=v[i];
            break;
        }
    }
    ll l=b-z;

    cout<<w+l<<endl;
    v.clear();
}

return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z,ami=0;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b && z!=0)
        {
            ami++;
            w=v[i];
            break;
        }
    }
    ll l=b-z;
if(ami>0)
    cout<<w+l<<endl;
    else
        cout<<-1<<endl;
    v.clear();
}

return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z,ami=0;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b)
        {
            ami++;
            w=v[i];
            break;
        }
    }
    ll l=b-z;
if(ami>0)
    cout<<w+l<<endl;
    else
        cout<<-1<<endl;
    v.clear();
}

return 0;
}


#include<bits/stdc++.h>

#define Input                   freopen("in.txt","r",stdin)
#define Output                  freopen("out.txt","w",stdout)
#define ll                      long long int
#define ull                     unsigned long long int
#define pii                     pair<int,int>
#define pll                     pair<ll,ll>
#define nl                      printf("\n")
#define sc                      scanf
#define scin(x)                 sc("%d",&(x))
#define scin2(x,y)              sc("%d %d",&(x),&(y))
#define scln(x)                 sc("%lld",&(x))
#define scln2(x,y)              sc("%lld %lld",&(x),&(y))
#define pf                      printf
#define all(a)                  (a.begin()),(a.end())
#define UNIQUE(X)               (X).erase(unique(all(X)),(X).end())
#define SORT_UNIQUE(c)          (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ms(a,b)                 memset(a,b,sizeof(a))
#define pb(a)                   push_back(a)
#define mp                      make_pair
#define db                      double
#define EPS                     10E-10
#define ff                      first
#define ss                      second
#define sqr(x)                  (x)*(x)
#define vi                      vector<int>
#define vl                      vector<ll>
#define vii                     vector<vector<int> >
#define vll                     vector<vector<ll> >
#define DBG                     pf("HI\n")
#define MOD                     1000000007
#define CIN                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define RUN_CASE(t,T)           for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)                 printf("Case %d: ",t)
#define CASEl(t)                printf("Case %d:\n",t)
#define intlimit                2147483690
#define longlimit               92233720368547758
#define infinity                (1<<28)
#define gcd(a,b)                __gcd(a,b)
#define lcm(a,b)                ((a)*(b))/gcd(a,b)
#define mxx                     123456789
#define PI                      2*acos(0.0)
#define rep(i,a,b)              for(__typeof(i) i=a;i<=b;i++)
#define rev(i,a,b)              for(__typeof(i) i=b;i>=a;i--)

//Moin Uddin Ruman , 10th batch CSE ,ID : 11808011 , Comilla University (2017-2018) :
//Email : moinruman11@gmail.com, Phone : 01879-238107 .
using namespace std;

int main()
{
ll t;
cin>>t;
while(t--)
{
    ll n,b,j,k,i;
    cin>>n>>b;
    vector<ll> v,v1;
    rep(i,1,n)
    {
        cin>>k;
        v.pb(k);

    }

    sort(v.begin(),v.end());
    ll y=v[0],w,z,ami=0;
    rev(i,1,n-1)
    {
         z=abs(v[i]-y);
        if(z<=b && v[i]>0)
        {
            ami++;
            w=v[i];
            break;
        }
    }
    ll l=b-z;
if(ami>0) {
        ll d=abs((w+l)-v[n-1]);
if(d<=b)
    cout<<w+l<<endl;
    else
        cout<<-1<<endl;
}
    else {
            if(n==1)
            {
                    cout<<v[0]+b<<endl;

            }
            else
        cout<<-1<<endl;
    }
    v.clear();
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  (cin >> args), ...);
}

int32_t main() {
  tc() {
    int n, k; read(n, k);
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) print(s + k);
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  ((scanf("%lld", &args)), ...);
}

int32_t main() {
  tc() {
    int n, k; read(n, k);
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) print(s + k);
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  ((scanf("%lld", &args)), ...);
}

int32_t main() {
  tc() {
    int n, k; cin >> n >> k;
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) cout << s + k << endl;
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  ((cin >> args), ...);
}

int32_t main() {
  tc() {
    int n, k; read(n, k);
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) print(s + k);
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  ((scanf("%lld", &args)), ...);
}

int32_t main() {
  tc() {
    int n, k; cin >> n >> k;
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) print(s + k);
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define logarr(a) for (auto i : a) cout << i << " "; cout << endl;
#define int long long
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define INF (int)1e9+7
#define deb(x) cout << "[" << #x << " : " << x << "] " << endl;
#define tc() int T; cin >> T; while(T--)
#define print(x) cout << x << endl

struct fastIO {
  fastIO() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(9);
  }

} fio;

template <typename... T>
void read(T&... args) {
  ((scanf("%lld", &args)), ...);
}

int32_t main() {
  tc() {
    int n, k; read(n, k);
    vector<int> v(n);
    int s = (int)1e9, l = 0;
    rep(i,0,n) {
      cin >> v[i];
      s = min(s, v[i]);
      l = max(l, v[i]);
    }
    if (s + k >= l - k) cout << s + k << endl;
    else print(-1);
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    int n, k;
    cin >> n >> k;
    int mr = 1 << 30, ml = -1;
    for (int j=0; j<n; j++) {
      int e; cin >> e;
      int l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
    }
    if (ml <= mr) cout << mr << endl;
    else cout << -1 << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    ll mr = 10e9, ml = -10e9;
    for (int j=0; j<n; j++) {
      ll e; cin >> e;
      ll l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr <= 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    int n, k;
    cin >> n >> k;
    int mr = 10e9, ml = -10e9;
    for (int j=0; j<n; j++) {
      ll e; cin >> e;
      ll l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr <= 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    unordered_map<ll, ll> a;
    for (int j=0; j<n; j++) {
      int e; cin >> e;
      a[e] += 1;
    }
    unordered_map<ll, ll> um;
    for(auto& e : a) {
      for (int l=e.first-k; l<=e.first+k; l++) {
        um[l] += e.second;
      }
    }
    ll m = -100000010;
    for (auto& e : um) {
      if(e.first > m && e.second == n){
        m = e.first;
      }
    }
    if (m != -100000010) cout << m << endl;
    else cout << -1 << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    int n, k;
    cin >> n >> k;
    int mr = 20e8, ml = -20e8;
    for (int j=0; j<n; j++) {
      int e; cin >> e;
      int l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr <= 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    ll mr = 10e9, ml = -10e9;
    for (ll j=0; j<n; j++) {
      ll e; cin >> e;
      ll l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr <= 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    ll mr = 100000010, ml = -100000010;
    for (int j=0; j<n; j++) {
      ll e; cin >> e;
      ll l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    int n, k;
    cin >> n >> k;
    int mr = 20e8+10, ml = -20e8+10;
    for (int j=0; j<n; j++) {
      int e; cin >> e;
      int l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr <= 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    unordered_map<ll, ll> um;
    for (int j=0; j<n; j++) {
      ll e; cin >> e;
      for (int l=e-k; l<=e+k; l++) {
        um[l] += 1;
      }
    }
    ll m = -100000010;
    for (auto& e : um) {
      if(e.first > m && e.second == n){
        m = e.first;
      }
    }
    if (m != -100000010) cout << m << endl;
    else cout << -1 << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

template<typename T> ostream& operator << (ostream& os, vector<T> v) {
  for (auto& i : v) os << i << " "; return os;
}
template<typename T> istream& operator >> (istream& is, vector<T>& v) {
  for (auto& i : v) is >> i; return is;
}
template<typename K, typename V> ostream& operator << (ostream& os, unordered_map<K, V> m) {
  for (auto& i : m) os << i.first << ":" << i.second << endl; return os;
}
template<typename T> inline bool chmin(T& x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template<typename T> inline bool chmax(T& x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  int q;
  cin >> q;
  for (int i=0; i<q; i++) {
    ll n, k;
    cin >> n >> k;
    ll mr = 10e17, ml = -10e17;
    for (int j=0; j<n; j++) {
      ll e; cin >> e;
      ll l = e-k, r = e+k;
      if (mr > r) mr = r;
      if (ml < l) ml = l;
      if (mr - ml < 0 || mr < 0) {
        mr = -1;
        break;
      }
    }
    cout << mr << endl;
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 100000000

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        bool check = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;
        for(int j = 0; j < n - 1; j++){
            if(arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;

                min = get_max(temp_min, min);
                max = get_min(temp_max, max);
            }
            else{
                printf("-1\n");
                check = false;
                break;
            }
        }
        if(check) printf("%d\n", max);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 200000000

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        bool check = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;
        for(int j = 0; j < n - 1; j++){
            if(arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;
                if(min <= max){
                    min = get_max(temp_min, min);
                    max = get_min(temp_max, max);
                }
                else{
                    check = false;
                    printf("-1\n");
                    break;
                }
            }
            else{
                printf("-1\n");
                check = false;
                break;
            }
        }
        if(check && max > 0) printf("%d\n", max);
        else printf("-1\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 200000000

int compare(const void *a, const void *b){
    int num1 = *(int *)a;
    int num2 = *(int *)b;

    if(num1 < num2) return -1;
    if(num1 > num2) return 1;
    return 0;
}

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE + 1];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        qsort(arr, n, sizeof(int), compare);

        int num = arr[0] + range;
        bool flag = true;

        for(int j = 1; j < n; j++){
            if(num < arr[j] - range){
                flag = false;
                break;
            }
        }

        if(flag) printf("%d\n", num);
        else printf("-1\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 100000000

int compare(const void *a, const void *b){
    int num1 = *(int *)a;
    int num2 = *(int *)b;

    if(num1 < num2) return -1;
    if(num1 > num2) return 1;
    return 0;
}

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        qsort(arr, n, sizeof(int), compare);

        bool check = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;
        for(int j = 0; j < n - 1; j++){
            if(arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;

                min = get_max(temp_min, min);
                max = get_min(temp_max, max);
            }
            else{
                printf("-1\n");
                check = false;
                break;
            }
        }
        if(check) printf("%d\n", max);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 200000000

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        bool flag = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;
        for(int j = 0; j < n - 1; j++){
            if(arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;
                if(min <= max){
                    min = get_max(temp_min, min);
                    max = get_min(temp_max, max);
                }
                else{
                    flag = false;
                    break;
                }
            }
            else{
                flag = false;
                break;
            }
        }
        if(flag && max > 0) printf("%d\n", max);
        else printf("-1\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 200000000

int compare(const void *a, const void *b){
    int num1 = *(int *)a;
    int num2 = *(int *)b;

    if(num1 < num2) return -1;
    if(num1 > num2) return 1;
    return 0;
}

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        qsort(arr, n, sizeof(int), compare);

        bool flag = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;

        for(int j = 0; j < n; j++){
            if(j < n - 1 && arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;
                min = get_max(temp_min, min);
                max = get_min(temp_max, max);
            }
            else{
                flag = false;
                break;
            }
        }
        if(flag && max > 0) printf("%d\n", max);
        else printf("-1\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 100
#define MAX_VAL 200000000

int compare(const void *a, const void *b){
    int num1 = *(int *)a;
    int num2 = *(int *)b;

    if(num1 < num2) return -1;
    if(num1 > num2) return 1;
    return 0;
}

int get_min(int num1, int num2){
    if(num1 >= num2) return num2;
    return num1;
}

int get_max(int num1, int num2){
    if(num1 >= num2) return num1;
    return num2;
}

int main(){
    int trial;
    scanf("%d", &trial);
    for(int i = 0; i < trial; i++){
        int n, range;
        int arr[SIZE];
        scanf("%d %d", &n, &range);
        for(int j = 0; j < n; j++)
            scanf("%d", &arr[j]);

        qsort(arr, n, sizeof(int), compare);

        bool flag = true;
        int max = MAX_VAL + 1, min = 0;
        int temp_max, temp_min;

        for(int j = 0; j < n; j++){
            if(j < n - 1 && arr[j] + range >= arr[j + 1] - range){
                temp_min = arr[j + 1] - range;
                temp_max = arr[j] + range;
                min = get_max(temp_min, min);
                max = get_min(temp_max, max);
            }
            if(j < n - 1 && arr[j] + range < arr[j + 1] - range){
                flag = false;
                break;
            }
        }
        if(flag && max > 0) printf("%d\n", max);
        else printf("-1\n");
    }
    return 0;
}