#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)2e5 + 5;
long long T, n, k, t, a[MAXN], b[MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T-- && cin >> n >> k) {
    for (long long i = 1; i <= n && cin >> t; ++i) a[i] = t - k, b[i] = t + k;
    long long l = a[1], h = b[1];
    for (long long i = 2; i <= n; ++i) l = max(l, a[i]), h = min(h, b[i]);
    if (l <= h)
      cout << h << endl;
    else
      cout << -1 << endl;
  }
}
