atcoder#AGC045F. [AGC045F] Division into Multiples

[AGC045F] Division into Multiples

Score : 18001800 points

Problem Statement

Snuke has X+YX+Y balls. XX of them have an integer AA written on them, and the other YY of them have an integer BB 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 CC. Find the maximum possible number of good groups.

Solve TT test cases for each input file.

Constraints

  • 1T2×1041 \leq T \leq 2 \times 10^4
  • 1A,X,B,Y,C1091 \leq A,X,B,Y,C \leq 10^9
  • ABA \neq B

Input

Input is given from Standard Input in the following format. The first line is as follows:

TT

Then, TT test cases follow. Each test case is given in the following format:

AA XX BB YY CC

Output

For each test case, print a line containing the maximum possible number of good groups.

3
3 3 4 4 5
2 1 1 5 3
3 1 4 2 5
2
2
0

In the first test case, we can have two good groups by making the following groups: {3,3,4}\{3,3,4\} and {3,4,4,4}\{3,4,4,4\}.

In the second test case, we can have two good groups by making the following groups: {2,1},{1,1,1},\{2,1\}, \{1,1,1\}, and {1}\{1\}.