#include <bits/stdc++.h>
using namespace std;
int soul(int s[], int n, int k) {
  int mx = *max_element(s, s + n);
  int mn = *min_element(s, s + n);
  if (mx - mn > 2 * k)
    return -1;
  else
    return mn + k;
}
int main() {
  int q, n, k;
  int s[100];
  int Q[100];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n >> k;
    for (int ii = 0; ii < n; ii++) {
      cin >> s[ii];
    }
    Q[i] = soul(s, n, k);
  }
  for (int jj = 0; jj < q; jj++) {
    cout << Q[jj] << endl;
  }
}
