#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int c = 0; c < t; c++) {
    int n, k;
    cin >> n >> k;
    int l, r, a;
    cin >> a;
    l = max(a - k, 0);
    r = a + k;
    for (int i = 1; i < n; i++) {
      cin >> a;
      l = max(l, a - k);
      r = min(r, a + k);
    }
    if (l > r)
      cout << "-1" << endl;
    else
      cout << r << endl;
  }
}
