#include <bits/stdc++.h>
using namespace std;
long long Read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long m, x, y, n;
long long abss(long long a) { return a >= 0 ? a : -a; }
long long minn(long long a, long long b) { return a > b ? b : a; }
int main() {
  long long T = Read(), ans;
  for (int i = 1; i <= T; i++) {
    m = Read(), x = Read(), y = Read(), n = Read();
    ans = 1e9 + 7;
    if (abss(x - y) % n == 0) {
      cout << abss(x - y) / n << endl;
      ans = 0;
    } else {
      if (abss(1 - y) % n == 0)
        ans = (abss(x - 1) + n - 1) / n + abss(y - 1) / n;
      if (abss(m - y) % n == 0)
        ans = minn(ans, (abss(m - x) + n - 1) / n + abss(y - m) / n);
      if (ans == 1e9 + 7)
        cout << "-1" << endl;
      else
        cout << ans << endl;
    }
  }
  return 0;
}
