#D9278. RLE Replacement

    ID: 7710 Type: Default 2000ms 134MiB

RLE Replacement

RLE Replacement

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 AA, BB, and CC that are encoded by RLE, your task is to implement a function replacing the first occurrence of the substring BB in AA with CC, and outputting the edited string encoded by RLE. If BB does not occur in AA, you must output AA encoded by RLE without changes.

Input

The input consists of three lines.

AA BB CC

The lines represent strings AA, BB, and CC that are encoded by RLE, respectively. Each of the lines has the following format:

c1c_1 l1l_1 c2c_2 l2l_2 \ldots cnc_n lnl_n $

Each cic_i (1in1 \leq i \leq n) is an uppercase English letter (A-Z) and lil_i (1in1 \leq i \leq n, 1li1081 \leq l_i \leq 10^8) is an integer which represents the length of the repetition of cic_i. The number nn of the pairs of a letter and an integer satisfies 1n1031 \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 cici+1c_i \neq c_{i+1} holds for any 1in11 \leq i \leq n-1.

Output

Replace the first occurrence of the substring BB in AA with CC if BB occurs in AA, and output the string encoded by RLE. The output must have the following format:

c1c_1 l1l_1 c2c_2 l2l_2 \ldots cmc_m lml_m $

Here, cici+1c_i \neq c_{i+1} for 1im11 \leq i \leq m-1 and li>0l_i \gt 0 for 1im1 \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 $

inputFormat

outputFormat

outputting the edited string encoded by RLE. If BB does not occur in AA, you must output AA encoded by RLE without changes.

Input

The input consists of three lines.

AA BB CC

The lines represent strings AA, BB, and CC that are encoded by RLE, respectively. Each of the lines has the following format:

c1c_1 l1l_1 c2c_2 l2l_2 \ldots cnc_n lnl_n $

Each cic_i (1in1 \leq i \leq n) is an uppercase English letter (A-Z) and lil_i (1in1 \leq i \leq n, 1li1081 \leq l_i \leq 10^8) is an integer which represents the length of the repetition of cic_i. The number nn of the pairs of a letter and an integer satisfies 1n1031 \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 cici+1c_i \neq c_{i+1} holds for any 1in11 \leq i \leq n-1.

Output

Replace the first occurrence of the substring BB in AA with CC if BB occurs in AA, and output the string encoded by RLE. The output must have the following format:

c1c_1 l1l_1 c2c_2 l2l_2 \ldots cmc_m lml_m $

Here, cici+1c_i \neq c_{i+1} for 1im11 \leq i \leq m-1 and li>0l_i \gt 0 for 1im1 \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 $

样例

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