#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    int mn = INF, mx = -1;
    for (int i = 1; i <= n; ++i) {
      int x;
      scanf("%d", &x);
      mn = min(mn, x);
      mx = max(mx, x);
    }
    int m = (mx + mn) >> 1;
    printf("%d\n", max(m - mn, mx - m) <= k ? mn + k : -1);
  }
  return 0;
}
