#include <bits/stdc++.h>
using namespace std;
const long long mod = 4294967296;
int n, m, q, k;
int a[200];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> q;
  int h = 0, l = 0;
  while (q--) {
    cin >> n >> k;
    int f = 1;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      int hh = a[i] + k, ll = a[i] - k;
      if (ll < 0) ll = 0;
      if (i == 0) {
        h = hh;
        l = ll;
      }
      if (ll > h || hh < l) f = 0;
      if (hh < h) h = hh;
      if (ll > l) l = ll;
    }
    if (f)
      cout << h << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
