#include <bits/stdc++.h>
const long long int MOD = 1e9 + 7;
const long long int INF = 1011111111;
const long long int LLINF = 1000111000111000111LL;
const long double EPS = 1e-10;
const long double PI = 3.14159265358979323;
using namespace std;
long long int power(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return power((x * x), n / 2);
  else
    return x * power((x * x), (n - 1) / 2);
}
vector<long long int> seve;
void seveprime() {
  int prime[1000000] = {0};
  for (int i = 2; i * i < 1000000; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j <= 1000000; j += i) prime[j] = 1;
    }
  }
  for (int i = 2; i <= 1000000; i++) {
    if (prime[i] == 0) seve.push_back(i);
  }
}
bool compare(pair<long long int, long long int> a,
             pair<long long int, long long int> b) {
  return a.second < b.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t, n, m, i, j, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    vector<pair<long long int, long long int> > v(n);
    for (i = 0; i < n; i++) {
      cin >> j;
      long long int l, r;
      r = j + k;
      l = max(1LL, j - k);
      v[i] = {l, r};
    }
    sort(v.begin(), v.end(), compare);
    long long int ans = v[0].second;
    int f = 0;
    for (i = 0; i < n; i++) {
      if (ans >= v[i].first && ans <= v[i].second)
        continue;
      else {
        f = 1;
        cout << -1 << '\n';
        break;
      }
    }
    if (f == 0) cout << ans << '\n';
  }
}
