#include <bits/stdc++.h>
using namespace std;
int setBit(int n, int p) { return n | (1 << p); }
bool checkBit(int n, int p) { return (bool)(n & (1 << p)); }
int fx[] = {0, -1, 0, 1};
int fy[] = {1, 0, -1, 0};
long long bigMod(long long x, long long n) {
  if (n == 0) return 1;
  long long t = bigMod(x, n / 2);
  t = ((t % 1000000007) * (t % 1000000007)) % 1000000007;
  if (n % 2) {
    t = ((x % 1000000007) * (t % 1000000007)) % 1000000007;
  }
  return t;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, x, y, d, p, q, r, ans = 100000000000000;
    cin >> n >> x >> y >> d;
    long long temp = abs(y - x);
    if (temp % d == 0) {
      cout << temp / d << endl;
    } else {
      temp = abs(x - 1);
      if (temp == 0) {
        p = 0;
      } else {
        p = (temp - 1) / d + 1;
      }
      if ((y - 1) % d == 0) {
        ans = p + (y - 1) / d;
      }
      temp = abs(n - x);
      if (temp == 0) {
        q = 0;
      } else {
        q = (temp - 1) / d + 1;
      }
      if ((n - y) % d == 0) {
        ans = min(ans, q + (n - y) / d);
      }
      if (ans == 100000000000000)
        cout << -1 << endl;
      else
        cout << ans << endl;
    }
  }
  return 0;
}
