#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, k, a, mn, mx, out;
  bool tf = true;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (i == 0) {
      mn = a;
      mx = a;
    }
    if (a > mx) mx = a;
    if (a < mn) mn = a;
    if ((mx - mn) > 2 * k) {
      out = -1;
    } else {
      out = mn + k;
    }
  }
  cout << out << endl;
}
int main(void) {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) solve();
}
