#include <bits/stdc++.h>
using namespace std;
int q, n, k;
int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n >> k;
    int arr[n];
    int Max = 0, Min = 100000000;
    for (int j = 0; j < n; j++) {
      cin >> arr[j];
      if (arr[j] > Max) Max = arr[j];
      if (arr[j] < Min) Min = arr[j];
    }
    if (Max - Min > 2 * k) {
      cout << -1 << "\n";
      continue;
    } else {
      cout << Min + k << "\n";
      continue;
    }
  }
}
