Under construction

Still being built. Things may change

Dihedral Group Product

Time limit5sMemory limit128 MB

Summary
Multiply two given elements of D_{m,n} with the relation ba = a^{m-1}b and print each product with exponents reduced modulo m and n.
Level

Easy3 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

A dihedral group is a set DD with a multiplication ∗* defined on it. Every element of DD is built from two elements aa and bb. The multiplication sign is almost never written out: the factors are written next to each other or collapsed into exponents. So a∗aa*a is written aaaa or a2a^2, and a∗a∗ba*a*b is written a2b1a^2b^1 or a2ba^2b.

A dihedral group DD takes two parameters mm and nn with m≥2m \ge 2 and n≥2n \ge 2, and is written Dm,nD_{m,n}. Three relations hold in Dm,nD_{m,n}.

am=a0=1,bn=b0=1,ba=am−1ba^m = a^0 = 1, \quad b^n = b^0 = 1, \quad ba = a^{m-1}b

Dm,nD_{m,n} therefore has exactly mnmn elements.

Dm,n={ajbk∣0≤j<m, 0≤k<n}={a0b0,a1b0,…,am−1b0,…,am−1bn−1}D_{m,n} = \{a^jb^k \mid 0 \le j < m,\ 0 \le k < n\} = \{a^0b^0, a^1b^0, \dots, a^{m-1}b^0, \dots, a^{m-1}b^{n-1}\}

For m=7m = 7 and n=2n = 2, D7,2D_{7,2} consists of these 14 elements.

{a0b0,a1b0,a2b0,a3b0,a4b0,a5b0,a6b0,a0b1,a1b1,a2b1,a3b1,a4b1,a5b1,a6b1}\{a^0b^0, a^1b^0, a^2b^0, a^3b^0, a^4b^0, a^5b^0, a^6b^0, a^0b^1, a^1b^1, a^2b^1, a^3b^1, a^4b^1, a^5b^1, a^6b^1\}

The multiplication is not commutative, so ba≠abba \ne ab. In D7,2D_{7,2} the third relation reads ba=a6bba = a^6b. Powers of a single letter combine as ajak=a(j+k) mod ma^ja^k = a^{(j+k) \bmod m} and bjbk=b(j+k) mod nb^jb^k = b^{(j+k) \bmod n}, but once the two letters mix you cannot simply add the exponents.

(ajbk)(apbq)≠a(j+p) mod mb(k+q) mod n(a^jb^k)(a^pb^q) \ne a^{(j+p) \bmod m}b^{(k+q) \bmod n}

Here is the correct way to multiply a3b1a^3b^1 by a2b1a^2b^1 in D7,2D_{7,2}. First rewrite the baba in the middle as a6ba^6b.

(a3b1)(a2b1)=(a3b0)(ba)(a1b1)=(a3b0)(a6b1)(a1b1)(a^3b^1)(a^2b^1) = (a^3b^0)(ba)(a^1b^1) = (a^3b^0)(a^6b^1)(a^1b^1)

Since a3a6=a(3+6) mod 7=a2a^3a^6 = a^{(3+6) \bmod 7} = a^2, the first two factors collapse into a2b1a^2b^1.

(a3b0)(a6b1)(a1b1)=(a2b1)(a1b1)(a^3b^0)(a^6b^1)(a^1b^1) = (a^2b^1)(a^1b^1)

Clean up the remaining product the same way.

(a2b1)(a1b1)=(a2b0)(ba)(a0b1)=(a2b0)(a6b1)(a0b1)=a8b2=a1b0(a^2b^1)(a^1b^1) = (a^2b^0)(ba)(a^0b^1) = (a^2b^0)(a^6b^1)(a^0b^1) = a^8b^2 = a^1b^0

So (a3b1)(a2b1)=a1b0(a^3b^1)(a^2b^1) = a^1b^0.

Given mm, nn, and two elements ajbka^jb^k and apbqa^pb^q of Dm,nD_{m,n}, write a program that computes their product.

Input

The input consists of several problem sets.

The first line of each problem set contains a problem ID and mm, nn, pp, separated by spaces. The problem ID is a string of uppercase letters and digits with no spaces, mm and nn are integers between 22 and 10001000, and pp is the number of multiplication questions in that set. Each of the next pp lines holds one question, two elements of Dm,nD_{m,n} separated by a space. An element is written as the letter a, an exponent, the letter b, and an exponent run together, like a3b1, and the exponents carry no leading zeros.

One problem set is followed immediately by the next. A line whose problem ID is ZZ marks the end of the input, and the rest of that line is ignored.

Output

For each multiplication question, print one line in this format.

ProblemID id: ajbk * apbq = arbs

id is the problem ID of that set, ajbk and apbq are the two input elements copied exactly as they were given, and arbs is their product. Write the result exponents so that 0≤r<m0 \le r < m and 0≤s<n0 \le s < n, and put a single space after the colon and on both sides of * and =.

Examples1

  1. Example 1

    Input
    C1 7 2 3
    a3b1 a2b1
    a5b1 a3b1
    a2b1 a4b1
    C2 9 3 2
    a3b1 a2b1
    a4b1 a3b1
    ZZ 0 0 0
    
    Expected output
    ProblemID C1: a3b1 * a2b1 = a1b0
    ProblemID C1: a5b1 * a3b1 = a2b0
    ProblemID C1: a2b1 * a4b1 = a5b0
    ProblemID C2: a3b1 * a2b1 = a1b2
    ProblemID C2: a4b1 * a3b1 = a1b2