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