Card Operation (Large)

Order a set of arithmetic cards applied to a starting value to maximize the final rational result, printed as a reduced fraction.

Hard8GreedySortingMathBrute forceNo attempts yetTime limit5sMemory limit512 MB

Problem

A card game called Operation is played with a deck of operation cards. Each card holds one arithmetic operation OiO_i (addition, subtraction, multiplication, or division) and one integer right operand ViV_i for that operation. A card can read + 0, or - -2, or / -4. Operands can be negative or zero, but a division card never has 0 as its operand.

In each round a starting integer SS is chosen and a set of CC cards is laid out. The player picks an order for the cards and uses each card exactly once. The operations are then applied to SS in that order, and a final result comes out.

The operands are integers, but the operations run on rational numbers. Suppose the starting value is 5 and the cards are + 1, - 2, * 3, and / -2. In that order the final result is (5+12)×3/(2)=6(5 + 1 - 2) \times 3 / (-2) = -6. The cards apply in the chosen order and operator precedence is ignored. With the order - 2, / -2, + 1, * 3 the result is ((52)/(2)+1)×3=3/2((5 - 2) / (-2) + 1) \times 3 = -3/2, which is the largest value this set of cards can reach.

Given the starting value and the cards, find the maximum possible final value. Report it as an irreducible fraction with a positive denominator.

Input

The first line holds the number of test cases TT. Each test case starts with one line holding two integers SS and CC, the starting value and the number of cards. Then CC lines follow. The i-th of those lines describes one card and holds one character OiO_i, which is +, -, *, or /, and one integer ViV_i.

Limits

  • 1T1001 \le T \le 100
  • 1000S1000-1000 \le S \le 1000
  • OiO_i is one of +, -, *, /, for all i.
  • 1000Vi1000-1000 \le V_i \le 1000, for all i.
  • If OiO_i is /, then Vi0V_i \ne 0, for all i.
  • 1C10001 \le C \le 1000

Output

For each test case, print one line holding Case #x: y z, where xx is the test case number starting from 1, and yy and zz are integers such that y/zy/z is the maximum possible final value, yy and zz have no common divisor other than 1 and -1, and zz is strictly greater than 0.

Notes

The numerator can run far past the range of a 64-bit integer, so the answer needs arbitrary precision arithmetic.

Zero has exactly one valid form here, 0 1. The form 0 2 is invalid because it reduces, and 0 -1 is invalid because the denominator must be positive.