#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, i, n, k;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld", &n, &k);
    long long int arr[n], flag = 1, max, min;
    for (i = 0; i < n; i++) cin >> arr[i];
    sort(arr, arr + n, greater<long long int>());
    min = arr[n - 1];
    max = arr[0];
    if (min + k >= max - k)
      printf("%lld\n", (min + k));
    else
      printf("-1\n");
  }
  return 0;
}
