#include <bits/stdc++.h>
long long int min(long long res, long long temp) {
  if (res > temp)
    return temp;
  else
    return res;
}
int main() {
  int t, flag = 0;
  long long int n, x, y, d, res, temp, a;
  scanf("%d", &t);
  while (t--) {
    scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
    a = abs(x - y);
    flag = 0;
    res = 1000000095;
    temp = res;
    if (a % d == 0) {
      flag = 1;
      res = a / d;
      res = min(res, temp);
      temp = res;
    }
    if ((y - 1) % d == 0) {
      flag = 2;
      if ((x - 1) % d == 0) {
        res = ((x - 1) / d) + ((y - 1) / d);
        res = min(res, temp);
        temp = res;
      } else {
        res = 1 + ((x - 1) / d) + ((y - 1) / d);
        res = min(res, temp);
        temp = res;
      }
    }
    if ((n - y) % d == 0) {
      flag = 3;
      if ((n - x) % d == 0) {
        res = ((n - x) / d) + ((n - y) / d);
        res = min(res, temp);
        temp = res;
      } else {
        res = 1 + ((n - x) / d) + ((n - y) / d);
        res = min(res, temp);
        temp = res;
      }
    }
    if (flag == 0) {
      res = -1;
    }
    printf("%lld\n", res);
  }
}
