#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    long long a, b, c, x;
    long long resp = 1e10;
    cin >> a >> b >> c >> x;
    long long k = abs(b - c);
    long long cur = 1e10;
    if (k % x == 0)
      resp = k / x;
    else {
      if ((c - 1) % x == 0)
        cur = min(cur, (b - 1 + x - 1) / x + (c - 1 + x - 1) / x);
      if ((a - c) % x == 0)
        cur = min(cur, (a - b + x - 1) / x + (a - c + x - 1) / x);
    }
    if (cur == 1e10 && resp == 1e10)
      resp = -1;
    else
      resp = min(resp, cur);
    cout << resp << '\n';
  }
  return 0;
}
