#include <bits/stdc++.h>
using namespace std;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  init();
  long long int q;
  cin >> q;
  cout << "\n";
  while (q--) {
    long long int n, k;
    cin >> n;
    cout << " ";
    cin >> k;
    cout << "\n";
    long long int i, min, max;
    long long int a[105];
    min = 0;
    max = 0;
    cin >> a[0];
    cout << " ";
    min = a[0];
    max = a[0];
    for (i = 1; i < n; i++) {
      cin >> a[i];
      if (a[i] > max) max = a[i];
      if (a[i] < min) min = a[i];
    }
    cout << "\n";
    if ((min + k) >= (max - k) && (min + k) <= (max + k))
      cout << (min + k);
    else
      cout << -1;
    cout << "\n";
  }
}
