//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1007*1007;

ll a, b, c, x, y;

void eukl(ll &x, ll &y, ll a, ll b) {
	if (!a) { x = 0; y = 1; return; }
	eukl(y, x, b % a, a);
	x -= y * (b / a);
}

ll odw(ll v)
{
	ll x, y;
	eukl(x, y, v, c);
	x%=c;
	x+=c;
	x%=c;
	return x;
}

void relax()
{
	ll g=__gcd(a, b);
	g=__gcd(g, c);
	a/=g;
	b/=g;
	c/=g;
	
	g=__gcd(a, b);
	a/=g;
	b/=g;
		
	g=__gcd(a, c);
	a/=g;
	c/=g;
	y/=g;
	
	g=__gcd(b, c);
	b/=g;
	c/=g;
	x/=g;
}

vector<pll> sta;
vector<pll> dod;
vll razy;
ll jest;

pll dajkt(int v, ll x)
{
	return {sta[v].first+x*dod[v].first, sta[v].second+x*dod[v].second};
}

pll daj(ll x)
{
	int v=0;
	while(razy[v]<=x)
	{
		x-=razy[v];
		v++;
	}
	return dajkt(v, x);
}

void euklides()
{
	ll mno=odw(b)*a;
	mno%=c;
	mno=c-mno;
	mno%=c;
	
	sta.clear();
	dod.clear();
	razy.clear();
	ll mini=1;
	ll maxi=1;
	ll wmi=mno;
	ll wma=mno;
	sta.push_back({0, c});
	dod.push_back({0, 0});
	razy.push_back(1);
	sta.push_back({1, mno});
	dod.push_back({0, 0});
	razy.push_back(1);
	while(mini+maxi<=c)
	{
		if ((wmi+wma)%c<wmi)
		{
			ll ile=wmi/(c-wma);
			sta.push_back({mini+maxi, (wmi+wma)%c});
			dod.push_back({maxi, wma-c});
			razy.push_back(ile);
			mini+=ile*maxi;
			wmi+=ile*wma;
			wmi%=c;
		}
		else
		{
			ll ile=(c-wma-1)/wmi;
			maxi+=ile*mini;
			wma+=ile*wmi;
			wma%=c;
		}
	}
	jest=0;
	for (ll i : razy)
		jest+=i;
}

int check(ll v)
{
	ll bsa=0;
	ll bsb=jest-1;
	while(bsa<bsb)
	{
		ll bss=(bsa+bsb+2)>>1;
		if (daj(bss).first*v<=x)
			bsa=bss;
		else
			bsb=bss-1;
	}
	ll g=bsa;
	if (g==jest-1)
		return 1;
	pll lew=daj(g);
	pll pra=daj(g+1);
	bsa=0;
	bsb=v;
	while(bsa<bsb)
	{
		ll bss=(bsa+bsb+2)>>1;
		if (bss*pra.first+(v-bss)*lew.first<=x)
			bsa=bss;
		else
			bsb=bss-1;
	}
	return (bsa*pra.second+(v-bsa)*lew.second<=y);
}

void test()
{
	scanf("%lld%lld%lld%lld%lld", &a, &x, &b, &y, &c);
	a%=c;
	b%=c;
	if (!a || !b)
	{
		ll jed=c/__gcd(c, a);
		ll dwa=c/__gcd(c, b);
		printf("%lld\n", x/jed+y/dwa);
		return;
	}
	relax();
	euklides();
	ll bsa=0;
	ll bsb=x+y;
	while(bsa<bsb)
	{
		ll bss=(bsa+bsb+2)>>1;
		if (check(bss))
			bsa=bss;
		else
			bsb=bss-1;
	}
	printf("%lld\n", bsa);
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		test();
	return 0;
}


Measured memory_bytes: 3832