#include <bits/stdc++.h>
using namespace std;
int main() {
  long long q, n, k, len = 0;
  cin >> q;
  while (q--) {
    long long sum = 0;
    cin >> n >> k;
    long long A[n];
    for (int i = 0; i < n; i++) {
      cin >> A[i];
    }
    sort(A, A + n);
    if ((A[n - 1] - A[0]) > k + k)
      sum = -1;
    else
      sum = A[0] + k;
    cout << sum << endl;
  }
  return 0;
}
