#include <bits/stdc++.h>
using namespace std;
void input(int &n, int &k, vector<int> &price) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    price.push_back(temp);
  }
}
int main() {
  int q;
  cin >> q;
  int n, k;
  for (int i = 1; i <= q; i++) {
    vector<int> price;
    input(n, k, price);
    sort(price.begin(), price.end());
    if ((price[n - 1] - k) > (price[0] + k))
      cout << -1 << endl;
    else
      cout << price[0] + k << endl;
  }
  return 0;
}
