This page is still under construction.

Parts of this page are still being built. What you see may change.

RLE Replacement

Time limit2sMemory limit128 MB

Summary
Replace the first occurrence of RLE string B inside RLE string A with RLE string C and print the result in RLE form.
Level

Medium6 of 10

Topics
String matching, Two pointers, Implementation
Solved
No attempts yet

Problem

Source code in one programming language is written with uppercase English letters only, and the same letter often repeats many times in a row. Very large source files in that language are stored compressed by run length encoding.

Run length encoding (RLE) rewrites every maximal block of identical letters as a pair of that letter and the length of the block. For example, the string RRRRLEEE becomes R4L1E3 under RLE.

Three strings AA, BB, and CC are given in RLE form. Write a program that replaces the first occurrence of BB as a substring of AA with CC and prints the resulting string in RLE form. If BB does not occur in AA, print AA in RLE form unchanged.

Input

The input consists of three lines.

A
B
C

The three lines hold the RLE encodings of AA, BB, and CC, in that order. Each line has this format:

c1 l1 c2 l2 ... cn ln $

Each cic_i (1≤i≤n1 \le i \le n) is an uppercase English letter (A through Z), and each lil_i (1≤i≤n1 \le i \le n, 1≤li≤1081 \le l_i \le 10^8) is an integer, the length of the repetition of cic_i. The number of pairs nn satisfies 1≤n≤1031 \le n \le 10^3. The letters and the integers are separated by a single space, and the terminal symbol $ ends the line. For every ii with 1≤i≤n−11 \le i \le n-1, ci≠ci+1c_i \neq c_{i+1} holds.

Output

If BB occurs in AA, replace the first occurrence of BB with CC; otherwise keep AA as it is. Print the result in RLE form on one line, in this format:

c1 l1 c2 l2 ... cm lm $

The output must satisfy ci≠ci+1c_i \neq c_{i+1} for 1≤i≤m−11 \le i \le m-1 and li>0l_i > 0 for 1≤i≤m1 \le i \le m.

Examples2

  1. Example 1

    Input
    R 100 L 20 E 10 $
    R 5 L 10 $
    X 20 $
    
    Expected output
    R 95 X 20 L 10 E 10 $
    
  2. Example 2

    Input
    A 3 B 3 A 3 $
    A 1 B 3 A 1 $
    A 2 $
    
    Expected output
    A 6 $