#include <bits/stdc++.h>
using namespace std;
const long long Inf = 1e15 + 100;
long long x, y, n, t, d, ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t >> x >> y >> d;
    ans = Inf;
    if ((y - x) % d == 0) {
      ans = (abs(x - y)) / d;
    }
    if ((y - 1) % d == 0) {
      int k = x / d;
      if (x % d != 0) k++;
      ans = min(ans, k + (y - 1) / d);
    }
    if ((t - y) % d == 0) {
      int k = (t - x) / d;
      if ((t - x) % d != 0) k++;
      ans = min(ans, k + (t - y) / d);
    }
    if (ans == Inf)
      cout << -1;
    else
      cout << ans;
    cout << endl;
  }
}
