#include <bits/stdc++.h>
using namespace std;
long long const N = 1e5 + 5;
long long const inf = 1e18 + 5;
long long const MOD = 1e9 + 7;
long long t, n, m, x, y, z, w, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> n >> k;
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    long long l = a[0] - k;
    long long h = a[0] + k;
    for (long long i = 1; i < n; i++) {
      l = max(l, a[i] - k);
      h = min(h, a[i] + k);
    }
    if (l > h)
      cout << "-1\n";
    else
      cout << h << "\n";
  }
  return 0;
}
