#include <bits/stdc++.h>
using namespace std;
int cl(int x, int y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, x, y, d;
    cin >> n >> x >> y >> d;
    if (x > y) {
      x = n - x + 1;
      y = n - y + 1;
    }
    if ((y - x) % d == 0)
      cout << (y - x) / d << "\n";
    else {
      int op1 = -1, op2 = -1;
      if ((y - 1) % d == 0) op1 = (y - 1) / d + cl(x - 1, d);
      if ((n - y) % d == 0) op2 = (n - y) / d + cl(n - x, d);
      if (op1 < 0) {
        if (op2 > 0)
          cout << op2 << "\n";
        else
          cout << "-1\n";
      } else {
        if (op2 < 0)
          cout << op1 << "\n";
        else
          cout << min(op1, op2) << "\n";
      }
    }
  }
}
