#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, k;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    long long int minimum = pow(10, 9), x, maximum = -1;
    while (n--) {
      cin >> x;
      minimum = min(x, minimum);
      maximum = max(x, maximum);
    }
    if (k + minimum >= maximum - k)
      cout << k + minimum << endl;
    else
      cout << -1 << endl;
  }
}
