#include <bits/stdc++.h>
using namespace std;
bool digits(long long n) {
  long long sum = 0;
  while (n) {
    sum += n % 10;
    n /= 10;
  }
  return sum % 4 == 0;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q;
  cin >> q;
  while (q--) {
    long long n, k;
    cin >> n >> k;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    long long mx = INT_MIN;
    long long mn = INT_MAX;
    for (long long i = 0; i < n; i++) {
      mx = max(mx, a[i]);
      mn = min(mn, a[i]);
    }
    if (mx - mn > 2 * k)
      cout << -1 << "\n";
    else {
      cout << mn + k << "\n";
    }
  }
  return 0;
}
