#include <bits/stdc++.h>
using namespace std;
vector<string> v, v1;
map<string, long long> mp, mp1, mp2;
char final[100000];
long long a[10000];
bool compare(string &s1, string &s2) { return s1.size() < s2.size(); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, x, y, x1, y1, test, d, move, right, diff1, diff2, min1, min2,
      min0;
  cin >> test;
  while (test--) {
    cin >> n >> x >> y >> d;
    x1 = min(x, y);
    y1 = max(x, y);
    if (x1 == y1) {
      cout << "0\n";
    } else {
      right = (y1 - x1) / d;
      if (d * right == y1 - x1) {
        cout << right << "\n";
        continue;
      } else {
        diff1 = x - 1;
        diff2 = n - x;
        min1 = -1;
        min2 = -1;
        move = ceil((double)diff1 / d);
        x = 1;
        right = (y - x) / d;
        if (d * right == y - x) {
          min1 = move + right;
        }
        move = ceil((double)diff2 / d);
        x = y;
        y = n;
        right = (y - x) / d;
        if (d * right == y - x) {
          min2 = move + right;
        }
        if (min1 == -1 && min2 == -1) {
          cout << "-1\n";
        } else {
          if (min1 != -1 && min2 != -1) {
            cout << min(min1, min2) << "\n";
          } else {
            if (min1 != -1) {
              cout << min1 << "\n";
            } else {
              cout << min2 << "\n";
            }
          }
        }
      }
    }
  }
}
