#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, k, a[109], mina = 100000000 + 9, maxa = 0;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] < mina) mina = a[i];
      if (a[i] > maxa) maxa = a[i];
    }
    int avg = (mina + maxa) / 2, ans;
    if (abs(avg - mina) > k || abs(avg - maxa) > k)
      ans = -1;
    else {
      int low = mina, high = mina + k;
      while (low < high) {
        int mid = (low + high + 1) / 2;
        if (abs(mid - mina) <= k || abs(mid - maxa) <= k) {
          low = mid;
        } else
          high = mid;
      }
      ans = (low + high) / 2;
    }
    cout << ans << endl;
  }
  return 0;
}
