Common Polynomial

Time limit1sMemory limit128 MB

Summary
Parse two polynomial expressions with parentheses and exponents, expand them, then compute and print the normalized greatest common divisor polynomial.
Level

Medium7 of 10

Topics
Math, Recursion, Implementation
Solved
No attempts yet

Problem

Among polynomials in a single variable xx, one whose coefficients are all integers is called an integer polynomial.

Given two integer polynomials AA and BB, an integer polynomial CC is called a common divisor of AA and BB if there exist integer polynomials XX and YY such that A=C⋅XA = C \cdot X and B=C⋅YB = C \cdot Y.

The greatest common divisor of the two polynomials is the common divisor of the highest degree. Ignoring a constant multiple, the greatest common divisor is unique: if both CC and DD are greatest common divisors of AA and BB, then there exist non-negative integers pp and qq with p⋅C=q⋅Dp \cdot C = q \cdot D.

Given two integer polynomials AA and BB, write a program that computes their greatest common divisor.

Input

The first line contains the number of test cases TT. Each test case consists of two lines, each containing one polynomial. A polynomial is written according to the following rules.

  1. A primary term is the variable x, a constant (digits 0~9), or an expression enclosed in parentheses. Examples: x, 99, (x+1)
  2. A factor is a primary term followed by ^ and an exponent (digits). Examples: x^05, 1^15, (x+1)^3
  3. A term is a product of one or more factors. Examples: 4x, (x+1)(x-2), 3(x+1)^2
  4. A polynomial is one or more terms joined by + or -. The first term may begin with -. Examples: -x+1, 3(x+1)^2-x(x-1)^2

When several digits are written together, they form a single constant. That is, 99 means 9999, not 9×99 \times 9.

When every input polynomial is fully expanded, each coefficient is at most 100100 and the degree of xx is at most 1010. Every exponent written after ^ is a non-negative integer. Only data that can be computed with 32-bit integers is given, so a correct computation does not overflow.

Output

For each test case, print the greatest common divisor of the two polynomials in the following format.

c0x^p0±c1x^p1±...±cnx^pn

Each cic_i is a positive integer, each pip_i is a non-negative integer, and p0>p1>⋯>pnp_0 > p_1 > \dots > p_n. The greatest common divisor of the coefficients, gcd⁡(c0,…,cn)\gcd(c_0, \dots, c_n), is 11. Each ± between terms is + or - according to the sign of that term's coefficient, and the first term c0xp0c_0 x^{p_0} is written without a leading sign.

In addition, follow these rules.

  1. If cic_i is 11 and pip_i is not 00, omit cic_i.
  2. Omit x0x^0.
  3. Write x1x^1 as x.

Examples7

  1. Example 1

    Input
    3
    -(x^3-3x^2+3x-1)
    (x-1)^2
    x^2+10x+25
    x^2+6x+5
    x^3+1
    x-1
    
    Expected output
    x^2-2x+1
    x+5
    1
    
  2. Example 2

    Input
    1
    x^2-1
    x-1
    
    Expected output
    x-1
    
  3. Example 3

    Input
    1
    x
    x+1
    
    Expected output
    1
    
  4. Example 4

    Input
    1
    2x+2
    4x+4
    
    Expected output
    x+1
    
  5. Example 5

    Input
    1
    -x^2+1
    x-1
    
    Expected output
    x-1
    
  6. Example 6

    Input
    1
    (x-1)^2(x+2)
    (x-1)(x+2)^2
    
    Expected output
    x^2+x-2
    
  7. Example 7

    Input
    1
    x^05
    x^02
    
    Expected output
    x^2