#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, k, l;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cin >> n >> k;
    int a[n];
    for (int j = 0; j < n; j++) {
      cin >> a[j];
    }
    int mx = *max_element(a, a + n);
    int mn = *min_element(a, a + n);
    if (mx - mn > 2 * k)
      cout << "-1"
           << "\n";
    else
      cout << mn + k << "\n";
  }
  return 0;
}
