#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int abs(int a) { return a < 0 ? -a : a; }
int cal(int a, int b) { return a % b == 0 ? a / b : a / b + 1; }
int main() {
  int T;
  scanf("%d", &T);
  for (int cas = 1; cas <= T; cas++) {
    int n, x, y, d;
    scanf("%d%d%d%d", &n, &x, &y, &d);
    if (abs(x - y) % d == 0)
      printf("%d\n", abs(x - y) / d);
    else {
      int ans = inf;
      if ((y - 1) % d == 0) ans = min(ans, cal(x - 1, d) + cal(y - 1, d));
      if ((n - y) % d == 0) ans = min(ans, cal(n - x, d) + cal(n - y, d));
      if (ans == inf) ans = -1;
      printf("%d\n", ans);
    }
  }
  return 0;
}
