#include <bits/stdc++.h>
using namespace std;
long long MAX = 1000005;
long long mod = 1e9 + 7;
int main() {
  long long T;
  cin >> T;
  while (T--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    if (abs(v[0] - v[n - 1]) > 2 * k) {
      cout << -1 << endl;
      continue;
    }
    cout << v[0] + k << endl;
  }
}
