#include <bits/stdc++.h>
using namespace std;
int n, d, x, y;
vector<int> arr;
long long answer;
void ReadInput() { cin >> n >> x >> y >> d; }
long long Solve() {
  int left = -1;
  int right = -1;
  if (abs(x - y) % d == 0) return abs(x - y) / d;
  if ((y - 1) % d == 0) left = (y - 1) / d + (x - 1 + d - 1) / d;
  if ((n - y) % d == 0) right = (n - y) / d + (n - x + d - 1) / d;
  if (left == -1 && right == -1) return -1;
  if (left == -1) return right;
  if (right == -1) return left;
  return min(left, right);
}
void WriteOutput() { cout << answer << endl; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (size_t i = 0; i < t; i++) {
    ReadInput();
    answer = Solve();
    WriteOutput();
  }
  return 0;
}
