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