import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long INF = 1L << 59;

void main() {
    auto T = readln.chomp.to!int;

    while (T--) {
        auto s = readln.split.map!(to!long);
        auto N = s[0];
        auto X = s[1];
        auto Y = s[2];
        auto D = s[3];
        long tmp1 = INF;
        long tmp2 = INF;
        long tmp3 = INF;
        if (abs(X - Y) % D == 0) {
            tmp1 = abs(X - Y) / D;
        }
        if (abs(Y - 1) % D == 0) {
            tmp2 = abs(X - 1) / D + (abs(X - 1) % D > 0) + abs(Y - 1) / D;
        }
        if (abs(Y - N) % D == 0) {
            tmp3 = abs(X - N) / D + (abs(X - N) % D > 0) + abs(Y - N) / D;
        }

        long ans = min(tmp1, tmp2, tmp3);
        writeln(ans == INF ? -1 : ans);
    }
}
