#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    using P = pair<char,ll>;
    vector<P> a, b, c;
    while(1) {
        char c;
        cin >> c;
        if(c == '$') break;
        ll l;
        cin >> l;
        a.emplace_back(c, l);
    }
    while(1) {
        char c;
        cin >> c;
        if(c == '$') break;
        ll l;
        cin >> l;
        b.emplace_back(c, l);
    }
    while(1) {
        char cc;
        cin >> cc;
        if(cc == '$') break;
        ll l;
        cin >> l;
        c.emplace_back(cc, l);
    }

    vector<P> ans;
    ll n = a.size(), idx = 0;
    REP(i, n) {
        if(a[i].first == b[idx].first && a[i].second >= b[idx].second) {
            if(b.size() == 1) {
                REP(j, n) {
                    if(j != i) ans.push_back(a[j]);
                    else {
                        for(auto k: c) ans.push_back(k);
                        if(a[i].second-b[0].second>0) {
                            ans.push_back({a[i].first, a[i].second-b[0].second});
                        }
                    }
                }
                break;
            }

            if(0 < idx && idx < (int)b.size()-1 && a[i].second != b[idx].second) {
                // ng
                idx = 0;
            } else {
                idx++;
                if(idx == (ll)b.size()) {
                    // [i-b.size()+1, i] の部分を消して c に置換する
                    // dump(i-b.size(), i);
                    vector<bool> del(a.size());
                    REP(j, b.size()) {
                        a[j+i-b.size()+1].second -= b[j].second;
                        if(a[j+i-b.size()+1].second == 0) {
                            del[j+i-b.size()+1] = true;
                        }
                    }
                    // REP(i, 10) cerr << del[i] << " ";
                    // cerr << endl;
                    bool first = true;
                    REP(j, a.size()) {
                        if((del[j] || j>i-(ll)b.size()+1) && first) {
                            for(auto k: c) ans.push_back(k);
                            first = false;
                        }
                        if(!del[j]) ans.push_back(a[j]);
                    }
                    break;
                }
            }
        } else {
            // ng
            idx = 0;
        }
    }

    if(ans.size() == 0) ans = a;
    // REP(i, 10) cerr << ans[i] << " ";
    // cerr << endl;

    vector<ll> head(ans.size());
    head[0] = 0;
    FOR(i, 1, ans.size()) {
        if(ans[i].first == ans[i-1].first) {
            head[i] = head[i-1];
            ans[head[i]].second += ans[i].second;
            ans[i].second = 0;
        } else {
            head[i] = i;
        }
    }

    vector<P> nans;
    for(auto i: ans) if(i.second != 0) nans.push_back(i);
    REP(i, nans.size()) cout << nans[i].first << " " << nans[i].second << " ";
    cout << "$" << endl;

    return 0;
}


Measured memory_bytes: 3352