#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    int q, k;
    cin >> q >> k;
    int a[q + 1];
    for (int i = 0; i < q; ++i) {
      cin >> a[i];
    }
    sort(a, a + q);
    int b = a[0] + k;
    int prbena = 0;
    for (int i = 1; i < q; ++i) {
      if (a[i] < b && a[i] + k > b) {
        continue;
      } else if (a[i] > b) {
        if (a[i] - b > k) {
          prbena = 1;
          break;
        }
      }
    }
    if (prbena)
      cout << -1 << endl;
    else
      cout << b << endl;
  }
}
