#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

void input_rle(vector<string> &S, vector<int> &I)
{
  string c; int l;
  while(cin >> c, c != "$"){
    S.push_back(c);
    cin >> l;
    I.push_back(l);
  }
}

int find_rle(vector<string> &A1, vector<int>&A2, vector<string>&B1, vector<int>&B2)
{
  int ret = -1;
  if(A1.size() < B1.size()) return -1;
  if(B1.size() == 1){
    rep(i, A1.size()){
      if(A1[i] == B1[0] && A2[i] >= B2[0]) return i;
    }
  }else if(B1.size() == 2){
    rep(i, A1.size()-1){
      if(A1[i] == B1[0] && A1[i+1] == B1[1] && A2[i] >= B2[0] && A2[i+1] >= B2[1]) return i;
    }
  }else{
    rep(i, A1.size()-B1.size()+1){
      if(!(A1[i] == B1[0] && A2[i] >= B2[0]
	   && A1[i+B1.size()-1] == B1[B1.size()-1] && A2[i+B1.size()-1] >= B2[B2.size()-1])) continue;
      for(int j=1;j<B1.size()-1;j++){
	if(!(A1[i+j] == B1[j] && A2[i+j] == B2[j])) break;
	if(j == B2.size()-2) return i;
      }
    }
  }
  return ret;
}

void merge(vector<string> &A1, vector<int>&A2)
{
  deque<string>D1; 
  deque<int>D2;    
  int i = 0;
  while(A2[i] == 0) i++;
  D1.push_back(A1[i]);  
  D2.push_back(A2[i]);
  ++i;
  for(;i<A1.size();i++){
    string d1 = D1.back(); 
    int  d2  = D2.back();
    if(A2[i] == 0) continue;
    if(d1 == A1[i]){
      D2.pop_back(); d2 += A2[i];
      D2.push_back(d2);
    }else{
      D1.push_back(A1[i]);
      D2.push_back(A2[i]);
    }
  }

  A1.clear();
  A1.clear();
  A1.insert(A1.begin(), D1.begin(), D1.end());
  A2.insert(A2.begin(), D2.begin(), D2.end());
}

void replace_rle(vector<string> &A1, vector<int>&A2, vector<string>&B1, vector<int>&B2,
	      vector<string> &C1, vector<int>&C2, int pos)
{
  if(B1.size() == 1){
    A2[pos] -= B2[0];
    A1.insert(A1.begin() + pos, C1.begin(), C1.end());
    A2.insert(A2.begin() + pos, C2.begin(), C2.end());
  }else{
    A2[pos] -= B2[0];
    A2[pos+B2.size()-1] -= B2[B2.size()-1];
    A1.erase(A1.begin() + pos+1, A1.begin() + pos + B1.size()-1);
    A2.erase(A2.begin() + pos+1, A2.begin() + pos + B2.size()-1);
    A1.insert(A1.begin() + pos+1, C1.begin(), C1.end());
    A2.insert(A2.begin() + pos+1, C2.begin(), C2.end());    
  }
}

int main()
{
  vector<string>A1, B1, C1;
  vector<int>A2, B2, C2;

  input_rle(A1, A2);
  input_rle(B1, B2);
  input_rle(C1, C2);

  int pos = find_rle(A1, A2, B1, B2);

  //  cout << pos << endl;
  if(pos != -1){
    replace_rle(A1, A2, B1, B2, C1, C2, pos);
    merge(A1, A2);
  }
  
  rep(i, A1.size())
    cout << A1[i] << " " << A2[i] << " ";
  cout << "$\n";
  return 0;
}

Measured memory_bytes: 3336