#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;
    int ans;
    if (abs(y - x) % d == 0)
      ans = abs(y - x) / d;
    else {
      if ((y - 1) % d == 0 || (n - y) % d == 0) {
        int a = INT_MAX, b = INT_MAX;
        if ((y - 1) % d == 0) a = int(ceil(1.0 * (x - 1) / d)) + (y - 1) / d;
        if ((n - y) % d == 0) b = int(ceil(1.0 * (n - x) / d)) + (n - y) / d;
        ans = min(a, b);
      } else
        ans = -1;
    }
    cout << ans << endl;
  }
}
