#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
void c_t_c() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  c_t_c();
  int t;
  cin >> t;
  while (t--) {
    long long n, x, y, d;
    cin >> n >> x >> y >> d;
    long long ans;
    if (abs(x - y) % d == 0 || y == 1 || y == n) {
      ans = abs(x - y) / d;
      if (abs(x - y) % d != 0) {
        ans += 1;
      }
      cout << ans << endl;
    } else {
      long long c1 = abs(x - 1) / d + abs(1 - y) / d;
      long long c2 = abs(n - x) / d + abs(n - y) / d;
      if ((x - 1) % d != 0) {
        c1 += 1;
      }
      if ((n - x) % d != 0) {
        c2 += 1;
      }
      if (abs(1 - y) % d == 0 && abs(n - y) % d == 0) {
        cout << min(c1, c2) << endl;
      } else if (abs(1 - y) % d == 0) {
        cout << c1 << endl;
      } else if (abs(n - y) % d == 0) {
        cout << c2 << endl;
      } else {
        cout << -1 << endl;
      }
    }
  }
  return 0;
}
