#include <bits/stdc++.h>
using namespace std;
int ar[2000000];
int ans[200000];
string asl[1000000];
string as[1000000];
int main() {
  long long n, i, j, a, b, c, d, e, f, g, l, h, m, maxx, minn, q;
  string s, t, o, r;
  char ch, ch1;
  long double p, y, x, u, z;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    h = 0;
    l = 1000000001;
    for (i = 0; i < n; i++) {
      cin >> a;
      h = max(h, a);
      l = min(l, a);
    }
    if (l + m >= h - m)
      cout << l + m << endl;
    else
      cout << -1 << endl;
  }
}
