#include <bits/stdc++.h>
using namespace std;
int an[1000];
int main() {
  int q;
  cin >> q;
  for (int iq = 0; iq < q; ++iq) {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      cin >> an[i];
    }
    sort(an, an + n);
    if (an[n - 1] - an[0] <= 2 * k) {
      cout << an[0] + k << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
