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