#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, x, y, d;
    cin >> n >> x >> y >> d;
    int dis = abs(x - y);
    if (dis % d == 0)
      cout << dis / d << "\n";
    else {
      int mn = INT_MAX;
      if (abs(y - 1) % d == 0) {
        mn = min(mn, abs(y - 1) / d + (x + d - 1) / d);
      }
      if (abs(y - n) % d == 0) {
        mn = min(mn, abs(y - n) / d + ((n - x) + d - 1) / d);
      }
      if (mn != INT_MAX)
        cout << mn << "\n";
      else
        cout << -1 << "\n";
    }
  }
  return 0;
}
