#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using VI = vector<int>;
int n, d, t, x, y;
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    if (x % d == y % d)
      cout << abs(x - y) / d << endl;
    else {
      int ans = -1;
      if (y % d == 1 % d) ans = (x - 1 + d - 1) / d + (y - 1 + d - 1) / d;
      if (y % d == n % d) {
        int tmp = (n - x + d - 1) / d + (n - y + d - 1) / d;
        if (ans == -1 || ans > tmp) ans = tmp;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
