Problem: H - RLE Replacement

Problem Statement

In JAG Kingdom, ICPC (Intentionally Compressible Programming Code) is one of the common programming languages. Programs in this language only contain uppercase English letters and the same letters often appear repeatedly in ICPC programs. Thus, programmers in JAG Kingdom prefer to compress ICPC programs by Run Length Encoding in order to manage very large-scale ICPC programs.

Run Length Encoding (RLE) is a string compression method such that each maximal sequence of the same letters is encoded by a pair of the letter and the length. For example, the string "RRRRLEEE" is represented as "R4L1E3" in RLE.

Now, you manage many ICPC programs encoded by RLE. You are developing an editor for ICPC programs encoded by RLE, and now you would like to implement a replacement function. Given three strings $A$, $B$, and $C$ that are encoded by RLE, your task is to implement a function replacing the first occurrence of the substring $B$ in $A$ with $C$, and outputting the edited string encoded by RLE. If $B$ does not occur in $A$, you must output $A$ encoded by RLE without changes.

Input

The input consists of three lines.

> $A$
>  $B$
>  $C$

The lines represent strings $A$, $B$, and $C$ that are encoded by RLE, respectively. Each of the lines has the following format:

> $c_1$ $l_1$ $c_2$ $l_2$ $\ldots$ $c_n$ $l_n$ \$

Each $c_i$ ($1 \leq i \leq n$) is an uppercase English letter (`A`-`Z`) and $l_i$ ($1 \leq i \leq n$, $1 \leq l_i \leq 10^8$) is an integer which represents the length of the repetition of $c_i$. The number $n$ of the pairs of a letter and an integer satisfies $1 \leq n \leq 10^3$. A terminal symbol `$` indicates the end of a string encoded by RLE. The letters and the integers are separated by a single space. It is guaranteed that $c_i \neq c_{i+1}$ holds for any $1 \leq i \leq n-1$.

Output

Replace the first occurrence of the substring $B$ in $A$ with $C$ if $B$ occurs in $A$, and output the string encoded by RLE. The output must have the following format:

> $c_1$ $l_1$ $c_2$ $l_2$ $\ldots$ $c_m$ $l_m$ \$

Here, $c_i \neq c_{i+1}$ for $1 \leq i \leq m-1$ and $l_i \gt 0$ for $1 \leq i \leq m$ must hold.

Sample Input 1


R 100 L 20 E 10 \$
R 5 L 10 \$
X 20 \$

Output for the Sample Input 1


R 95 X 20 L 10 E 10 \$

Sample Input 2


A 3 B 3 A 3 \$
A 1 B 3 A 1 \$
A 2 \$

Output for the Sample Input 2


A 6 \$





Example

Input

R 100 L 20 E 10 \$
R 5 L 10 \$
X 20 \$


Output

R 95 X 20 L 10 E 10 \$

Solution (in CPP):

Solution:
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int main() {
	char at[1000]; int an[1000];
	char bt[1000]; int bn[1000];
	char ct[1000]; int cn[1000];
	char x;
	int c1 = 0; int m;
	while (cin >> x) {
		if (x == '$')break;
		cin >> m;
		at[c1] = x; an[c1] = m; c1++;
	}
	int c2 = 0;
	while (cin >> x) {
		if (x == '$')break;
		cin >> m;
		bt[c2] = x; bn[c2] = m; c2++;
	}
	int c3 = 0;
	while (cin >> x) {
		if (x == '$')break;
		cin >> m;
		ct[c3] = x; cn[c3] = m; c3++;
	}
	int chk = -1;
	rep(i, c1 - c2+1) {
		if (at[i] == bt[0]) {
			bool f = true;
			if (an[i] < bn[0])f = false;
			Rep(k, 1 ,c2-1){
				if (at[i + k] != bt[k] || an[i + k] != bn[k])f = false;
			}
			if (at[i+c2-1]!=bt[c2-1]||an[i + c2 - 1] < bn[c2 - 1])f = false;
			if (f) {
				chk = i; break;
			}
		}
	}
	queue<pair<char, int>> q1, q2;
	if (chk == -1) {
		rep(i, c1) {
			q1.push({ at[i],an[i] });
		}
	}
	else {
		rep(i, chk) {
			q1.push({ at[i],an[i] });
	    }
		if (c2>1&&an[chk] - bn[0] > 0) {
			q1.push({ at[chk],an[chk] - bn[0] });
		}
		rep(i, c3) {
			q1.push({ ct[i],cn[i] });
		}
		if (an[chk + c2 - 1] - bn[c2 - 1] > 0) {
			q1.push({ at[chk + c2 - 1],an[chk + c2 - 1] - bn[c2 - 1] });
		}
		Rep(i, chk + c2, c1) {
			q1.push({ at[i],an[i] });
		}
	}
	pair<char, int>memo = { '#',-1 };
	while (!q1.empty()) {
		pair<char, int> now = q1.front(); q1.pop();
		if (now.first == memo.first) {
			memo.second += now.second;
		}
		else {
			if (memo.second >= 0) {
				q2.push(memo);
			}
			memo = now;
		}
	}
	q2.push(memo);
	while (!q2.empty()) {
		pair<char, int> out = q2.front(); q2.pop();
		cout << out.first << " " << out.second << " ";
	}
	cout << '$' << endl;
	return 0;
}