#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long n, long long lelo) {
  if (n == 0) return 1;
  if (n == 1) return a;
  if (n == 2) return (a * a) % lelo;
  if (n % 2)
    return (a * power(a, n - 1, lelo)) % lelo;
  else
    return power(power(a, n / 2, lelo), 2, lelo) % lelo;
}
void swap(long long &a, long long &b) {
  long long tm = a;
  a = b;
  b = tm;
}
const long long N = 1e+5;
bool comp(const pair<long long, long long> &p1,
          const pair<long long, long long> &p2) {
  if (p1.first != p2.first)
    return p1.first > p2.first;
  else
    return p1.second < p2.second;
}
long long n, k;
vector<long long> v;
bool chk(long long x) {
  for (long long(i) = (0); (i) < (n); (i)++)
    if (abs(v[i] - x) > k) return 0;
  return 1;
}
void solve() {
  cin >> n >> k;
  v.resize(n);
  for (long long(i) = (0); (i) < (n); (i)++) cin >> v[i];
  sort((v).begin(), (v).end());
  long long t1 = v[0] + k, t2 = v[n - 1] - k;
  if (v[n - 1] - v[0] <= k) {
    cout << v[0] + k;
    return;
  }
  if (t2 > t1) {
    cout << -1;
    return;
  }
  cout << t1;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long double tt = 1.0 * clock();
  long long t = 1;
  cin >> t;
  for (long long(i) = (1); (i) < (t + 1); (i)++) {
    solve();
    if (i < t) cout << "\n";
  }
  tt = 1.000000000 * (clock() - tt) / CLOCKS_PER_SEC;
  return 0;
}
