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 ll long long

ll gcd(ll a, ll b) {
  return b ? gcd(b, a % b) : a;
}
void exgcd(ll a, ll b, ll& x, ll& y) {
  if (!b) {
    x = 1, y = 0;
  } else {
    exgcd(b, a % b, y, x);
    y -= (a / b) * x;
  }
}
ll inv(ll a, ll Mod) {
  ll x, y;
  exgcd(a, Mod, x, y);
  return x < 0 ? x + Mod : x;
} 

typedef class vec {
  public:
    ll dx, dy;
    int c;

    vec(ll dx, ll dy, int c) : dx(dx), dy(dy), c(c) { }
} vec;

int T;
ll A, X, B, Y, C;
vector<vec> conv;

void solve() {
  scanf("%lld%lld%lld%lld%lld", &A, &X, &B, &Y, &C);
  ll g = gcd(A, B), ga, gb;
  A /= g, B /= g, C /= gcd(g, C);
  ga = gcd(A, C);
  A /= ga, Y /= ga, C /= ga;
  gb = gcd(B, C);
  B /= gb, X /= gb, C /= gb;
  if (C == 1) {
    printf("%lld\n", X + Y);
    return;
  }

  conv.clear();
  ll W = C, H = A * inv(B, C) % C, x0 = 0, ia = inv(A, C), p = 0, np;
  while (W && H) {
    if (W >= H) {
      ll t = W / H;
      W -= t * H;
      x0 += t * H;
      np = x0 * B % C * ia % C;
      np += !np * C;
      conv.emplace_back((np - p) / t, H, t);
      p = np;
    } else {
      if (!(H % W)) { 
        conv.emplace_back(C - p, W, 1);
        break;
      } else {
        H %= W;
      }
    }
  }

  ll l = Y / C + 1, r = X + Y, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    ll ca = 0, cb = mid * C;
    for (auto v : conv) {
      ll ub = min((cb - Y - 1) / v.dy + 1, 1ll * v.c * mid);
      ub = min(ub, 1000000001ll);
      ca += v.dx * ub;
      cb -= v.dy * ub;
      if (ca > X || cb <= Y) {
        break;
      }
    }
    if (ca <= X && cb <= Y) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  printf("%d\n", l - 1);
}

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