#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t, n, x, y, d;
  cin >> t;
  while (t--) {
    long long ans, left = 0, right = 0, x_left, x_right, leftres, rightres;
    cin >> n >> x >> y >> d;
    if (abs(y - x) % d == 0) {
      ans = abs(y - x) / d;
    } else {
      if (abs(y - 1) % d == 0) {
        left = 1;
        leftres = abs(y - 1) / d + abs(x - 1) / d + 1;
      }
      if (abs(n - y) % d == 0) {
        right = 1;
        rightres = abs(n - y) / d + abs(n - x) / d + 1;
      }
      if (left) {
        if (right == 0) {
          ans = leftres;
        } else {
          ans = min(leftres, rightres);
        }
      } else {
        if (right == 0) {
          ans = -1;
        } else {
          ans = rightres;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
