#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const long long inf = 0x3f3f3f3f;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, x, y, d;
    scanf("%lld%lld", &n, &x);
    scanf("%lld%lld", &y, &d);
    long long ans = inf;
    long long dt = abs(x - y);
    if ((dt % d) == 0) {
      long long now = dt / d;
      if (now < ans) ans = now;
    }
    dt = abs(y - 1);
    if ((dt % d) == 0) {
      long long now = (x - 1 + d - 1) / d;
      now += dt / d;
      if (ans > now) ans = now;
    }
    dt = abs(n - y);
    if ((dt % d) == 0) {
      long long now = (n - x + d - 1) / d;
      now += dt / d;
      if (ans > now) ans = now;
    }
    if (ans == inf) ans = -1;
    printf("%lld\n", ans);
  }
  return 0;
}
