#include <bits/stdc++.h>
inline int sbt(int x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, x, y, d;
    int res = 1000000007;
    cin >> n >> x >> y >> d;
    int dff = abs(x - y);
    if (!(dff % d)) res = min(res, dff / d);
    if (!((y - 1) % d))
      res = min(res, (y - 1) / d + (x - 1) / d + (((x - 1) % d) > 0));
    if (!((n - y) % d))
      res = min(res, (n - x) / d + (n - y) / d + (((n - x) % d) > 0));
    if (res == 1000000007)
      cout << -1;
    else
      cout << res;
    cout << endl;
  }
  return 0;
}
