#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long LL;
using namespace std;
char s[2005], t[2005], w[2005], ans[2005];
int lenans;
int a[2005], b[2005], d[2005], e[2005];
char Get() {
    auto c = getchar();
    while (c != '$' && (c < 'A' || c > 'Z'))
        c = getchar();
    return c;
}
void legal(char *c, int *l, int &len) {
    int newlen = 0;
    char newc[2005];
    int newl[2005];
    for (int i = 0; i < len; ++i) {
        if (l[i] == 0)
            continue;
        if (newlen && newc[newlen - 1] == c[i]) {
            newl[newlen - 1] += l[i];
        } else {
            newc[newlen] = c[i];
            newl[newlen++] = l[i];
        }
    }
    len = newlen;
    for (int i = 0; i < len; ++i) {
        c[i] = newc[i];
        l[i] = newl[i];
    }
}
int main() {
    int lena = 0;
    for (char c = Get(); c != '$'; c = Get()) {
        s[lena] = c;
        scanf("%d", &a[lena++]);
    }
    int lenb = 0;
    for (char c = Get(); c != '$'; c = Get()) {
        t[lenb] = c;
        scanf("%d", &b[lenb++]);
    }
    int lenc = 0;
    for (char c = Get(); c != '$'; c = Get()) {
        w[lenc] = c;
        scanf("%d", &d[lenc++]);
    }
    if (lenb == 1) {
        for (int i = 0; i < lena; ++i) {
            if (s[i] != t[0] || a[i] < b[0])
                continue;
            for (int j = 0; j < i; ++j) {
                ans[lenans] = s[j];
                e[lenans++] = a[j];
            }
            for (int j = 0; j < lenc; ++j) {
                ans[lenans] = w[j];
                e[lenans++] = d[j];
            }
            ans[lenans] = s[i];
            e[lenans++] = a[i] - b[0];
            for (int j = i + 1; j < lena; ++j) {
                ans[lenans] = s[j];
                e[lenans++] = a[j];
            }
            legal(ans, e, lenans);
            for (int i = 0; i < lenans; ++i) {
                printf("%c %d ", ans[i], e[i]);
            }
            printf("$\n");
            return 0;
        }
        for (int i = 0; i < lena; ++i) {
            printf("%c %d ", s[i], a[i]);
        }
        printf("$\n");
        return 0;
    }
    for (int i = 0; i < lena; ++i) {
        if (s[i] != t[0] || a[i] < b[0])
            continue;
        bool flag = true;
        for (int j = 1; j < lenb - 1; ++j) {
            if (i + j >= lena) {
                flag = false;
                break;
            }
            flag &= (s[i + j] == t[j] && a[i + j] == b[j]);
        }
        if (!flag)
            continue;
        if (i + lenb - 1 >= lena || s[i + lenb - 1] != t[lenb - 1] ||
            a[i + lenb - 1] < b[lenb - 1])
            continue;
        for (int j = 0; j < i; ++j) {
            ans[lenans] = s[j];
            e[lenans++] = a[j];
        }
        ans[lenans] = s[i];
        e[lenans++] = a[i] - b[0];
        for (int j = 0; j < lenc; ++j) {
            ans[lenans] = w[j];
            e[lenans++] = d[j];
        }
        ans[lenans] = s[i + lenb - 1];
        e[lenans++] = a[i + lenb - 1] - b[lenb - 1];
        for (int j = i + lenb; j < lena; ++j) {
            ans[lenans] = s[j];
            e[lenans++] = a[j];
        }
        legal(ans, e, lenans);
        for (int i = 0; i < lenans; ++i) {
            printf("%c %d ", ans[i], e[i]);
        }
        printf("$\n");
        return 0;
    }
    for (int i = 0; i < lena; ++i) {
        printf("%c %d ", s[i], a[i]);
    }
    printf("$\n");
    return 0;
}

Measured memory_bytes: 3224