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>
#include <iostream>
#include <vector>
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
inline char gc() {
//	return getchar();
	static char buf[100000],*l=buf,*r=buf;
	return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<class T> void rd(T &x) {
	scanf("%d",&x); return;
	x=0; int f=1,ch=gc();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
	while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=gc();}
	x*=f;
}
template<class T> inline bool Cmax(T &x,T y) {return x<y?x=y,1:0;} 
typedef long long ll;
int T,A,X,B,Y,C,D;
vector<int> pos,cnt;
int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
int exgcd(int a,int b,int &x,int &y) {
	if(b==0) {x=1,y=0; return a;}
	int d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
inline int inver(int a,int mod) {
	int x,y,d=exgcd(a,mod,x,y);
	if(x<0) x+=mod;
	return x;
}
inline ll lowdiv(ll a,ll b) {return a/b-(a%b&&(a^b)<0);}
void init() {
	int d=gcd(A,B);
	A/=d,B/=d,C/=gcd(d,C);
	for(int _=0;_<2;++_) {
		d=gcd(A,C);
		C/=d,A/=d;
		int t=gcd(B,d);
		B/=t,Y/=d/t;
		swap(A,B),swap(X,Y);
	}
}
void getpos() {
	pos.clear(),cnt.clear();
	int W=C,H=D,now=0,r=inver(D,C);
	while(W) {
		int d=W/H;
		pos.push_back((ll)now*r%C),cnt.push_back(d); 
		now+=d*H;
		W%=H; if(W==0) break;
		H%=W; if(H==0) H=W;
	}
	pos.push_back(C);
}
inline int gety(int x) {return x==0?C:(C-(ll)x*D%C)%C;}
int main() {
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout);
	rd(T);
	for(int kase=1;kase<=T;++kase) {
		rd(A),rd(X),rd(B),rd(Y),rd(C);
		init();
		if(C==1) {printf("%d\n",X+Y); continue;}
		D=(ll)A*inver(B,C)%C;
		getpos();
		int an=0;
		for(int i=0;i<cnt.size();++i) {
			int xl=pos[i],xr=pos[i+1],yl=gety(xl),yr=gety(xr);
			int dx=(xr-xl)/cnt[i],dy=(yl-yr)/cnt[i];
			int l=0,r=X+Y,re=-1;
			while(l<=r) {
				int mid=((ll)l+r)>>1;
				ll p=lowdiv(X-(ll)xl*mid,dx),q=lowdiv(Y-(ll)yr*mid,dy);
				if(p>=0&&q>=0&&p+q>=(ll)cnt[i]*mid) re=mid,l=mid+1;
				else r=mid-1;
			}
			Cmax(an,re);
		}
		printf("%d\n",an);
	}
	return 0;
}