#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.141592653589793238462643383279;
const long long MOD = 998244353;
const long long INF = 1e9;
const long double eps = 0.000000000000001;
void boot_up() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(15);
}
signed main() {
  boot_up();
  long long q;
  cin >> q;
  while (q--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> a(n);
    long long mini = 1e10, maxi = -1;
    map<long long, long long> ma;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      mini = min(mini, a[i]);
      maxi = max(maxi, a[i]);
      ma[a[i]]++;
    }
    if (maxi - mini > 2 * k) {
      cout << -1 << endl;
    } else {
      cout << mini + k << endl;
    }
  }
  return 0;
}
