#include <bits/stdc++.h>
using namespace std;
int main() {
  long long tc;
  cin >> tc;
  long long n, x, y, d;
  while (tc--) {
    cin >> n >> x >> y >> d;
    long long ans = 2ll * int(1e9);
    if (x == y) {
      ans = 0L;
    }
    if (abs(x - y) % d == 0ll) {
      ans = min(ans, abs(x - y) / d);
    }
    if (abs(y - 1) % d == 0ll) {
      long long val = (abs(y - 1) / d) + ceil((x - 1) / (double)d);
      ans = min(ans, val);
    }
    if (abs(y - n) % d == 0ll) {
      long long val = (abs(y - n) / d) + ceil(abs(x - n) / (double)d);
      ans = min(ans, val);
    }
    if (ans == 2ll * int(1e9)) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}
