#include <bits/stdc++.h>
const long long MOD = 998244353;
const int MAX = 1e9 + 7;
const int MIN = -1e9 + 7;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    int a[n + 5];
    for (int i = 0; i < n; i++) cin >> a[i];
    int mx = *max_element(a, a + n);
    int mn = *min_element(a, a + n);
    int ans = mn + k;
    if (mx - k <= ans && mn + k >= ans)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
