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