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