#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, c, n, k, i, b, l;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    b = 0;
    long long int a[n];
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    if (a[0] + k >= a[n - 1] - k)
      cout << k + a[0] << endl;
    else
      cout << "-1\n";
  }
  return 0;
}
