#include <bits/stdc++.h>
const int N = 500000;
const int mod = 998244353;
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
bool cmp(int a, int b) { return a > b; }
long long n, t, m;
int a[300007];
vector<int> v;
int main() {
  io();
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    if ((a[n] - a[1]) <= (2 * m))
      cout << a[1] + m << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
