#include <bits/stdc++.h>
using namespace std;
int n, x, y, d;
long long const MAX = 4 * 1e6;
int techo(int a, int b) {
  if (a % b) return a / b + 1;
  return a / b;
}
int main() {
  cin.tie(0), ios_base::sync_with_stdio(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> x >> y >> d;
    if (abs(y - x) % d == 0)
      cout << abs(y - x) / d << endl;
    else if ((y - 1) % d == 0) {
      if ((n - y) % d == 0) {
        cout << min(techo(x, d) + (y - 1) / d, techo(n - x, d) + (n - y) / d)
             << endl;
      } else
        cout << techo(x, d) + (y - 1) / d << endl;
    } else if ((n - y) % d == 0) {
      cout << techo(n - x, d) + (n - y) / d << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
