#include <bits/stdc++.h>
using namespace std;
int findMax(int price[], int diff, int l) {
  int minp = price[0];
  int maxp = price[0];
  for (int i = 0; i < l; i++) {
    minp = min(minp, price[i]);
  }
  for (int i = 0; i < l; i++) {
    maxp = max(maxp, price[i]);
  }
  int mintest = minp + diff;
  int maxtest = maxp - diff;
  if (mintest < maxtest) {
    return -1;
  }
  return minp + diff;
}
int main() {
  int cases;
  cin >> cases;
  int res[cases];
  for (int i = 0; i < cases; i++) {
    int goods, diff;
    cin >> goods >> diff;
    int price[goods];
    for (int o = 0; o < goods; o++) {
      cin >> price[o];
    }
    res[i] = findMax(price, diff, goods);
  }
  for (int i = 0; i < cases; i++) {
    cout << res[i] << endl;
  }
}
