#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  long long x, y, n, d;
  while (q--) {
    cin >> n >> x >> y >> d;
    if (abs(x - y) % d == 0) {
      cout << abs(x - y) / d << endl;
      continue;
    }
    int cnt = INT_MAX;
    if ((y - 1) % d == 0) {
      cnt = y / d + (x / d) + (x % d == 0 ? 0 : 1);
    }
    if ((n - y) % d == 0) {
      cnt = min((n - y) / d + (n - x) / d + ((n - x) % d == 0 ? 0 : 1),
                (long long)cnt);
    }
    if (cnt == INT_MAX)
      cout << -1 << endl;
    else
      cout << cnt << endl;
  }
  return 0;
}
