#include <bits/stdc++.h>
using namespace std;
const int N = 1;
const int MOD = 1e9 + 7;
const int bm = 1;
int n, m;
void sinh() {
  freopen(".inp", "w", stdout);
  srand(time(0));
}
int main() {
  ios_base::sync_with_stdio(0);
  int t, first, second, d;
  cin >> t;
  while (t--) {
    cin >> n >> first >> second >> d;
    int MIN = MOD;
    if ((second - first) % d == 0) MIN = abs(second - first) / d;
    if (second % d == 1) MIN = min((first - 1) / d + (second - 1) / d + 1, MIN);
    if ((n - second) % d == 0)
      MIN =
          min((n - first) / d + (n - second) / d + ((n - first) % d != 0), MIN);
    if (MIN == MOD)
      cout << -1;
    else
      cout << MIN;
    cout << endl;
  }
}
