#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, i, j, n, k, v;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> n >> k;
    vector<long long int> x;
    for (j = 0; j < n; j++) {
      cin >> v;
      x.push_back(v);
    }
    sort(x.begin(), x.end());
    if ((x[0] + k) >= (x[n - 1] - k)) {
      cout << x[0] + k << endl;
      ;
    } else {
      cout << "-1" << endl;
    }
  }
}
