#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    int Min = 999999999;
    int Max = -1;
    for (int i = 1; i <= n; i++) {
      int tem;
      cin >> tem;
      Min = min(Min, tem);
      Max = max(Max, tem);
    }
    if (abs((Min + k) - Max) <= k) {
      cout << Min + k << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}
