#include <bits/stdc++.h>
using namespace std;
int q, n, k;
int a[110];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + n + 1);
    int Min = a[1], Max = a[n];
    if (abs(Max - (Min + k)) > k)
      cout << -1;
    else
      cout << Min + k;
    cout << "\n";
  }
}
