#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, i, j = 0, k, c = INT_MAX;
    cin >> n >> k;
    vector<long long> v;
    for (i = 0; i < n; i++) {
      long long a;
      cin >> a;
      v.push_back(a);
      c = min(c, a + k);
    }
    for (i = 0; i < n; i++) {
      if (abs(v[i] - c) > k) j = 1;
    }
    if (j == 1)
      cout << -1 << endl;
    else
      cout << c << endl;
  }
}
