#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
int t;
void solve() {
  while (t--) {
    int n, d, x, y;
    cin >> n >> x >> y >> d;
    if ((y - x) % d == 0) {
      cout << abs(y - x) / d << endl;
      continue;
    }
    int res1 = -1, res2 = -1;
    if ((y - 1) % d == 0) {
      res1 = (abs(x - 1) / d);
      if ((x - 1) % d) res1++;
      res1 += (y - 1) / d;
    }
    if ((n - y) % d == 0) {
      res2 = (abs(n - x) / d);
      if ((n - x) % d) res2++;
      res2 += (n - y) / d;
    }
    if (res1 == -1 && res2 == -1) {
      cout << -1 << endl;
    } else {
      if (res1 == -1)
        cout << res2 << endl;
      else if (res2 == -1)
        cout << res1 << endl;
      else
        cout << min(res1, res2) << endl;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  while (cin >> t) {
    solve();
  }
  return 0;
}
