#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    int mi = INT_MAX;
    int ma = INT_MIN;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      if (x > ma) ma = x;
      if (x < mi) mi = x;
    }
    if ((ma - mi) > 2 * k) {
      cout << "-1" << endl;
    } else {
      cout << mi + k << endl;
    }
  }
}
