Under construction

Still being built. Things may change

Fun with fractions

Time limit1sMemory limit128 MB

Summary
Add up to 999 mixed numbers, integers, or fractions per test case and print each sum in lowest terms as a mixed number.
Level

Easy3 of 10

Topics
Math, Implementation, String
Solved
No attempts yet

Problem

A rational number is the ratio of two integers. Given a numerator nn and a denominator dd, we write the fraction as n/dn/d. A rational number has more than one representation. For example, 1/21/2 and 2/42/4 have the same value. A representation n/dn/d with gcd⁡(∣n∣,∣d∣)=1\gcd(|n|, |d|) = 1 is in lowest terms. So 1/21/2 is in lowest terms and 2/42/4 is not.

Addition of rational numbers is defined below. The right side is not guaranteed to be in lowest terms.

n1d1+n2d2=n1d2+n2d1d1d2\frac{n_1}{d_1} + \frac{n_2}{d_2} = \frac{n_1 d_2 + n_2 d_1}{d_1 d_2}

A rational number whose numerator is at least as large as its denominator can be written as a mixed number, which has an integer part and a fractional part. For example, 5135\frac{1}{3} is the mixed number that equals 16/316/3.

Read a sequence of rational numbers and print their sum.

Input

The input holds several tests. The first line of each test has an integer nn (1≤n<10001 \le n < 1000). A line holding n=0n = 0 ends the input.

The next nn lines each hold one string with no spaces. Each string is one rational number in one of the three forms below, and it may not be in lowest terms. ww, nn, and dd are integers with 0≤w,n<10000 \le w, n < 1000 and 1≤d<10001 \le d < 1000.

  • w,n/d: a mixed number equal to (w×d+n)/d(w \times d + n) / d
  • n/d: a rational number whose integer part is 0
  • w: an integer whose fractional part is 0

Output

For each test print one line that begins with Test x: , followed by the sum in lowest terms. Here xx is the test number counting from 1.

If the sum has integer part ww and fractional part n/dn/d, print it as w,n/d when both parts are present, as w when only the integer part is present, and as n/d when only the fractional part is present. If neither part is present, print 0.

Examples1

  1. Example 1

    Input
    2
    1/2
    1/3
    3
    1/3
    2/6
    3/9
    3
    1
    2/3
    4,5/6
    0
    
    Expected output
    Test 1: 5/6
    Test 2: 1
    Test 3: 6,1/2