#include <bits/stdc++.h>
using namespace std;
int n, x, y, d, T;
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> x >> y >> d;
    int sum;
    if ((abs(y - x)) % d == 0 || (n - y) % d == 0 || (y - 1) % d == 0) {
      if ((abs(y - x)) % d == 0)
        sum = (abs(y - x)) / d;
      else {
        int sxl, sxr;
        if ((x - 1) % d)
          sxl = (x - 1) / d + 1;
        else
          sxl = (x - 1) / d;
        if ((n - x) % d)
          sxr = (n - x) / d + 1;
        else
          sxr = (n - x) / d;
        int fl = (y - 1) % d, fr = (n - y) % d;
        if (fl == 0 && fr != 0) sum = sxl + (y - 1) / d;
        if (fl != 0 && fr == 0) sum = sxr + (n - y) / d;
        if (fl == 0 && fr == 0) {
          sum = min(sxl + (y - 1) / d, sxr + (n - y) / d);
        }
      }
      cout << sum << endl;
    } else
      cout << "-1" << endl;
  }
  return 0;
}
