#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    int v[n];
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    sort(v, v + n);
    long long diff = v[n - 1] - v[0];
    if (diff > 2 * k) {
      cout << -1 << endl;
    } else {
      for (int j = 0; j < n; j++) {
        v[j] += k;
      }
      sort(v, v + n);
      cout << v[0] << endl;
    }
  }
  return 0;
}
