#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
long long sum(long long x) {
  long long r = 0;
  while (x > 0) {
    r += x % 10;
    x = x / 10;
  }
  return r;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long q;
  cin >> q;
  ;
  while (q--) {
    long long n;
    cin >> n;
    ;
    long long k;
    cin >> k;
    ;
    long long a[(n) + 1];
    for (long long i = (1); i <= ((n)); i++) {
      cin >> a[i];
    };
    long long x = -inf, y = inf;
    for (long long i = (1); i <= (n); i++) {
      x = max(x, a[i] - k);
      y = min(y, a[i] + k);
      if (x > y) {
        cout << -1 << '\n';
        goto meow;
      }
    }
    cout << y << '\n';
  meow:;
  }
}
