A. Vasya and Book

Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.

Input

The first line contains one integer tt (1≤t≤1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤1091≤n,d≤109, 1≤x,y≤n1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

Output

Print one line for each test.

If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print −1−1.

Example

input

3
10 4 5 2
5 1 3 4
20 4 19 3

output

4
-1
5

题意:一本笔记本总共有n页,现在要从x页翻到y页,每次翻页必须间隔d页,最多翻到第一页或者最后一页,求从x页翻到y页需要的最小次数。

 

思路:只需要判断三次:

是否能直接从x页翻到y页;

是否能从第一页翻到y页;

是否能从最后一页翻到y页;

 

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)

int main()
{
	int t,n,d,x,y,s,s1,s2,num,num1,num2,flag;
	cin>>t;
	while(t--)
	{
		flag=0;
		cin>>n>>x>>y>>d;
		s=abs(x-y);
		if(s%d==0)
		{
			cout<<s/d<<endl;
			continue;
		}
		if((n-x)%d==0)
			s1=(n-x)/d;
		else
			s1=(n-x)/d+1; 
		if((x-1)%d==0)
			s2=(x-1)/d;
		else
			s2=(x-1)/d+1; 
		
		if((n-y)%d==0)
			num1=(n-y)/d+s1;
		else
			num1=inf;
		if((y-1)%d==0)
			num2=(y-1)/d+s2;
		else
			num2=inf;	
		num=min(num1,num2);
		if(num==inf)
			cout<<"-1"<<endl;
		else
		cout<<num<<endl;
	}	
	return 0;
}