for s in [*open(0)][1:]:
n,x,y,d=map(int,s.split())
a,b,c=abs(x-y)//d,(x+d-1)//d+(y-1)//d,-((x-n)//d)+(n-y)//d
r=a if x%d==y%d else -1
if r<0 and y%d==1:r=b
if n%d==y%d:r=[min(r,c),c][r<0]
print(r)
#pragma GCC optimize(0)
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <cstring>
#define fi first
#define se second
#define endl "\n"
#define int long long
#define loop(i,a,b) for(int i = a;i <= b;i ++)
#define pool(i,a,b) for(int i = a;i >= b;i --)
using namespace std;
typedef pair<int,int> PII;
inline void YES(){cout << "YES" << endl;}
inline void NO(){cout << "NO" << endl;}
const int N = 2e5 + 5,M = 3e3 + 5,S = 1e6 + 7,Mod = 998244353;
void solve(){
int n,x,y,d;
cin >> n >> x >> y >> d;
int ans = 1e18;
int len = abs(x - y);
//直接走
if(len % d == 0) ans = min(ans,len / d);
//先走到1;
if((y - 1) % d == 0) {
ans = min(ans,(x + d - 2) / d + (y - 1) / d);
}
if((n - y) % d == 0) {
ans = min(ans,(n - x + d - 1) / d + (n - y) / d);
}
if(ans == 1e18) ans = -1;
cout << ans << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t --){
solve();
}
return 0;
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |