#include <bits/stdc++.h>
using namespace std;
auto SOLUTION_FUNCTION(void) -> void {
  int n, k;
  cin >> n >> k;
  vector<long long> v(n);
  for (auto &i : v) {
    cin >> i;
  };
  sort(v.begin(), v.end());
  int target = v.front() + k;
  bool ok = true;
  for (int i = 0; i < v.size() && ok; ++i) {
    if (abs(v[i] - target) <= k or abs(v[i] + target) <= k) {
      continue;
    }
    ok = false;
    break;
  }
  if (ok) {
    cout << target << '\n';
  } else {
    cout << "-1" << '\n';
  }
  return;
}
int main(void) {
  int testcase = 1;
  cin >> testcase;
  while (testcase--) {
    SOLUTION_FUNCTION();
  }
  return 0;
}
