#include <bits/stdc++.h>
const int maxn = 10000;
using namespace std;
long long n, d;
long long qiu1(long long x, long long y) {
  long long ans = -1;
  if ((y - 1) % d == 0) {
    ans = (y - 1) / d;
    ans += (x - 1) / d;
    if ((x - 1) % d != 0) ans++;
  }
  return ans;
}
long long qiu2(long long x, long long y) {
  long long ans = -1;
  if ((n - y) % d == 0) {
    ans = (n - y) / d;
    ans += (n - x) / d;
    if ((n - x) % d != 0) ans++;
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long x, y;
    cin >> n >> x >> y >> d;
    long long t = y - x;
    if (y < x) {
      t = x - y;
    }
    if (t % d == 0) {
      cout << t / d << endl;
      continue;
    }
    long long ans1 = -1, ans2 = -1;
    ans1 = qiu1(x, y);
    ans2 = qiu2(x, y);
    if (ans1 == -1 && ans2 == -1) {
      printf("-1");
    } else {
      if (ans1 == -1) {
        cout << ans2;
      } else {
        if (ans2 == -1)
          cout << ans1;
        else
          cout << min(ans1, ans2);
      }
    }
    cout << endl;
  }
  return 0;
}
