#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, d, x, y, t;
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    long long ans = 2e18;
    if (abs(x - y) % d == 0) {
      cout << abs(x - y) / d << endl;
    } else {
      if ((y - 1) % d == 0) {
        long long num = 0;
        if ((x - 1) % d) num++;
        num += (x - 1) / d;
        num += (y - 1) / d;
        ans = min(num, ans);
      }
      if ((n - y) % d == 0) {
        long long num = 0;
        if ((n - x) % d) num++;
        num += (n - x) / d;
        num += (n - y) / d;
        ans = min(num, ans);
      }
      if (ans == 2e18)
        cout << -1 << endl;
      else
        cout << ans << endl;
    }
  }
  return 0;
}
