#include <bits/stdc++.h>
using namespace std;
int n, x, y, d, t, mn = 2e9;
int main() {
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    scanf("%d%d%d%d", &n, &x, &y, &d);
    if (abs(y - x) % d == 0)
      printf("%d\n", abs(y - x) / d);
    else if (((y - 1) % d == 0) || ((n - y) % d == 0)) {
      int k = 2e9, j = 2e9;
      if ((y - 1) % d == 0) {
        k = 0;
        k = x / d;
        if (x % d) k++;
        k += (y - 1) / d;
      }
      if ((n - y) % d == 0) {
        j = 0;
        int g = n - x;
        j = g / d;
        if (g % d) j++;
        j += (n - y) / d;
      }
      mn = min(j, k);
      printf("%d\n", mn);
    } else
      printf("-1\n");
  }
}
