#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, x, y, d;
    cin >> n >> x >> y >> d;
    if (abs(x - y) % d == 0) {
      cout << abs(x - y) / d;
    } else {
      long long a = 1e17, b = 1e17;
      if ((y - 1) % d == 0) {
        a = ceil((double)(x - 1) / d) + (y - 1) / d;
      }
      if ((n - y) % d == 0) {
        b = ceil((double)(n - x) / d) + (n - y) / d;
      }
      if (a == b && a == 1e17) {
        cout << -1;
      } else {
        cout << min(a, b);
      }
    }
    cout << endl;
  }
  return 0;
}
