#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, n, x, d, y, c = 0, f = 1000000000, l = 1000000000,
                               r = 1000000000, fx, lx;
  cin >> t;
  while (t--) {
    f = 1000000000, l = 1000000000, r = 1000000000;
    cin >> n >> x >> y >> d;
    if (x == y) {
      cout << 0 << endl;
      continue;
    }
    r = ((x - y) > 0) ? (x - y) : (y - x);
    if (r % d == 0) {
      cout << r / d << endl;
      continue;
    }
    if ((y - 1) % d == 0) f = (y - 1) / d;
    if ((n - y) % d == 0) l = (n - y) / d;
    if (f != 1000000000) {
      fx = ((x - 1) % d == 0) ? (x - 1) / d : ((x - 1) / d) + 1;
      f = f + fx;
    }
    if (l != 1000000000) {
      lx = ((n - x) % d == 0) ? (n - x) / d : ((n - x) / d) + 1;
      l = l + lx;
    }
    r = (f <= l) ? f : l;
    if (r == 1000000000) {
      cout << "-1" << endl;
      continue;
    }
    cout << r << endl;
  }
  return 0;
}
