#include <bits/stdc++.h>
using namespace std;
int main() {
  long long test = 0, x = 0, y = 0, d = 0, n = 0;
  while (cin >> test) {
    while (test--) {
      cin >> n >> x >> y >> d;
      long long ans1 = 0, ans2 = 0, ans3 = 0;
      ans1 = ans2 = ans3 = 0x3f3f3f3f3f;
      if (abs(x - y) % d == 0) {
        ans1 = abs(x - y) / d;
      }
      if ((y - 1) % d == 0) {
        ans2 = (x + d - 2) / d + (y - 1) / d;
      }
      if ((n - y) % d == 0) {
        ans3 = (n - x + d - 1) / d + (n - y) / d;
      }
      long long ans = min(ans1, ans2);
      ans = min(ans, ans3);
      if (ans == 0x3f3f3f3f3f)
        printf("-1\n");
      else
        printf("%lld\n", ans);
    }
  }
}
