#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, k, ans = -1;
    cin >> n >> k;
    long long int a[n], ma = -1;
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      ma = max(ma, a[i]);
    }
    long long int l = 1, r = ma + k;
    while (l <= r) {
      long long int mid = l + (r - l) / 2, ref = 0;
      for (long long int i = 0; i < n; i++) {
        if (abs(mid - a[i]) > k && mid > a[i]) {
          ref = 1;
          break;
        } else if (abs(mid - a[i]) > k && mid < a[i]) {
          ref = 2;
          break;
        }
      }
      if (ref == 0) {
        ans = mid;
        l = mid + 1;
      } else if (ref == 2) {
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
