#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, x, y, d, res1, res2, res3, f = 0;
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    res1 = INT_MAX;
    res2 = INT_MAX;
    res3 = INT_MAX;
    f = 0;
    if (abs(x - y) % d == 0) {
      res1 = abs(x - y) / d;
      f = 1;
    }
    if ((y - 1) % d == 0) {
      res2 = ((y - 1) / d) + (((x - 1) / d) + 1);
      f = 1;
    }
    if ((n - y) % d == 0) {
      res3 = ((n - y) / d) + (((n - x) / d) + 1);
      f = 1;
    }
    if (f == 0) {
      cout << -1 << endl;
    } else {
      cout << min(res1, min(res2, res3)) << endl;
    }
  }
  return 0;
}
