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