#include <bits/stdc++.h>
using namespace std;
bool check(int x, int n, int a[], int k) {
  bool ans = true;
  for (int i = 0; i < n; i++) {
    if (abs(x - a[i]) > k) {
      ans = false;
      break;
    }
  }
  return ans;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, k, mx, mn;
    cin >> n >> k;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    a[0] += k;
    if (abs(a[n - 1] - a[0]) <= k) {
      cout << a[0] << endl;
    } else
      cout << "-1\n";
  }
}
