#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int T, n, x, y, d;
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    scanf("%d%d%d%d", &n, &x, &y, &d);
    int ans = 0x7fffffff;
    if (abs(y - x) % d == 0) {
      ans = min(ans, abs(y - x) / d);
    } else if ((y - 1) % d == 0 || ((n - y) % d == 0)) {
      if ((y - 1) % d == 0)
        ans = min(ans, (y - 1) / d + (x - 1) / d + ((x - 1) % d == 0 ? 0 : 1));
      if ((n - y) % d == 0)
        ans = min(ans, (n - y) / d + (n - x) / d + ((n - x) % d == 0 ? 0 : 1));
    } else {
      ans = -1;
    }
    printf("%d\n", ans);
  }
  return 0;
}
