#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, x, y, d, i, m, mint, t;
  cin >> m;
  for (i = 1; i <= m; i++) {
    mint = 2000900003;
    t = 0;
    cin >> n >> x >> y >> d;
    if (abs(x - y) % d == 0 && abs(x - y) / d < mint) {
      mint = abs(x - y) / d;
      t = 1;
    }
    if ((y - 1) % d == 0 &&
        (y - 1) / d + abs(x - 1) / d + min(abs(x - 1) % d, 1) < mint) {
      mint = (y - 1) / d + abs(x - 1) / d + min(abs(x - 1) % d, 1);
      t = 1;
    }
    if ((n - y) % d == 0 &&
        (n - y) / d + abs(n - x) / d + min(abs(n - x) % d, 1) < mint) {
      mint = (n - y) / d + abs(n - x) / d + min(abs(n - x) % d, 1);
      t = 1;
    }
    if (t == 1)
      cout << mint << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
