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