#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define MAX 10

struct Info{

	ll table[MAX];
};

char buf[1005];
Info expression(int left,int right);

ll gcd(ll x,ll y){
	if(x < y){
		swap(x,y);
	}
	if(y == 0){
		return x;
	}else{
		return gcd(y,x%y);
	}
}

void init(Info& info){

	for(int i = 0; i < MAX; i++){

		info.table[i] = 0;
	}
}

int get_degree(Info A){

	for(int i = MAX-1; i >= 0; i--){
		if(A.table[i] != 0){

			return i;
		}
	}
	return -1;
}

Info calc_plus(Info A,Info B){

	Info ret;
	init(ret);

	for(int i = 0; i < MAX; i++){

		ret.table[i] = A.table[i]+B.table[i];
	}

	return ret;
}

Info calc_minus(Info A,Info B){

	Info ret;
	init(ret);

	for(int i = 0; i < MAX; i++){

		ret.table[i] = A.table[i]-B.table[i];
	}

	return ret;
}

Info mult(Info A,Info B){

	Info ret;
	init(ret);

	int deg_A = get_degree(A);
	int deg_B = get_degree(B);

	for(int i = 0; i <= deg_A; i++){
		for(int k = 0; k <= deg_B; k++){

			ret.table[i+k] += A.table[i]*B.table[k];
		}
	}

	return ret;
}

Info power(Info A,int num){

	Info ret = A;

	for(int i = 0; i < num-1; i++){

		ret = mult(ret,A);
	}
	return ret;
}

Info neg(Info A){

	Info ret;
	init(ret);

	for(int i = 0; i < MAX; i++){

		ret.table[i] = A.table[i]*(-1);
	}

	return ret;
}

Info primary(int left,int right){

	if(buf[left] == '('){

		return expression(left+1,right-1);

	}else{

		Info ret;
		init(ret);

		if(buf[left] == 'x'){

			ret.table[1] = 1;

		}else{

			bool neg_FLG = false;
			int start = left;

			if(buf[left] == '-'){

				neg_FLG = true;
				start = left+1;

			}

			for(int i = start; i <= right; i++){

				ret.table[0] = 10*ret.table[0]+(buf[i]-'0');
			}

			if(neg_FLG){

				ret.table[0] *= -1;
			}
		}

		return ret;
	}
}

Info factor(int left,int right){

	int depth = 0,loc = -1;

	//深さ0の^を探す
	for(int i = left; i <= right; i++){

		if(buf[i] == '('){

			depth++;
		}else if(buf[i] == ')'){

			depth--;

		}else if(depth == 0 && buf[i] == '^'){

			loc = i;
			break;
		}
	}

	if(loc == -1){

		return primary(left,right);
	}

	Info A = primary(left,loc-1);
	Info num = primary(loc+1,right);

	return power(A,num.table[0]);
}

Info term(int left,int right){

	int loc = left,next;
	bool neg_FLG = false;

	if(buf[left] == '-'){
		neg_FLG = true;
		loc++;
	}


	Info ret,tmp;
	init(ret);

	bool is_First = true;

	while(loc <= right){

		if(buf[loc] >= '0' && buf[loc] <= '9'){

			for(next = loc;next <= right && buf[next] >= '0' && buf[next] <= '9'; next++);

			tmp = factor(loc,next-1);

			if(buf[next] == '^'){

				int start = ++next;

				for(; next <= right && buf[next] >= '0' && buf[next] <= '9'; next++);

				Info tmp2 = factor(start,next-1);
				tmp = power(tmp,tmp2.table[0]);

			}

			if(is_First){

				ret = tmp;
				is_First = false;

			}else{

				ret = mult(ret,tmp);
			}

			loc = next;

		}else if(buf[loc] == '('){

			int depth = 0,close_pos;

			for(next = loc; next <= right; next++){

				if(buf[next] == '('){

					depth++;
				}else if(buf[next] == ')'){

					depth--;
					if(depth == 0){
						close_pos = next;
						break;
					}
				}
			}

			tmp = expression(loc+1,close_pos-1);

			if(buf[close_pos+1] == '^'){

				for(next = close_pos+2; next <= right && buf[next] >= '0' && buf[next] <= '9'; next++);

				Info tmp2 = factor(close_pos+2,next-1);

				tmp = power(tmp,tmp2.table[0]);

				if(is_First){

					ret = tmp;
					is_First = false;

				}else{

					ret = mult(ret,tmp);
				}

				loc = next;

			}else{

				if(is_First){

					ret = tmp;
					is_First = false;

				}else{

					ret = mult(ret,tmp);
				}
				loc = close_pos+1;
			}

		}else{ //buf[loc] == 'x'

			tmp = factor(loc,loc);

			if(buf[loc+1] == '^'){

				for(next = loc+2; next <= right && buf[next] >= '0' && buf[next] <= '9'; next++);

				Info tmp2 = factor(loc+2,next-1);
				tmp = power(tmp,tmp2.table[0]);

				if(is_First){

					ret = tmp;
					is_First = false;

				}else{

					ret = mult(ret,tmp);
				}
				loc = next;

			}else{

				if(is_First){

					ret = tmp;
					is_First = false;

				}else{

					ret = mult(ret,tmp);
				}

				loc++;
			}
		}
	}

	if(neg_FLG){

		ret = neg(ret);
	}

	return ret;
}

