#include <bits/stdc++.h>
using namespace std;
map<int, bool> mapi;
vector<pair<int, int> > p;
long long mod = 1e9 + 7;
char sl = '\n';
int q = 0;
int n = 0, k = 0;
void solve() {
  cin >> n >> k;
  vector<int> prices(n);
  for (int i = (0); i < (n); ++i) cin >> prices[i];
  int lsu = *(max_element((prices).begin(), (prices).end()));
  int lin = *(min_element((prices).begin(), (prices).end()));
  cout << (((lsu - (lin + k)) > k) ? -1 : (lin + k)) << sl;
  return;
}
int main() {
  cin >> q;
  while (q) {
    solve();
    --q;
  }
  return 0;
}
