#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
using namespace std;
map<int, int> multiplyPolynomials(const vector<pair<int, int>>& poly1, const vector<pair<int, int>>& poly2) {
map<int, int> result;
for (const auto& term1 : poly1) {
int exp1 = term1.first;
int coeff1 = term1.second;
for (const auto& term2 : poly2) {
int exp2 = term2.first;
int coeff2 = term2.second;
int newExp = exp1 + exp2;
int newCoeff = coeff1 * coeff2;
result[newExp] += newCoeff;
}
}
return result;
}
string formatPolynomial(const map<int, int>& poly) {
stringstream ss;
bool firstTerm = true;
for (auto it = poly.rbegin(); it != poly.rend(); ++it) {
int exp = it->first;
int coeff = it->second;
if (coeff == 0) {
continue;
}
if (!firstTerm&&coeff > 0 ) {
ss<<'+';
}
firstTerm = false;
if (exp == 0) {
ss << coeff;
} else if (coeff == 1) {
ss << "x^" << exp;
} else if (coeff == -1) {
ss << "-x^" << exp;
} else {
ss << coeff << "x^" << exp;
}
}
return ss.str();
}
int main() {
freopen("mul.in","r",stdin);
freopen("mul.out","w",stdout);
vector<pair<int, int>> poly1, poly2;
int exponent, coefficient;
while(true){
cin >> exponent;
if (exponent == -1&&getchar()!=' ') break;
cin >> coefficient;
poly1.emplace_back(exponent, coefficient);
}
while (true) {
cin >> exponent;
if (exponent == -1&&getchar()!=' ') break;
cin >> coefficient;
poly2.emplace_back(exponent, coefficient);
}
map<int, int> result = multiplyPolynomials(poly1, poly2);
cout << formatPolynomial(result) << endl;
return 0;
}