#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, k;
  cin >> q;
  vector<int> vi;
  int maxx, minn, a;
  for (int i = 0; i < q; i++) {
    cin >> n >> k;
    vi.clear();
    for (int i = 0; i < n; i++) {
      cin >> a;
      vi.push_back(a);
    }
    minn = *min_element(vi.begin(), vi.end());
    maxx = *max_element(vi.begin(), vi.end());
    if (maxx - minn > 2 * k) {
      cout << -1 << endl;
      continue;
    }
    cout << minn + k << endl;
  }
  return 0;
}
