#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=203,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    vector<int> A,B,C,D,E;
    vector<char> a,b,c,d,e;
    
    while(1){
        char x;cin>>x;
        if(x!='$'){
            a.push_back(x);
            int y;cin>>y;
            A.push_back(y);
        }else{
            break;
        }
    }
    
    while(1){
        char x;cin>>x;
        if(x!='$'){
            b.push_back(x);
            int y;cin>>y;
            B.push_back(y);
        }else{
            break;
        }
    }
    
    while(1){
        char x;cin>>x;
        if(x!='$'){
            c.push_back(x);
            int y;cin>>y;
            C.push_back(y);
        }else{
            break;
        }
    }
    
    int i=0,j=0;
    
    while(i<A.size()&&j<B.size()){
        if(a[i]==b[j]&&A[i]>=B[j]){
            int ii=i+1,jj=j+1;
            bool ok=false;
            if(jj==B.size()){
                for(int k=0;k<i;k++){
                    d.push_back(a[k]);
                    D.push_back(A[k]);
                }
                for(int k=0;k<C.size();k++){
                    d.push_back(c[k]);
                    D.push_back(C[k]);
                }
                if(A[i]!=B[j]){
                    d.push_back(a[i]);
                    D.push_back(A[i]-B[j]);
                }
                for(int k=ii;k<A.size();k++){
                    d.push_back(a[k]);
                    D.push_back(A[k]);
                }
                break;
            }
            
            while(ii<A.size()&&jj<B.size()){
                if(jj==B.size()-1){
                    if(a[ii]==b[jj]&&A[ii]>=B[jj]){
                        ok=true;
                    }
                    break;
                }else{
                    if(a[ii]==b[jj]&&A[ii]==B[jj]){
                        ii++;
                        jj++;
                    }else{
                        break;
                    }
                }
            }
            
            if(ok){
                for(int k=0;k<i;k++){
                    d.push_back(a[k]);
                    D.push_back(A[k]);
                }
                if(A[i]!=B[j]){
                    d.push_back(a[i]);
                    D.push_back(A[i]-B[j]);
                }
                for(int k=0;k<C.size();k++){
                    d.push_back(c[k]);
                    D.push_back(C[k]);
                }
                if(A[ii]!=B[jj]){
                    d.push_back(a[ii]);
                    D.push_back(A[ii]-B[jj]);
                }
                for(int k=ii+1;k<A.size();k++){
                    d.push_back(a[k]);
                    D.push_back(A[k]);
                }
                
                break;
            }else i++;
        }else i++;
    }
    
    //for(int i=0;i<D.size();i++) cout<<d[i]<<" "<<D[i]<<endl;
    
    if(D.size()==0){
        for(int i=0;i<A.size();i++){
            cout<<a[i]<<" "<<A[i]<<" ";
        }
        cout<<"$\n";
    }else{
        for(int i=0;i<D.size();i++){
            if(e.size()&&E.size()&&e.back()==d[i]){
                E.back()+=D[i];
            }else{
                e.push_back(d[i]);
                E.push_back(D[i]);
            }
        }
        
        for(int i=0;i<E.size();i++){
            cout<<e[i]<<" "<<E[i]<<" ";
        }
        cout<<"$\n";
    }
    
}




Measured memory_bytes: 3268