#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(NULL);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n, k, ans;
    cin >> n >> k;
    vector<int> arr(n);
    for (auto &x : arr) cin >> x;
    long long mn = *min_element(arr.begin(), arr.end()),
              mx = *max_element(arr.begin(), arr.end());
    if (mn + k >= mx - k)
      cout << mn + k << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}
