#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const long long INF = 0x3f3f3f3f3f3f3f;
int t;
long long n, x, y, d, m;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%I64d%I64d%I64d%I64d", &n, &x, &y, &d);
    if (labs(x - y) % d == 0) {
      cout << labs(x - y) / d << endl;
    } else {
      int l = (x - 1) / d;
      if ((x - 1) % d) l++;
      int r = (n - x) / d;
      if ((n - x) % d) r++;
      long long ans = INF;
      if ((y - 1) % d == 0) {
        ans = min(ans, l + (y - 1) / d);
      }
      if ((n - y) % d == 0) {
        ans = min(ans, r + (n - y) / d);
      }
      if (ans != INF) {
        cout << ans << endl;
      } else {
        cout << -1 << endl;
      }
    }
  }
  return 0;
}
