#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int u, v, next, w;
} Edge;
Edge e[1];
int cnt, head[1];
inline void add(int u, int v, int w) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].w = w;
  e[cnt].next = head[u];
  head[u] = cnt++;
  e[cnt].u = v;
  e[cnt].v = u;
  e[cnt].w = w;
  e[cnt].next = head[v];
  head[v] = cnt++;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int q, n, k, a;
int main() {
  q = read();
  while (q--) {
    n = read(), k = read();
    int maxi = -1, mini = 0x7f7f7f7f;
    for (int i = 1; i <= n; i++)
      a = read(), maxi = max(maxi, a), mini = min(mini, a);
    int dis = maxi - mini;
    int res;
    if (dis <= k)
      res = mini + k;
    else {
      if (mini + 2 * k < maxi)
        res = -1;
      else
        res = mini + k;
    }
    cout << res << endl;
  }
}
