#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
istream &operator>>(istream &in, pair<T, U> &data) {
  in >> data.first >> data.second;
  return in;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vect) {
  for (unsigned i = 0; i < vect.size(); i++) {
    in >> vect[i];
  }
  return in;
}
namespace std {
template <>
struct hash<std::pair<int, int>> {
  inline size_t operator()(const std::pair<int, int> &v) const {
    std::hash<int> int_hasher;
    return int_hasher(v.first) ^ int_hasher(v.second);
  }
};
}  // namespace std
template <typename Out>
void Split(const std::string &s, char delim, Out result) {
  std::stringstream ss(s);
  std::string item;
  while (std::getline(ss, item, delim)) {
    *(result++) = item;
  }
}
std::vector<std::string> str_split(const std::string &s, char delim) {
  std::vector<std::string> elems;
  Split(s, delim, std::back_inserter(elems));
  return elems;
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int ti = 1; ti <= t; ti++) {
    int n, d, x, y;
    cin >> n >> x >> y >> d;
    if (x == y) {
      cout << 0 << endl;
    } else if (x < y && ((y - x) % d == 0)) {
      cout << (y - x) / d << endl;
    } else if (x > y && ((x - y) % d == 0)) {
      cout << (x - y) / d << endl;
    } else {
      int from_first = -1;
      int from_sec = -1;
      if ((y - 1) % d == 0) {
        from_first = (y - 1) / d + (x - 1 + d - 1) / d;
      }
      if ((n - y) % d == 0) {
        from_sec = (n - y) / d + (n - x + d - 1) / d;
      }
      if (from_first == -1 && from_sec == -1) {
        cout << -1 << endl;
      } else {
        if (from_first == -1) {
          cout << from_sec << endl;
        } else if (from_sec == -1) {
          cout << from_first << endl;
        } else {
          cout << min(from_first, from_sec) << endl;
        }
      }
    }
  };
  return 0;
}
