Gift Set

Polycarp has xx of red and yy of blue candies. Using them, he wants to make gift sets. Each gift set contains either aa red candies and bb blue candies, or aa blue candies and bb red candies. Any candy can belong to at most one gift set.

Help Polycarp to find the largest number of gift sets he can create.

For example, if x=10x=10, y=12y=12, a=5a=5, and b=2b=2, then Polycarp can make three gift sets: 

  • In the first set there will be 55 red candies and 22 blue candies; 
  • In the second set there will be 55 blue candies and 22 red candies; 
  • In the third set will be 55 blue candies and 22 red candies. 

Note that in this example there is one red candy that Polycarp does not use in any gift set.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

Each test case consists of a single string containing four integers xx, yy, aa, and bb (1≤x,y,a,b≤1091≤x,y,a,b≤109).

Output

For each test case, output one number — the maximum number of gift sets that Polycarp can make.

Example

Input

9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3

Output

3
0
4
1000000000
1
1
1
5
0 

 

 


#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int t,x,y,a,b;
bool check(int mid)
{
	if(b==a) 
		return mid<=min(x/a,y/b);
	int l=(int)ceil(1.0*(y-a*mid)/(b-a)); 
	int r=(int)floor(1.0*(x-b*mid)/(a-b));
	l=max(0LL,l),r=min(r,mid);
	if(l<=r) return 1;
	else return 0;
} 

signed main()																						
{	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>x>>y>>a>>b;
		if(a<b) swap(a,b);
		int l=0,r=max(x/a,y/b),ans=0;
		while(l<=r)
		{
			int mid=l+r>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值