#include <bits/stdc++.h>
using namespace std;
bool solve(vector<long long> a, long long val, long long mx) {
  for (auto now : a) {
    if (abs(now - val) > mx) return false;
  }
  return true;
}
signed main() {
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long n, k, mn = INT_MAX, mx = INT_MIN;
    cin >> n >> k;
    vector<long long> a(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] < mn) mn = a[i];
      if (a[i] > mx) mx = a[i];
    }
    if (mx - mn > 2 * k) {
      cout << -1 << "\n";
      continue;
    }
    long long left = mx + k + 1, right = (mx + mn) / 2;
    for (long long i = 0; i < 100; i++) {
      long long mid = (left + right) / 2;
      if (solve(a, mid, k))
        right = mid;
      else
        left = mid;
    }
    cout << right << "\n";
  }
  return 0;
}
