#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int minimum = 100000006;
    int maximum = 0;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      minimum = min(minimum, a);
      maximum = max(maximum, a);
    }
    if ((minimum + k) < (maximum - k)) {
      cout << -1 << endl;
    } else {
      cout << minimum + k << endl;
    }
  }
}
