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