#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int j = 0; j < q; ++j) {
    int n, k, mi = 1e9, ma = 0;
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      mi = min(mi, a);
      ma = max(ma, a);
    }
    if (abs(ma - (mi + k)) <= k) {
      cout << mi + k << "\n";
    } else {
      cout << -1 << "\n";
    }
  }
}
