#include <bits/stdc++.h>
using namespace std;
string findSum(string str1, string str2) {
  if (str1.length() > str2.length()) swap(str1, str2);
  string str = "";
  int n1 = str1.length(), n2 = str2.length();
  reverse(str1.begin(), str1.end());
  reverse(str2.begin(), str2.end());
  int carry = 0;
  for (int i = 0; i < n1; i++) {
    int sum = ((str1[i] - '0') + (str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  for (int i = n1; i < n2; i++) {
    int sum = ((str2[i] - '0') + carry);
    str.push_back(sum % 10 + '0');
    carry = sum / 10;
  }
  if (carry) str.push_back(carry + '0');
  reverse(str.begin(), str.end());
  return str;
}
int dgtsum(int n) {
  int sum = 0;
  while (n) {
    sum += (n % 10);
    n /= 10;
  }
  return sum;
}
long long int GCD(long long int a, long long int b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
string findMin(string s1, string s2) {
  int sz1 = s1.size(), sz2 = s2.size();
  if (sz1 == sz2) return min(s1, s2);
  if (sz1 < sz2) return s1;
  return s2;
}
int ara[100005];
void solve() {
  int n, k, a, mx, mn;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (!i) {
      mx = a, mn = a;
    }
    mx = max(mx, a);
    mn = min(mn, a);
  }
  if (mx <= (mn + 2 * k)) {
    a = mn + k;
    cout << a << endl;
  } else
    cout << -1 << endl;
}
int main() {
  int tst = 1;
  scanf("%d", &tst);
  while (tst--) solve();
  return 0;
}
