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<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
#define int long long
using namespace std;
int A,B,C,X,Y,H,G,D;
int top,q1[555],q2[555],q3[555];
int gcd(int x,int y){
	return y?gcd(y,x%y):x;
}
void exgcd(int x,int y,int &a,int &b){
	if (!y){
		a=1; b=0;
		return;
	}
	exgcd(y,x%y,b,a);
	b-=x/y*a;
}
int getinv(int x,int y){
	int a,b;
	exgcd(x,y,a,b);
	return (a%y+y)%y;
}
void get_seq(int C,int D){
	top=q1[0]=q2[0]=0;
	int len=1,x,a=1,b=0;
	for (;D;){
		//cout<<"debug "<<C<<' '<<D<<' '<<a<<endl;
		++top;
		q1[top]=q1[top-1]+C/D*a;
		q2[top]=q2[top-1]+C/D*D;
		q3[top]=C/D;
		For(T,1,2){
			if (T==1) b+=C/D*a;
			else a+=C/D*b;
			x=C%D; C=D; D=x;
			if (!D) break;
		}
	}
}
ll Mydiv(ll x,ll y){
	return x<0?-1:x/y;
}
void solve(){
	scanf("%lld%lld%lld%lld%lld",&A,&X,&B,&Y,&C);
	G=gcd(A,B); A/=G; B/=G; C/=gcd(C,G);
	G=gcd(A,C); A/=G; C/=G;
	H=gcd(G,B); B/=H; Y/=G/H;
	G=gcd(B,C); B/=G; C/=G;
	H=gcd(G,A); A/=H; X/=G/H;
	A%=C; B%=C;
	if (C==1) return cout<<X+Y<<endl,void(0);
	D=1ll*A*getinv(B,C)%C; get_seq(C,D);
	if (q1[top]!=C) q1[++top]=C,q2[top]=C,q3[top]=1;
	//For(i,1,top) cout<<q1[i]<<' '<<q2[i]<<' '<<q3[i]<<endl;
	int ans=0;
	For(i,1,top){
		int lx=q1[i-1],rx=q1[i];
		int ly=C-q2[i-1],ry=C-q2[i];
		int dx=(rx-lx)/q3[i],dy=(ly-ry)/q3[i];
		int l=ans+1,r=X+Y+1;
		//cout<<lx<<' '<<ry<<' '<<dx<<' '<<dy<<' '<<ans<<endl;
		while (l<=r){
			int mid=(l+r)/2;
			ll p=Mydiv(X-1ll*mid*lx,dx);
			ll q=Mydiv(Y-1ll*mid*ry,dy);
			if (p>=0&&q>=0&&p+q>=1ll*q3[i]*mid)
				ans=max(ans,mid),l=mid+1;
			else r=mid-1;
		}
	}
	cout<<ans<<endl;
}
signed main(){
	int T;
	scanf("%lld",&T);
	while (T--) solve();
}