#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, k, a;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    int mina = 1e9, maxa = 0;
    for (int i = 0; i < n; i++) {
      cin >> a;
      maxa = max(maxa, a);
      mina = min(mina, a);
    }
    if (maxa - mina > 2 * k)
      printf("-1\n");
    else
      cout << mina + k << endl;
  }
  return 0;
}
