C:没有做出来,哎呦难受
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc u << 1
#define rc u << 1 | 1
void solve();
const int N = 1e6 + 10;
signed main() {
IOS;
ll t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
//ai ai+k-1 是亮的,没有搞清楚这个玩意。哎
void solve() {
ll n, k; cin >> n >> k;
vector<ll> a(n);
vector<ll> b(n);
ll mod = 2 * k;
for(int i = 0; i < n; ++ i) cin >> a[i];
sort(all(a));
for(int i = 0; i < n; ++ i)
{
a[i] += (a[n-1] + k -1 - a[i])/(mod)*mod;
}
sort(all(a));
if(a[n-1] - a[0] >= k) cout << -1 << endl;
else cout << a[n-1] << endl;
}