#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int n, x, y, d;
    cin >> n >> x >> y >> d;
    int det = abs(x - y);
    if (det % d == 0) {
      cout << det / d << endl;
      continue;
    } else if (y == 1) {
      cout << (x - 1 + d - 1) / d << endl;
      continue;
    } else if (y == n) {
      cout << (n - x + d - 1) / d << endl;
      continue;
    }
    if ((y - 1) % d != 0 && (n - y) % d != 0) {
      cout << -1 << endl;
    } else if ((y - 1) % d != 0) {
      cout << (n - x + d - 1) / d + (n - y) / d << endl;
    } else if ((n - y) % d != 0) {
      cout << (x - 1 + d - 1) / d + (y - 1) / d << endl;
    } else
      cout << min((n - x + d - 1) / d + (n - y) / d,
                  (x - 1 + d - 1) / d + (y - 1) / d)
           << endl;
  }
}
