#include <bits/stdc++.h>
using namespace std;
const int N = 300;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, x, y, d;
    cin >> n >> x >> y >> d;
    long long diff = abs(y - x);
    if (diff % d == 0) {
      cout << diff / d << '\n';
      continue;
    }
    long long mn = 1e18;
    if ((y - 1) % d == 0) {
      mn = (x - 1 + d - 1) / d;
      mn += y / d;
    }
    if ((n - y) % d == 0) {
      long long cur = ((n - x) + d - 1) / d;
      cur += ((n - y) + d - 1) / d;
      mn = min(mn, cur);
    }
    if (mn == 1e18)
      cout << -1 << '\n';
    else
      cout << mn << '\n';
  }
  return 0;
}
