#include <bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
bool sortcol(const vector<int>& v1, const vector<int>& v2) {
  return v1[1] < v2[1];
}
void fastio() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
bool compare(const pair<long long int, long long int>& a,
             const pair<long long int, long long int>& b) {
  if (a.first > b.first) return true;
  if (a.first == b.first) return a.second < b.second;
  return false;
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
vector<int> vin(int n) {
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    v.push_back(t);
  }
  return v;
}
vector<int> ch;
void solve() {
  long long int n, k;
  cin >> n >> k;
  long long int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  long long int l = a[0] - k, r = a[0] + k;
  for (int i = 0; i < n; i++) {
    long long int x = a[i] - k;
    long long int y = a[i] + k;
    l = max(l, x);
    r = min(r, y);
  }
  if (l > r) {
    cout << -1 << "\n";
  } else
    cout << r << "\n";
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) solve();
}
