Problem: Snuke has X+Y balls. X of them have an integer A written on them, and the other Y of them have an integer B written on them.

Snuke will divide these balls into some number of groups. Here, every ball should be contained in exactly one group, and every group should contain one or more balls.

A group is said to be good when the sum of the integers written on the balls in that group is a multiple of an integer C. Find the maximum possible number of good groups.

Solve T test cases for each input file.

Constraints

* 1 \leq T \leq 2 \times 10^4
* 1 \leq A,X,B,Y,C \leq 10^9
* A \neq B

Input

Input is given from Standard Input in the following format. The first line is as follows:


T


Then, T test cases follow. Each test case is given in the following format:


A X B Y C


Output

For each test case, print a line containing the maximum possible number of good groups.

Example

Input

3
3 3 4 4 5
2 1 1 5 3
3 1 4 2 5


Output

2
2
0

Solution (in CPP):

Solution:
#include<cstdio>
int gcd(int a,int b){return b?gcd(b,a%b):a;}
long long inv(int a,int p){return p==1?0:(1+p*(a-inv(p%a,a)))/a%p;}
bool find(long long a1,long long b1,long long c1,long long a2,long long b2,long long c2,long long&x,long long&y){
	long long t1=a1/c1,t2=a2/c2,y1=b1/c1,y2=(b2-1)/c2;
	a1-c1*t1<0?t1--:1;
	a2-c2*t2<0?t2--:1;
	b1-c1*y1<0?y1--:1;
	b2-1-c2*y2<0?y2--:1;
	if(y1<y2){
		x=0;y=y2;
		return 1;
	}
	if(y1>y2||t1>t2||!a2)return 0;
	if(t1){
		bool res=find(a1-c1*t1,b1,c1,a2-c2*t2,b2,c2,x,y);y+=x*t1;
		return res;
	}
	bool res=find(c2,c2*(y2+1)-b2,a2,c1,c1*(y1+1)-b1,a1,x,y);
	t1=x;x=y;y=t1+y1+1;
	return res;
}
int calc(int A,int X,int B,int Y,int C){
	int G=gcd(B,C),z=C/G,x=G/gcd(A,G),y=1ll*A*x/G*inv(B/G,z)%z;
	if(!y)return X/x+Y/z;
	long long x0,y0,l=0,r=1ll*Y*x+1ll*X*y+1,m;
	while(r-l>1){
		m=(l+r)/2;
		if(find(-y,-y,z,-1ll*Y*x-1ll*X*y,m-1ll*Y*x-1ll*X*y,1ll*X*z,x0,y0))r=m;
		else l=m;
	}
	find(-y,-y,z,-1ll*Y*x-1ll*X*y,r-1ll*Y*x-1ll*X*y,1ll*X*z,x0,y0);
	y0=(x0+1)*y%z;x0=(x0+1)*x;
	int t=(x0*Y+y0*X)/z/x,x1=inv(x0/x*y/z,x0/x)*x*t%x0;
	x1=X-(X+x1)%x0;
	if(y0*x1+x0*Y<1ll*t*z*x)t--;
	return t;
}
int main(){
	int T;scanf("%d",&T);
	while(T--){
		int A,X,B,Y,C;
		scanf("%d%d%d%d%d",&A,&X,&B,&Y,&C);
		printf("%d\n",calc(A,X,B,Y,C));
	}
}