#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int moth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int mod = 1e9 + 7;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, x, y, k;
    cin >> n >> x >> y >> k;
    if (x < y && (y - x) % k == 0) {
      cout << (y - x) / k << endl;
      continue;
    } else if (x >= y && (x - y) % k == 0) {
      cout << (x - y) / k << endl;
      continue;
    }
    if ((y - 1) % k == 0 || (n - y) % k == 0) {
      int a = 0x3f3f3f3f, b = 0x3f3f3f3f;
      if ((y - 1) % k == 0)
        a = (y - 1) / k + ((x - 1) % k == 0 ? (x - 1) / k : (x - 1) / k + 1);
      if ((n - y) % k == 0)
        b = (n - y) / k + ((n - x) % k == 0 ? (n - x) / k : (n - x) / k + 1);
      cout << min(a, b) << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
