#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 10;
int a[N];
int n, k, q;
int main() {
  ios_base::sync_with_stdio(0);
  ;
  cin >> q;
  while (q--) {
    cin >> n >> k;
    int fg = 1;
    for (int i = 0; i < n; i++) cin >> a[i];
    int opt = *min_element(a, a + n) + k;
    for (int i = 0; i < n; i++) {
      if (abs(opt - a[i]) > k) {
        fg = 0;
        cout << -1 << endl;
        break;
      }
    }
    if (fg) cout << opt << endl;
  }
}
