#include <bits/stdc++.h>
using namespace std;
long long int mult(long long int a, long long int b,
                   long long int p = 1000000007) {
  return ((a % p) * (b % p)) % p;
}
long long int add(long long int a, long long int b,
                  long long int p = 1000000007) {
  return (a % p + b % p) % p;
}
long long int fpow(long long int n, long long int k,
                   long long int p = 1000000007) {
  long long int r = 1;
  while (k > 0) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
    k = k >> 1;
  }
  return r;
}
long long int inv(long long int a, long long int p = 1000000007) {
  return fpow(a, p - 2, p);
}
int main() {
  long long int q;
  cin >> q;
  while (q--) {
    long long int n, k;
    cin >> n >> k;
    vector<long long int> v(n);
    for (long long int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    long long int diff = abs(v[0] - v[n - 1]);
    long long int ans = 0;
    if (diff == 0) {
      ans = v[0] + k;
    } else if (abs(v[n - 1] - v[0]) <= k) {
      long long int t = abs(v[n - 1] - v[0]);
      ans = v[n - 1];
      ans += k - t;
    } else {
      long long int th = (v[0] + v[n - 1]) / 2;
      if (abs(th - v[0]) > k || abs(th - v[n - 1]) > k)
        ans = -1;
      else {
        ans = th;
        if ((k - abs(v[0] - th)) > 0) ans += (k - abs(v[0] - th));
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
