#include <bits/stdc++.h>
using namespace std;
int t;
int num[105];
int main() {
  scanf("%d", &t);
  while (t--) {
    int n, k;
    memset(num, 0, sizeof(num));
    scanf("%d%d", &n, &k);
    int i, j;
    for (i = 0; i < n; i++) scanf("%d", &num[i]);
    sort(num, num + n);
    if ((num[n - 1] - num[0]) > 2 * k) {
      cout << -1 << endl;
      continue;
    }
    cout << num[0] + k << endl;
  }
  return 0;
}
