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