#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<long long> n(t), x(t), y(t), d(t);
  long long s, sl, sr, sum;
  for (int i = 0; i < t; i++) {
    cin >> n[i] >> x[i] >> y[i] >> d[i];
    s = abs(y[i] - x[i]);
    sl = (n[i] - x[i] + d[i] - 1) / d[i] + (n[i] - y[i] + d[i] - 1) / d[i];
    sr = (y[i] - 2 + d[i]) / d[i] + (x[i] - 2 + d[i]) / d[i];
    sum = min(sl, sr);
    if (s % d[i] == 0) {
      cout << s / d[i] << endl;
    } else if ((n[i] - y[i]) % d[i] == 0 && (y[i] - 1) % d[i] == 0) {
      cout << sum << endl;
    } else if ((n[i] - y[i]) % d[i] == 0) {
      cout << sl << endl;
    } else if ((y[i] - 1) % d[i] == 0) {
      cout << sr << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
