#include <bits/stdc++.h>
const int MAX_INT = INT32_MAX;
const long long MAX_LL = INT64_MAX;
const int N = 1e8;
bool isPrime[N];
using namespace std;
int npr(int n, int r) {
  int res = 1;
  for (int i = n - r + 1; i <= n; i++) {
    res *= i;
  }
  return res;
}
int ncr(int n, int r) {
  int res = 1;
  int rFact = 1;
  for (int i = n - r + 1; i <= n; i++) {
    res *= i;
    if (res % rFact == 0 && rFact <= r) {
      res /= rFact;
      rFact++;
    }
  }
  return res;
}
void sieve() {
  memset(isPrime, true, N);
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i <= N; i++) {
    if (isPrime[i]) {
      for (int j = i * i; j <= N; j += i) {
        isPrime[j] = false;
      }
    }
  }
}
void solve() {
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    vector<long long> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    long long mx = v[0] + k;
    if (abs(v.back() - mx) > k)
      cout << -1 << endl;
    else
      cout << mx << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
