#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    long int k;
    cin >> n >> k;
    long int a[n];
    for (int 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[0] + k >= a[n - 1] - k)
      cout << a[0] + k << endl;
    else
      cout << -1 << endl;
  }
}
