#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Node{
	char ch;
	int length;
	Node* prev;
	Node* next;
};

struct Info{
	void set(char arg_ch,int arg_length){
		ch = arg_ch;
		length = arg_length;
	}
	char ch;
	int length;
	bool isFirst,isLast;
};

Info A[3000],B[3000],C[3000];
int a_len = 0,b_len = 0,c_len = 0;

class List{
public:
	List(){
		motherNode = new Node();
		motherNode->ch = '#';
		motherNode->length = -1;
		motherNode->prev = motherNode;
		motherNode->next = motherNode;
		matchFirstNode = motherNode;
		matchLastNode = motherNode;
		left_complete_match = false;
		right_complete_match = false;
	};
	void registA(Info info){

		Node* tmpNode = motherNode->prev;
		motherNode->prev = new Node;
		motherNode->prev->ch = info.ch;
		motherNode->prev->length = info.length;
		motherNode->prev->next = motherNode;
		motherNode->prev->prev = tmpNode;
		tmpNode->next = motherNode->prev;
	};

	bool isMatchB(){

		bool FLG;
		int index;

		Node* startNode = motherNode;
		Node* tmpNode;

		while(true){
			startNode = startNode->next;
			if(startNode == motherNode)break;

			tmpNode = startNode;

			index = 0;

			FLG = true;

			while(tmpNode != motherNode){
				if(tmpNode->ch != B[index].ch){
					FLG = false;
					break;
				}else{

					if(B[index].isFirst == false && B[index].isLast == false){
						if(tmpNode->length != B[index].length){
							FLG = false;
							break;
						}else{
							tmpNode = tmpNode->next;
							index++;
						}
					}else{
						if(tmpNode->length >= B[index].length){
							tmpNode = tmpNode->next;
							index++;
						}else{
							FLG = false;
							break;
						}
					}
				}
				if(index == b_len)break;
			}

			if(FLG == true && index == b_len){

				if(startNode->length == B[0].length){
					matchFirstNode = startNode->prev;
					left_complete_match = true;
				}else{
					matchFirstNode = startNode;
				}

				tmpNode = tmpNode->prev;
				if(tmpNode->length == B[b_len-1].length){
					matchLastNode = tmpNode->next;
					right_complete_match = true;
				}else{
					matchLastNode = tmpNode;
				}
				return true;
			}
		}

		return false;
	}

