#include <bits/stdc++.h>
using namespace std;
const long long Linf = 0x7f7f7f7f7f7f7f7f;
const int Inf = 0x3f3f3f3f;
long long t, n, x, y, d;
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    long long ans = Linf;
    if (abs(x - y) % d == 0) ans = min(ans, abs(x - y) / d);
    if (abs(y - 1) % d == 0) {
      long long dis = (y - 1) / d + ceil(1.0 * (x - 1) / d);
      ans = min(ans, dis);
    }
    if (abs(n - y) % d == 0) {
      long long dis = (n - y) / d + ceil(1.0 * (n - x) / d);
      ans = min(ans, dis);
    }
    if (ans == Linf)
      cout << -1;
    else
      cout << ans;
    cout << endl;
  }
  return 0;
}
