The Frobenius Problem

No attempts yetTime limit1sMemory limit128 MB

Problem

The Frobenius problem is an old problem in mathematics, named after the German mathematician G. Frobenius (1849-1917).

Let a1,a2,,ana_1, a_2, \ldots, a_n be integers larger than 1 whose greatest common divisor is 1. It is known that only finitely many integers greater than or equal to 0 cannot be written as w1a1+w2a2++wnanw_1 a_1 + w_2 a_2 + \cdots + w_n a_n with integer coefficients wi0w_i \ge 0. The largest such integer is the Frobenius number of a1,a2,,ana_1, a_2, \ldots, a_n, written F(a1,a2,,an)F(a_1, a_2, \ldots, a_n). So F(a1,a2,,an)F(a_1, a_2, \ldots, a_n) is the largest nonnegative integer that is not a nonnegative integer linear combination of a1,a2,,ana_1, a_2, \ldots, a_n.

For n=2n = 2 there is a simple formula for F(a1,a2)F(a_1, a_2). For n3n \ge 3 it is much more complicated. When n=3n = 3, formulas are known only for special choices of a1,a2,a3a_1, a_2, a_3, and for n>4n > 4 no formula is known at all.

This problem asks about the case n=4n = 4. Four integers a,b,c,da, b, c, d are given with a,b,c,d>1a, b, c, d > 1 and gcd(a,b,c,d)=1\gcd(a, b, c, d) = 1. Two things are asked.

  • How many nonnegative integers less than or equal to 1,000,000 cannot be written as a nonnegative integer linear combination of aa, bb, cc and dd?
  • Is the Frobenius number of a,b,c,da, b, c, d less than or equal to 1,000,000, and if it is, what is its value?

Input

The first line contains the number of test cases. Each test case has the following format.

  • One line with four integers a,b,c,da, b, c, d separated by single spaces (1<a,b,c,d100001 < a, b, c, d \le 10000 and gcd(a,b,c,d)=1\gcd(a, b, c, d) = 1).

Output

Print two lines for every test case.

  • The first line contains the number of integers between 0 and 1,000,000 (both included) that cannot be written as aw+bx+cy+dza \cdot w + b \cdot x + c \cdot y + d \cdot z with nonnegative integers w,x,y,zw, x, y, z.
  • The second line contains the Frobenius number if it is less than or equal to 1,000,000, and -1 otherwise.