#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,k,x,mx,mn; cin >> t;
while(t--){
cin >> n >> k;
map<int,int> f;
for(int i=0;i<n;i++){
cin >> x;
f[x]++;
}
auto it1 = f.begin();
auto it2 = f.rbegin();
mn = it1->first, mx = it2->first;
mn + k >= mx - k ? cout << mn+k << endl : cout << "-1\n";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCl7CglpbnQgdCxuLGsseCxteCxtbjsJY2luID4+IHQ7Cgl3aGlsZSh0LS0pewoJCWNpbiA+PiBuID4+IGs7CgkJbWFwPGludCxpbnQ+IGY7CgkJZm9yKGludCBpPTA7aTxuO2krKyl7CgkJCWNpbiA+PiB4OwoJCQlmW3hdKys7CgkJfQoJCWF1dG8gaXQxID0gZi5iZWdpbigpOwoJCWF1dG8gaXQyID0gZi5yYmVnaW4oKTsKCQltbiA9IGl0MS0+Zmlyc3QsIG14ID0gaXQyLT5maXJzdDsKCQltbiArIGsgPj0gbXggLSBrID8gY291dCA8PCBtbitrIDw8IGVuZGwgOiBjb3V0IDw8ICItMVxuIjsKCX0KCXJldHVybiAwOwp9