#include <bits/stdc++.h>
using namespace std;
long long int power(long long int n, long long int m) {
  if (m == 0) return 1;
  long long int t = power(n, m / 2);
  if (m % 2 == 0) return (t * t);
  return (((t * t)) * n);
}
long long int logtwo(long long int n) {
  if (n == 1) return 0;
  return logtwo(n / 2) + 1;
}
long long int gcd(long long int a, long long int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
string itb(long long int n) {
  string s = "";
  while (n > 0) {
    long long int d = n % 2;
    s = (char)(d + 48) + s;
    n = n / 2;
  }
  return s;
}
long long int bti(string s) {
  long long int ans = 0, n = s.length();
  for (int i = 0; i < n; i++) {
    ans += (int)(s[n - 1 - i] - 48) * power(2, i);
  }
  return ans;
}
long long int power_with_modulo(long long int a, long long int b,
                                long long int n) {
  long long int x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y) % n;
    }
    y = (y * y) % n;
    b /= 2;
  }
  return x % n;
}
int main() {
  long long int q, n, ans, k, i, x, j;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    long long int a[n], flag = 0;
    for (i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    if (a[0] + k >= a[n - 1])
      cout << a[0] + k << endl;
    else {
      if (a[n - 1] - k <= a[0] + k)
        cout << a[0] + k << endl;
      else
        cout << -1 << endl;
    }
  }
  return 0;
}