Info expression(int left,int right){

	if(buf[left] == '-' && buf[left+1] == '('){

		return neg(expression(left+1,right));
	}

	int depth = 0;
	queue<int> Q;

	//深さ0の+または-を探す
	for(int i = left; i <= right; i++){

		if(buf[i] == '('){

			depth++;
		}else if(buf[i] == ')'){

			depth--;

		}else if(depth == 0 && i != left && (buf[i] == '+' || buf[i] == '-')){

			Q.push(i);
		}
	}

	if(Q.size() == 0){

		return term(left,right);
	}

	Info ret = term(left,Q.front()-1);

	while(true){

		char op = buf[Q.front()];
		int pre_loc = Q.front();
		Q.pop();

		Info tmp;

		if(Q.empty()){

			tmp = term(pre_loc+1,right);

			if(op == '+'){

				ret = calc_plus(ret,tmp);

			}else{

				ret = calc_minus(ret,tmp);
			}
			break;

		}else{

			tmp = term(pre_loc+1,Q.front()-1);

			if(op == '+'){

				ret = calc_plus(ret,tmp);

			}else{

				ret = calc_minus(ret,tmp);
			}
		}
	}

	return ret;
}

void func(){

	int length;
	for(length = 0; buf[length] != '\0'; length++);

	Info info_A = expression(0,length-1);

	int deg_A = get_degree(info_A);

	scanf("%s",buf);

	for(length = 0; buf[length] != '\0'; length++);

	Info info_B = expression(0,length-1);

	int deg_B = get_degree(info_B);

	if(deg_A < deg_B){

		swap(info_A,info_B);
		swap(deg_A,deg_B);
	}

	while(true){

		deg_B = get_degree(info_B);
		if(deg_B == -1)break;

		deg_A = get_degree(info_A);

		for(int i = deg_A; i >= deg_B; i--){

			ll common = gcd(abs(info_A.table[i]),abs(info_B.table[deg_B]));
			ll tmp = info_B.table[deg_B]/common;

			for(int k = 0; k < MAX; k++){

				info_A.table[k] *= tmp;
			}

			tmp = info_A.table[i]/info_B.table[deg_B];

			for(int k = 0; k <= deg_B; k++){

				info_A.table[i-k] -= info_B.table[deg_B-k]*tmp;
			}

			tmp = 0;
			for(int k = 0; k < MAX; k++){

				tmp = gcd(abs(tmp),abs(info_A.table[k]));
			}

			if(tmp != 0){

				for(int k = 0; k < MAX; k++){

					info_A.table[k] /= tmp;
				}
			}
		}
		swap(info_A,info_B);
	}

	//return;

	deg_A = get_degree(info_A);
	if(info_A.table[deg_A] < 0){

		info_A = neg(info_A);
	}

	ll tmp = 0;
	for(int i = 0; i < MAX; i++){

		tmp = gcd(abs(tmp),abs(info_A.table[i]));
	}
	if(tmp != 0){

		for(int i = 0; i < MAX; i++){

			info_A.table[i] /= tmp;
		}
	}

	bool is_First = true;

	for(int i = MAX-1; i >= 0; i--){

		if(info_A.table[i] == 0)continue;

		if(info_A.table[i] > 0){

			if(is_First){

				is_First = false;

			}else{

				printf("+");
			}

			if(info_A.table[i] != 1){
				printf("%lld",info_A.table[i]);
			}

		}else{ //info_A.table[i] > 0

			if(info_A.table[i] == -1){

				printf("-");

			}else{

				printf("%lld",info_A.table[i]);
			}
		}

		if(i >= 2){

			printf("x^%d",i);

		}else if(i == 1){

			printf("x");

		}else{ //i == 0

			if(abs(info_A.table[i]) == 1){

				printf("1");
			}
		}
	}
	printf("\n");
}

int main(){

	while(true){

		scanf("%s",buf);
		if(buf[0] == '.')break;

		func();
	}

	return 0;
}



Measured memory_bytes: 3240