#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 2003;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t, n, x, y, d;
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    long long pageBack = x - 1, pageFront = n - x;
    long long b = y - 1, f = n - y;
    long long side1 = 0x3f3f3f3f, side2 = 0x3f3f3f3f, side3 = 0x3f3f3f3f;
    if (b % d == 0) {
      side1 = pageBack / d;
      if (pageBack % d) ++side1;
      side1 += (b / d);
    }
    if (f % d == 0) {
      side2 = pageFront / d;
      if (pageFront % d) ++side2;
      side2 += (f / d);
    }
    if (abs(y - x) % d == 0) side3 = abs(y - x) / d;
    long long ans = min(min(side1, side2), side3);
    if (ans == 0x3f3f3f3f) ans = -1;
    cout << ans << '\n';
  }
  return 0;
}
