### classes

class Polyn
  attr_accessor :n, :cs

  def initialize(cs0 = [0]) @cs = cs0.clone; @n = cs0.length - 1 end
  def ==(pl) @n == pl.n && @cs == pl.cs end
  def -@() Polyn.new(cs.map{|ci| -ci}) end
  
  def normalize!() 
    while @n > 0 && @cs[@n] == 0
      @cs.pop
      @n -= 1
    end
    return self if @cs == [0]
    
    g = @cs.inject{|g, ci| g = g.gcd(ci)}
    g = -g if g > 0 && @cs.last < 0
    if g != 1
      (0..@n).each{|i| @cs[i] /= g}
    end

    self
  end

  def +(pl)
    n0 = (@n >= pl.n) ? @n : pl.n
    cs0 = []
    for i in (0..n0)
      if @cs[i].nil?
        cs0 << pl.cs[i]
      elsif pl.cs[i].nil?
        cs0 << @cs[i]
      else
        cs0 << @cs[i] + pl.cs[i]
      end
    end
    Polyn.new(cs0)
  end

  def -(pl) self + (-pl) end

  def *(pl)
    cs0 = Array.new(@n + pl.n + 1){0}
    for i in (0..@n)
      for j in (0..pl.n)
        cs0[i + j] += pl.cs[j] * @cs[i]
      end
    end
    Polyn.new(cs0)
  end

  def %(pl)
    n0 = @n - pl.n
    return self if n0 < 0

    cs0 = @cs.map{|ci| Rational(ci)}
    cs1 = pl.cs.map{|ci| Rational(ci)}

    @n.downto(pl.n).each do |i|
      c = cs0[i] / cs1[pl.n]
      (0..pl.n).each{|j| cs0[i - j] -= c * cs1[pl.n - j]}
    end

    l = cs0.inject(1){|l, ci| l = l.lcm(ci.denominator)}
    cs2 = cs0.map{|ci| ci.numerator * l / ci.denominator}
    Polyn.new(cs2)
  end

  def to_s
    return "#{@cs[0]}" if @n == 0

    str = ''

    @n.downto(1).each do |k|
      next if @cs[k] == 0

      sign = (@cs[k] < 0) ? '-' : (k < @n) ? '+' : ''
      ca = @cs[k].abs
      d = (ca != 1) ? "#{ca}" : ''
      pow = (k > 1) ? "^#{k}" : ''
      str += (sign + d + 'x' + pow)
    end

    if @cs[0] != 0
      sign = (@cs[0] < 0) ? '-' : '+'
      ca = @cs[0].abs
      str += (sign + "#{ca}")
    end
    str
  end
end

### global variables

Zero = Polyn.new([0])
One = Polyn.new([1])
Minus = Polyn.new([-1])
X = Polyn.new([0, 1])

### subroutines

def num(str)
  n = 0
  while str[$pos] =~ /[0-9]/
    n = n * 10 + str[$pos].to_i
    $pos += 1
  end
  n
end

def primary(str)
  if str[$pos] == 'x'
    $pos += 1
    return X
  end

  if str[$pos] == '('
    $pos += 1
    expr0 = expr(str)
    $pos += 1
    return expr0
  end

  n = num(str)
  Polyn.new([n])
end

def factor(str)
  factor0 = primary(str)
  if str[$pos] == '^'
    $pos += 1
    k = num(str)
    pow = One
    while k > 0
      pow *= factor0 if (k & 1) == 1
      factor0 *= factor0
      k >>= 1
    end
    factor0 = pow
  end
  factor0
end

def term(str)
  term0 = factor(str)
  while str[$pos] && str[$pos] !~ /[\+\-\)]/
    factor1 = factor(str)
    term0 *= factor1
  end
  term0
end

def expr(str)
  asign = One
  if str[$pos] == '-'
    asign = Minus
    $pos += 1
  end

  expr0 = term(str) * asign
  while str[$pos] == '+' || str[$pos] == '-'
    sign = str[$pos]
    $pos += 1
    term1 = term(str)

    if sign == '+'
      expr0 += term1
    else
      expr0 -= term1
    end
  end
  expr0
end

def gcd(pl0, pl1)
  a, b = (pl0.n >= pl1.n) ? [pl0, pl1] : [pl1, pl0]

  loop do
    r = (a % b).normalize!
    break if r.cs == [0]
    a = b
    b = r
  end

  b
end

### main

loop do
  line = gets.strip
  break if line == '.'

  $pos = 0
  pl0 = expr(line).normalize!
  #p pl0.to_s

  $pos = 0
  pl1 = expr(gets.strip).normalize!
  #p pl1.to_s

  puts gcd(pl0, pl1).to_s
end