#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int n, x, y, d;
int solve() {
  int ret = INT_MAX;
  if ((x - y) % d == 0) ret = min(ret, abs(x - y) / d);
  if (y % d == 0) ret = min(ret, (x + d - 1) / d + y / d);
  if (y % d == (n - 1) % d)
    ret = min(ret, (n - 1 - x + d - 1) / d + (n - 1 - y) / d);
  return ret == INT_MAX ? -1 : ret;
}
void run() {
  scanf("%d%d%d%d", &n, &x, &y, &d), --x, --y;
  printf("%d\n", solve());
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) run();
  return 0;
}
