This page is still under construction.

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

Changing Phone Numbers

Time limit1sMemory limit128 MB

Summary
Given area codes and a sequence of rules (digit duplication, digit swap, area-code change) applied over time, answer queries transforming a phone number from one year to another.
Level

Hard8 of 10

Topics
String, Simulation, Implementation, Hash map
Solved
No attempts yet

Problem

You work for OTC, the Olandican Telecommunication Company. Unfortunately, OTC did not make wise decisions when assigning telephone numbers to its clients. For example, it underestimated the growth in demand, so it had to increase the number of digits in the local telephone numbers of Oland (the capital) from six to seven, and then again from seven to eight.

A telephone number consists of two parts: an area code and the local number used within that area. For example, if the area code of Oland city is 021, then a telephone number in that city may look like 0211234567. No area code is a prefix of another area code.

Changing telephone numbers is not easy, because it requires updating several databases containing millions of records. Fortunately, the changes follow only a limited set of rules. In the rules below, the position ii is counted starting from 11 within the local number, i.e. after removing the area code.

  1. For every local number under a given area code, repeat the ii-th digit of the local number once more. For example, under area code 021, repeating the second digit changes 0211234567 into 02112234567.
  2. For every number under a given area code, swap the ii-th and (i+1)(i+1)-th digit of the local number. For example, under area code 021, swapping the second and third digits changes 0211234567 into 0211324567.
  3. Change the area code of a given area to a new value. For example, changing 021 into 0211 changes 0211234567 into 02111234567.

Changing area codes with the third rule always preserves the property that no area code is a prefix of another. Given the area-code information, all telephone numbers, and the set of rules that are applied, write a program that determines the resulting telephone numbers after all changes.

Input

The first part of the input describes the area codes. The first line contains a single integer AA (1≤A≤10001 \le A \le 1000), the number of area codes, followed by AA lines, each of the form:

area-code   area-name

area-code is a string of at least 11 and at most 66 digits, and area-name is a string of at least 11 and at most 2020 letters (uppercase and lowercase allowed). No two lines share the same area code, and no two lines share the same area name.

The second part describes the rules applied. Its first line contains a single integer RR (0≤R≤100000 \le R \le 10000), the number of rules, followed by RR lines, each of the form:

year   rule-info

year is the year in which the rule is applied. Each rule takes effect on the first day of that year, and at most one rule is applied in any given year. Depending on the rule, rule-info has one of the following forms:

  • 1 area-name i
  • 2 area-name i
  • 3 area-name new-area-code

You may assume the input is always consistent: rule position indices are never out of range, and at no point in time is one area code a prefix of another.

The third part consists of several queries, each of the form:

year1   year2   number

The query asks: given that in year1 there was a telephone number number, what does that number become in year2 (with year1 ≤\le year2)? You must apply, in order, the rules that were applied in the years from year1 + 1 through year2. The queries end with a line containing three zeros. Every year is a positive integer less than 10910^9.

When a line contains more than one data item (a number or a name), the items are separated by one or more spaces, and there may be an arbitrary number of leading or trailing spaces. Each data item has at least one character, and each input line has at most 5050 characters.

Output

For each query, print on its own line the resulting telephone number after the changes.

Examples1

  1. Example 1

    Input
    4
    021 Oland
    0511 Moland
    0311 Boland
    03121 Kamand
    7
    2002 1 Moland 2
    2001 3 Moland 0515
    2003 2 Boland 3
    2005 1 Kamand 1
    2000 1 Kamand 1
    1999 1 Kamand 1
    1998 3 Oland 012345
    2000 2005 0311243426
    2000 2005 05113837462
    2000 2005 03121437478
    0 0 0
    
    Expected output
    0311244326
    051538837462
    031214437478