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>
using namespace std;

#define int long long
int a,b,x,y,c;

inline int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}

inline void exgcd(int a,int b,int &x,int &y){
	if(b)exgcd(b,a%b,y,x),y-=a/b*x;
	else x=1,y=0;
}

inline int inv(int x,int p){//gcd(a,b)=0;
	int a,b;exgcd(x,p,a,b);
	if(a<0)a+=p;
	return a;
}

vector<int> s, t;

inline int gety(int x,int step){
	if(x==0)return c;
	return (c-1ll*x*step%c)%c;
}

inline int lowdiv(int x,int y){
	return x/y-(x%y&&(x^y)<0);
}

void Main(){
	int ans=0;
	cin >> a >> x >> b >> y >> c;
	int g=gcd(a,b);a/=g,b/=g;
	c/=gcd(c,g);
	for(int _=1;_<=2;_++){
		g=gcd(a,c);
		c/=g,a/=g;int h=gcd(b,g);
		b/=h,y/=g/h;

		swap(a,b),swap(x,y);
	}
	// cerr << a << " " << x << " " << b << " " << y << " " << c << endl;
	if(c==1){
		printf("%lld\n",x+y);return ;
	}
	int step=1ll*inv(b,c)*a%c;
	vector<int> pos, cnt;
		int z=inv(step, c), h=step, w=c, cur=0;
		while(w){
			int p=w/h;
			pos.push_back(1ll*cur*z%c);
			cnt.push_back(p);
			cur+=p*h;
			w%=h;
			if(w==0)break;
			h%=w;
			if(h==0)h=w;
		}
		pos.push_back(c);
	// for(size_t i=0;i<pos.size();i++)cout << pos[i] << " " ;puts("");
	// for(size_t i=0;i<cnt.size();i++)cout << cnt[i] << " ";puts("");
	// cerr << "????" << endl;
	for(size_t i=0;i<cnt.size();i++){
		int lx=pos[i], ly=gety(lx, step);
		int rx=pos[i+1], ry=gety(rx, step);
		
		// cerr << lx << " " << ly << " " << rx << " " << ry << endl;
		// cerr << cnt[i] << "??" << endl;
		int dx=(rx-lx)/cnt[i];
		int dy=(ly-ry)/cnt[i];
		// cerr << dx << " " << dy << endl;
		int lw=0,up=x+y+1;
		// cerr << "st" << endl;
		while(up-lw>1){
			int mid=(lw+up)>>1;
			int p=lowdiv(x-lx*mid,dx);
			int q=lowdiv(y-ry*mid,dy);
			if(p>=0&&q>=0&&p+q>=cnt[i]*mid)
				lw=mid;
			else
				up=mid;
		}
		// cerr << "ed" << endl;
		ans=max(ans, lw);
	}
	printf("%lld\n",ans);
}
#undef int

int main(){int T;cin >> T;while(T--)Main();}