#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      long long digi;
      cin >> digi;
      v.push_back(digi);
    }
    long long maxi = (*max_element(v.begin(), v.end()));
    long long mini = (*min_element(v.begin(), v.end()));
    long long diff = ceil(((float)maxi - mini) / 2);
    if ((maxi - mini) > 2 * k) {
      cout << "-1" << endl;
    } else {
      long long ans = mini + k;
      cout << ans << endl;
    }
  }
}
