#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, k, a[105];
  scanf("%d", &q);
  while (q--) {
    memset(a, 0, sizeof(a));
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    sort(a, a + n);
    if (a[n - 1] - a[0] > 2 * k)
      printf("-1\n");
    else
      printf("%d\n", a[0] + k);
  }
  return 0;
}
