#include <bits/stdc++.h>
using namespace std;
int main() {
  int T, n, k, a[102];
  scanf("%d", &T);
  while (T--) {
    int max = 0;
    int min = 100000007;
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      if (a[i] > max) max = a[i];
      if (a[i] < min) min = a[i];
    }
    if (max - min > 2 * k) {
      printf("-1\n");
      continue;
    }
    printf("%d\n", min + k);
  }
}
