#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first > b.first;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int test;
  cin >> test;
  for (int i = 0; i < test; i++) {
    long long n, x, y, d;
    cin >> n >> x >> y >> d;
    if (abs(x - y) % d == 0)
      cout << abs(x - y) / d << endl;
    else {
      if ((y - 1) % d == 0 || (n - y) % d == 0) {
        long long ans1 = numeric_limits<long long>::max(),
                  ans2 = numeric_limits<long long>::max();
        if ((y - 1) % d == 0)
          ans1 = ((y - 1) / d) + (((x - 1) / d) + ((x - 1) % d != 0));
        if ((n - y) % d == 0)
          ans2 = ((n - y) / d) + (((n - x) / d) + ((n - x) % d != 0));
        cout << min(ans1, ans2) << endl;
      } else
        cout << -1 << endl;
    }
  }
  return 0;
}
