#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int j = 0; j < n; j++) cin >> a[j];
    int b = *max_element(a.begin(), a.end());
    int l = *min_element(a.begin(), a.end());
    if (b - 2 * k > l)
      cout << -1 << endl;
    else
      cout << l + k << endl;
  }
}
