#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const ld pi = acos(0) * 2;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
template <typename CT, typename T>
inline void reset_container(CT &c, int sz, T v) {
  c.resize(sz);
  for (auto &x : c) x = v;
}
int n, k;
int a[101];
void solve() {
  read(n);
  read(k);
  for (int i = (1); i <= (n); i++) read(a[i]);
  sort(a + 1, a + n + 1);
  if (a[n] - k > a[1] + k)
    puts("-1");
  else
    writeln(a[1] + k);
}
int main() {
  int q;
  read(q);
  while (q--) solve();
}
