#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
using namespace std;
bool isDigit(char c) { return c >= '0' && c <= '9';}
bool isUpperCaseLetter(char c) { return c >= 'A' && c <= 'Z'; }
bool isLowerCaseLetter(char c) { return c >= 'a' && c <= 'z'; }
void update(map<string, int> &map, string key, int value)
{
if (map.count(key))
{
map.at(key) = map.at(key) + value;
}
else
{
map.insert(make_pair(key, value));
}
}
void split(string formula, vector<string> &storage)
{
int begin = 0, length = 0;
for (int i = 0; i < formula.length(); i++)
{
if (formula.at(i) == '+')
{
storage.push_back(formula.substr(begin, length));
length = 1;
begin = ++i;
}
else
{
length++;
}
}
storage.push_back(formula.substr(begin, length));
}
int getMultiplier(string formula)
{
int res = 0;
for (int i = 0; i < formula.length(); i++)
{
if (isDigit(formula.at(i)))
{
res *= 10;
res += (formula.at(i) - '0');
}
else break;
}
return res == 0 ? 1 : res;
}
void getContent(string formula, int multiplier, map<string, int> &content)
{
if (formula.find('(') != string::npos)
{
int begin = formula.find('(') + 1;
int end;
int bracketCounter = 1;
for (int i = begin; true; i++)
{
if (formula.at(i) == '(')
{
bracketCounter ++;
}
else if (formula.at(i) == ')')
{
bracketCounter--;
if (bracketCounter == 0)
{
end = i;
break;
}
}
}
int newMultiplier = multiplier;
int afterEnd = end + 1;
int backCoefficient = 0;
for (int i = 1; true; i++)
{
if (end + i != formula.length() && isDigit(formula.at(end + i)))
{
backCoefficient *= 10;
backCoefficient += (formula.at(end + i) - '0');
afterEnd++;
}
else
{
if (backCoefficient == 0)
{
backCoefficient++;
}
newMultiplier *= backCoefficient;
break;
}
}
if (begin != 1)
{
getContent(formula.substr(0, begin - 1), multiplier, content);
}
getContent(formula.substr(begin, end - begin), newMultiplier, content);
if (afterEnd < formula.length())
{
getContent(formula.substr(afterEnd), multiplier, content);
}
}
else
{
string name = "";
string coefficient = "";
for (char c : formula)
{
if (isDigit(c))
{
coefficient += c;
}
else if (isLowerCaseLetter(c))
{
name += c;
}
else if (isUpperCaseLetter(c))
{
if (name.empty())
{
name += c;
}
else
{
if (coefficient.empty())
{
coefficient = "1";
}
update(content, name, multiplier * stoi(coefficient));
name.clear();
name += c;
coefficient = "";
}
}
}
if (!name.empty())
{
if (coefficient.empty())
{
coefficient = "1";
}
update(content, name, multiplier * stoi(coefficient));
}
}
}
void process(vector<string> subformulas, map<string, int> &content)
{
for (string subformula : subformulas)
{
int multiplier = getMultiplier(subformula);
if (multiplier != 1)
{
getContent(subformula.substr(floor(log10(multiplier)+1)), multiplier, content);
}
else
{
getContent(subformula, multiplier, content);
}
}
}
int main()
{
string mainFormula;
vector<string> mainSubformulas;
map<string, int> mainContent;
cin >> mainFormula;
split(mainFormula, mainSubformulas);
process(mainSubformulas, mainContent);
int n;
cin >> n;
string formulaToCompare;
vector<string> subformulasToCompare;
map<string, int> contentToCompare;
for (int i = 0; i < n; i++)
{
subformulasToCompare.clear();
contentToCompare.clear();
cin >> formulaToCompare;
split(formulaToCompare, subformulasToCompare);
process(subformulasToCompare, contentToCompare);
cout << mainFormula << (mainContent == contentToCompare ? "==" : "!=") << formulaToCompare << endl;
}
return 0;
}