#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, inf = 2e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, x, y, d;
    cin >> n >> x >> y >> d;
    if (x == y)
      cout << "0\n";
    else if (abs(y - x) % d == 0)
      cout << abs(y - x) / d << "\n";
    else {
      int a = (x - 1) / d;
      if ((x - 1) % d != 0) a++;
      int t = n - x;
      int b = t / d;
      if (t % d != 0) b++;
      if ((y - 1) % d == 0)
        a = a + (y - 1) / d;
      else
        a = inf;
      if ((n - y) % d == 0)
        b = b + (n - y) / d;
      else
        b = inf;
      a = min(a, b);
      if (a == inf) a = -1;
      cout << a << "\n";
    }
  }
  return 0;
}
