A simple continued fraction representation of a real number r is built by splitting r into its integer part and the reciprocal of what is left, then splitting that reciprocal the same way, and so on. The representation has the form
r=a0+a1+a2+a3+⋯111
where every ai is an integer and a1,a2,… are positive. The ai are called partial quotients. For 5.4 the partial quotients are a0=5, a1=2 and a2=2.
An irrational number such as 2 needs infinitely many partial quotients. A rational number has finitely many, and the list is unique once you require the last partial quotient to differ from 1. Two rational numbers are given as continued fractions. Compute their sum, difference, product and quotient, and write each result as a continued fraction.
The input holds several test cases. Each test case takes three lines. The first line has two integers n1 and n2 (1≤ni≤9), the number of partial quotients of the rational numbers r1 and r2. The second line has the n1 partial quotients of r1 and the third line has the n2 partial quotients of r2. Every partial quotient satisfies ∣a0∣≤10 and 0<ai≤10 for i≥1, the last partial quotient of each number is never 1, and r2 is not 0. A line holding two zeros ends the input.
For each test case print a line reading Case k:, where k is the number of the test case counting from 1. Then print four lines holding the continued fraction representations of r1+r2, r1−r2, r1×r2 and r1/r2, in that order. Write the partial quotients of one number on a single line separated by single spaces, and never let the last partial quotient be 1. Every value that appears during the computation fits in a signed 64-bit integer.