#include <bits/stdc++.h>
using namespace std;
const int N=300+5;
int n,m,q,c=0;
int a[N][N]={0},ans[N];
string s[N],name[N];
string s1,s2;
int gcd(int x,int y){
if (y==0)return x;
return gcd(y,x%y);
}
int abs(int x){
if (x>=0)return x;
return -x;
}
void gauss(){
int i=1,now=1;
for (;i<=c&&now<=n+m;i++,now++){
for (int j=i+1;j<=c;j++){
if (abs(a[j][now])>abs(a[i][now])){
for (int k=1;k<=n+m+1;k++)swap(a[i][k],a[j][k]);
}
}
if (a[i][now]==0){
i--;continue;
}
for (int j=1;j<=c;j++){
if (i==j||a[j][now]==0)continue;
int p=abs(a[i][now]*a[j][now])/gcd(abs(a[i][now]),abs(a[j][now]));
int x=p/abs(a[j][now]),y=p/abs(a[i][now]);
if (a[i][now]*a[j][now]<0)y=-y;
for (int k=1;k<=n+m+1;k++){
a[j][k]=a[j][k]*x-a[i][k]*y;
}
}
}
int y=1;
for (int i=1;i<=n+m;i++){
int p=gcd(a[i][i],a[i][n+m+1]);
a[i][i]/=p;a[i][n+m+1]/=p;
y=y*a[i][i]/gcd(y,a[i][i]);
}
for (int i=1;i<=n+m;i++){
ans[i]=a[i][n+m+1]*y/a[i][i];
}
return ;
}
int main(){
freopen ("Sbfenjielu.in","r",stdin);
freopen ("Sbfenjielu.out","w",stdout);
cin>>n>>m;
for (int i=1;i<=n;i++){
cin>>s[i];
int sl=s[i].length();
for (int j=0;j<sl;){
int t=0;string p;
p+=s[i][j];j++;
while('a'<=s[i][j]&&s[i][j]<='z'&&j<sl){
p+=s[i][j];
j++;
}
int num=0;
while('1'<=s[i][j]&&s[i][j]<='9'&&j<sl){
num*=10;num+=s[i][j]-'0';
j++;
}
if (num==0)num=1;
for (int k=1;k<=c;k++){
if (name[k]==p){
a[k][i]+=num;t=-1;break;
}
}
if (t!=-1){
name[++c]=p;a[c][i]+=num;
}
}
}
for (int i=1;i<=m;i++){
cin>>s[i+n];
int sl=s[i+n].length();
for (int j=0;j<sl;){
int t=0;string p;
p+=s[i+n][j];j++;
while('a'<=s[i+n][j]&&s[i+n][j]<='z'&&j<sl){
p+=s[i+n][j];
j++;
}
int num=0;
while('1'<=s[i+n][j]&&s[i+n][j]<='9'&&j<sl){
num*=10;num+=s[i+n][j]-'0';
j++;
}
if (num==0)num=1;
for (int k=1;k<=c;k++){
if (name[k]==p){
a[k][n+i]+=-num;t=-1;break;
}
}
if (t!=-1){
name[++c]=p;a[c][n+i]+=-num;
}
}
}
a[++c][1]=1;a[c][n+m+1]=1;
gauss();
for (int i=1;i<=n;i++){
if (ans[i]!=1)cout<<ans[i];
cout<<s[i];
if (i!=n)cout<<"+";
}
cout<<"=";
for (int i=1;i<=m;i++){
if (ans[i+n]!=1)cout<<ans[i+n];
cout<<s[i+n];
if (i!=m)cout<<"+";
}
return 0;
}