	void replace(){

		Node* tmpNode,*preNode,*firstNext;
		int index;

		firstNext = matchFirstNode->next;

		if(left_complete_match){
			if(matchFirstNode->ch == C[0].ch){
				matchFirstNode->length += C[0].length;
				matchFirstNode->next = new Node;
				tmpNode = matchFirstNode->next;
				index = 1;
			}else{
				matchFirstNode->next = new Node;
				tmpNode = matchFirstNode->next;
				index = 0;
			}
		}else{

			if(b_len > 1){
				if(matchFirstNode->ch != C[0].ch){
					matchFirstNode->length -= B[0].length;
					matchFirstNode->next = new Node;
					tmpNode = matchFirstNode->next;
					index = 0;
				}else{
					matchFirstNode->length = matchFirstNode->length - B[0].length + C[0].length;
					matchFirstNode->next = new Node;
					tmpNode = matchFirstNode->next;
					index = 1;
				}
			}else{
				firstNext->prev = new Node;
				firstNext->prev->next = firstNext;
				firstNext->prev->ch = matchFirstNode->ch;
				firstNext->prev->length = matchFirstNode->length - B[0].length;
				firstNext = firstNext->prev;
				firstNext->prev = matchFirstNode;

				if(matchFirstNode->prev->ch == C[0].ch){
					matchFirstNode->prev->length += C[0].length;
					tmpNode = matchFirstNode;
					index = 1;
				}else{
					tmpNode = matchFirstNode;
					index = 0;
				}
			}
		}

		preNode = matchFirstNode;

		if(index == c_len){
			if(right_complete_match){
				if(matchLastNode->ch != C[0].ch){
					matchFirstNode->next = matchLastNode;
					matchLastNode->prev = matchFirstNode;
				}else{
					matchFirstNode->length += matchLastNode->length;
					matchFirstNode->next = matchLastNode->next;
					matchLastNode->next->prev = matchFirstNode;
				}
			}else{
				if(matchFirstNode != matchLastNode){
					if(matchLastNode->ch != C[0].ch){
						matchFirstNode->next = matchLastNode;
						matchLastNode->prev = matchFirstNode;
						matchLastNode->length -= B[b_len-1].length;
					}else{
						matchFirstNode->length += matchLastNode->length - B[b_len-1].length;
						matchFirstNode->next = matchLastNode->next;
						matchLastNode->next->prev = matchFirstNode;
					}
				}else{

					if(b_len == 1){ //c_len == 1
						if(firstNext->ch != C[0].ch){
							matchFirstNode->ch = C[0].ch;
							matchFirstNode->length = C[0].length;
							matchFirstNode->next = firstNext;
						}else{ //firstNext.ch == C[0].ch
							matchFirstNode->ch = C[0].ch;
							matchFirstNode->length = C[0].length + firstNext->length;
							matchFirstNode->next = firstNext->next;
							firstNext->next->prev = matchFirstNode;
						}
					}else{ //b_len != 1
						//Do nothing
					}
				}
			}

			return;
		}

		while(index < c_len){
			if(index != c_len - 1){
				tmpNode->ch = C[index].ch;
				tmpNode->length = C[index].length;
				tmpNode->prev = preNode;
				preNode = tmpNode;
				tmpNode->next = new Node;
				tmpNode = tmpNode->next;
				index++;
			}else{
				if(right_complete_match){
					if(matchLastNode->ch != C[index].ch){
						tmpNode->ch = C[index].ch;
						tmpNode->length = C[index].length;
						tmpNode->prev = preNode;
						tmpNode->next = matchLastNode;
						matchLastNode->prev = tmpNode;
						index++;
					}else{
						tmpNode->ch = C[index].ch;
						tmpNode->length = C[index].length + matchLastNode->length;
						tmpNode->prev = preNode;
						tmpNode->next = matchLastNode->next;
						matchLastNode->next->prev = tmpNode;
						index++;
					}
				}else{

					if(matchFirstNode != matchLastNode){
						if(matchLastNode->ch != C[index].ch){
							tmpNode->ch = C[index].ch;
							tmpNode->length = C[index].length;
							tmpNode->prev = preNode;
							tmpNode->next = matchLastNode;
							matchLastNode->length -= B[b_len-1].length;
							matchLastNode->prev = tmpNode;
							index++;
						}else{
							tmpNode->ch = C[index].ch;
							tmpNode->length = matchLastNode->length-B[b_len-1].length + C[index].length;
							tmpNode->prev = preNode;
							tmpNode->next = matchLastNode->next;
							matchLastNode->next->prev = tmpNode;
							index++;
						}
					}else{

						if(firstNext->ch != C[index].ch){
							tmpNode->ch = C[index].ch;
							tmpNode->length = C[index].length;
							tmpNode->prev = preNode;
							tmpNode->next = firstNext;
							firstNext->prev = tmpNode;
							index++;
						}else{
							tmpNode->ch = C[index].ch;
							tmpNode->length = C[index].length + firstNext->length;
							tmpNode->prev = preNode;
							tmpNode->next = firstNext->next;
							firstNext->next->prev = tmpNode;
							index++;
						}
					}
				}
			}
		}
	}

	void output(){

		Node* tmpNode = motherNode->next;
		while(tmpNode != motherNode){
			printf("%c %d ",tmpNode->ch,tmpNode->length);
			tmpNode = tmpNode->next;
		}

		printf("$\n");
	}

private:
	Node* motherNode,*matchFirstNode,*matchLastNode;
	bool left_complete_match,right_complete_match;
};



int main(){

	List list;

	char buf[2];
	int num;
	Info tmp;

	while(true){
		scanf("%s",buf);
		if(buf[0] == '$')break;

		scanf("%d",&num);

		tmp.set(buf[0],num);
		A[a_len++].set(buf[0],num);

		list.registA(tmp);
	}

	for(int i = 0; i < 2000; i++){
		B[i].isFirst = false;
		B[i].isLast = false;
	}

	while(true){
		scanf("%s",buf);
		if(buf[0] == '$')break;

		scanf("%d",&num);

		B[b_len++].set(buf[0],num);
	}

	B[0].isFirst = true;
	B[b_len-1].isLast = true;

	if(!list.isMatchB()){

		for(int i = 0; i < a_len; i++){
			printf("%c %d ",A[i].ch,A[i].length);
		}
		printf("$\n");
		return 0;
	}

	while(true){
		scanf("%s",buf);
		if(buf[0] == '$')break;

		scanf("%d",&num);

		C[c_len++].set(buf[0],num);
	}

	list.replace();

	list.output();

	return 0;
}

Measured memory_bytes: 2848