Under construction

Still being built. Things may change

Find and Replace

Time limit2sMemory limit1024 MB

Summary
Starting from "a", apply letter-to-string replacements, then print the substring from position l to position r of the final string.
Level

Medium6 of 10

Topics
Recursion, Dynamic programming, String
Solved
No attempts yet

Problem

Bessie is using miV, the newest text-editing software. Its find-and-replace feature lets her find every occurrence of a lowercase English letter cc and replace each one with a nonempty string ss of lowercase letters. For example, given the string "ball", if Bessie picks cc to be 'l' and ss to be "na", the string becomes "banana".

Bessie starts with the string "a" and applies a number of these operations, which produces a final string SS. Since SS can be very long, for given ll and rr with 1≤l≤r≤min⁡(∣S∣,1018)1\le l\le r\le \min(|S|,10^{18}), she wants to know the substring Sl…rS_{l\dots r} from the ll-th to the rr-th character, inclusive.

The sum of ∣s∣|s| over all operations is at most 2⋅1052\cdot 10^5, and r−l+1≤2⋅105r-l+1\le 2\cdot 10^5.

Input

The first line contains ll, rr, and the number of operations. Each of the following lines describes one operation and contains cc and ss. All characters are lowercase English letters from 'a' to 'z'.

Output

Print the string Sl…rS_{l\dots r} on a single line.

Hint

The string changes as follows:

a →\rightarrow ab →\rightarrow bcb →\rightarrow bdeb →\rightarrow bbbdebbb

Examples1

  1. Example 1

    Input
    3 8 4
    a ab
    a bc
    c de
    b bbb
    
    Expected output
    bdebbb