#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
using namespace std;
int t, n, x, y, d;
void Pro() {
  cin >> n >> x >> y >> d;
  if (x == y) return void(cout << 0 << '\n');
  if (abs(x - y) % d == 0) return void(cout << abs(x - y) / d << '\n');
  int ca1, ca2;
  ca1 = ca2 = 1000000007;
  if (y % d == 1) {
    ca1 = (y - 1) / d;
    ca1 += (x - 2) / d + 1;
  }
  if ((n - y) % d == 0) {
    ca2 = (n - y) / d;
    ca2 += (n - x - 1) / d + 1;
  }
  ca1 = min(ca1, ca2);
  if (ca1 == 1000000007) ca1 = -1;
  cout << ca1 << '\n';
}
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> t;
  while (t--) Pro();
  return 0;
}
