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