#include <bits/stdc++.h>
using namespace std;
const int nmax = 100010;
void Solve() {
  int n, x, y, d;
  cin >> n >> x >> y >> d;
  if (abs(y - x) % d == 0)
    cout << abs(y - x) / d << '\n';
  else {
    int t1 = (x - 1) / d;
    if ((x - 1) % d != 0) t1++;
    if (abs(y - 1) % d == 0)
      t1 = t1 + abs(y - 1) / d;
    else
      t1 = 1000000007;
    int t2 = (n - x) / d;
    if ((n - x) % d != 0) t2++;
    if (abs(n - y) % d == 0)
      t2 = t2 + abs(n - y) / d;
    else
      t2 = 1000000007;
    if (min(t1, t2) == 1000000007)
      cout << -1 << '\n';
    else
      cout << min(t1, t2) << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) Solve();
  return 0;
}
