#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 1e9;
int main() {
  int t, n, x, y, d, sum;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d%d%d", &n, &x, &y, &d);
    sum = inf;
    if (abs(x - y) % d == 0) sum = min(sum, abs(x - y) / d);
    if (abs(y - 1) % d == 0)
      sum = min(sum,
                abs(y - 1) / d + abs(x - 1) / d + (int)(abs(x - 1) % d != 0));
    if (abs(y - n) % d == 0)
      sum = min(sum,
                abs(y - n) / d + abs(x - n) / d + (int)(abs(x - n) % d != 0));
    if (sum == inf) sum = -1;
    printf("%d\n", sum);
  }
  return 0;
}
