#include <bits/stdc++.h>
int main() {
  int n;
  int N;
  std::cin >> N;
  while (N--) {
    int k;
    std::cin >> n >> k;
    int max_ = -1, min_ = 100000000 + 10;
    int a;
    for (int i = 1; i <= n; ++i) {
      std::cin >> a;
      max_ = std::max(max_, a);
      min_ = std::min(min_, a);
    }
    if (max_ - k <= min_ + k)
      std::cout << min_ + k << std::endl;
    else
      std::cout << -1 << std::endl;
  }
  return 0;
}
