#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int j = 0; j < n; ++j) {
      cin >> a[j];
    }
    int mn = *min_element(a.begin(), a.end());
    int mx = *max_element(a.begin(), a.end());
    if (mx - mn > 2 * k)
      cout << -1 << endl;
    else
      cout << mn + k << endl;
  }
  return 0;
}
