#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main() {
  int t;
  for (cin >> t; t--;) {
    int n, x, y, d;
    cin >> n >> x >> y >> d;
    int an = inf;
    if (abs(x - y) % d == 0) {
      an = abs(x - y) / d;
    } else {
      if ((y - 1) % d == 0) {
        an = min(an, (x - 1) / d + 1 + (y - 1) / d);
      }
      if ((n - y) % d == 0) {
        an = min(an, (n - x) / d + 1 + (n - y) / d);
      }
    }
    if (an == inf) an = -1;
    cout << an << endl;
  }
}
