#include <bits/stdc++.h>
int main() {
  int t, n, x, y, d, i;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d %d %d %d", &n, &x, &y, &d);
    int a1 = 0, a2 = 0, s = -1, s1 = -1, s2 = -1;
    if ((y - x) % d == 0) {
      if (y > x) {
        s = (y - x) / d;
      } else {
        s = (x - y) / d;
      }
    } else {
      if ((y - 1) % d == 0) {
        a1 = (x - 1) / d;
        if ((x - 1) % d != 0) {
          a1++;
        }
        s1 = a1 + (y - 1) / d;
      }
      if ((n - y) % d == 0) {
        a2 = (n - x) / d;
        if ((n - x) % d != 0) {
          a2++;
        }
        s2 = a2 + (n - y) / d;
      }
      if (s1 == -1 && s2 != -1) {
        s = s2;
      } else if (s1 != -1 && s2 == -1) {
        s = s1;
      } else if (s1 < s2) {
        s = s1;
      } else if (s2 < s1) {
        s = s2;
      }
    }
    printf("%d\n", s);
  }
}
