#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, y, d, t;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld %lld %lld %lld", &n, &x, &y, &d);
    if (abs(y - x) % d != 0 && abs(y - 1) % d != 0 && abs(n - y) % d != 0)
      printf("-1\n");
    else {
      if (abs(y - x) % d == 0)
        printf("%lld\n", abs(y - x) / d);
      else {
        if (abs(y - 1) % d == 0 && abs(n - y) % d == 0) {
          printf("%lld\n",
                 min((x - 1) / d + ((x - 1) % d == 0 ? 0 : 1) + (y - 1) / d,
                     (n - x) / d + ((n - x) % d == 0 ? 0 : 1) + (n - y) / d));
        } else if (abs(n - y) % d == 0)
          printf("%lld\n",
                 (n - x) / d + ((n - x) % d == 0 ? 0 : 1) + (n - y) / d);
        else {
          printf("%lld\n",
                 (x - 1) / d + ((x - 1) % d == 0 ? 0 : 1) + (y - 1) / d);
        }
      }
    }
  }
  return 0;
}
