#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int t;
  long long n, x, y, d;
  scanf("%d", &t);
  while (t--) {
    cin >> n >> x >> y >> d;
    if (abs(x - y) % d == 0) {
      cout << abs(x - y) / d << endl;
    } else {
      long long left = -1, right = -1;
      if ((y - 1) % d == 0) {
        if ((x - 1) % d) {
          left = (x - 1) / d + 1;
        } else {
          left = (x - 1) / d;
        }
        left += (y - 1) / d;
      }
      if ((n - y) % d == 0) {
        if ((n - x) % d) {
          right = (n - x) / d + 1;
        } else {
          right = (n - x) / d;
        }
        right += (n - y) / d;
      }
      if (left != -1 && right != -1) {
        cout << min(left, right) << endl;
      } else if (left != -1 || right != -1) {
        cout << max(left, right) << endl;
      } else {
        cout << -1 << endl;
      }
    }
  }
  return 0;
}
