#include <bits/stdc++.h>
using namespace std;
;
const long long INF = 1e9 + 7;
const int N = 5e3 + 10;
void combine(int& l1, int& r1, int l2, int r2) {
  l1 = max(l1, l2);
  r1 = min(r1, r2);
}
int main() {
  int ncase;
  scanf("%d", &ncase);
  while (ncase--) {
    int n, k;
    scanf("%d%d", &n, &k);
    int l = 1, r = INF;
    while (n--) {
      int x;
      scanf("%d", &x);
      combine(l, r, x - k, x + k);
    }
    l = max(1, l);
    if (r < l)
      puts("-1");
    else
      printf("%d\n", r);
  }
  return 0;
}
