Under construction

Still being built. Things may change

Making Number

Time limit1.5sMemory limit1024 MB

Summary
Given X and a Y whose digits change one at a time, answer queries for a digit of the smallest rearrangement of X's digits, with no leading zero, that is at least Y.
Level

Medium7 of 10

Topics
Segment tree, Greedy, String
Solved
No attempts yet

Problem

You are given two positive integers XX and YY with the same number of digits in base 10. ZZ is the positive integer in base 10 that satisfies all of the following conditions.

  • The digits of ZZ are a rearrangement of the digits of XX. The first digit of ZZ cannot be 0. For example, if X=1103X=1103, ZZ can be 11031103 or 31013101, but ZZ cannot be 21102110, 301301, or 01310131.
  • Y≤ZY\le Z.
  • ZZ is the minimum value that satisfies the conditions above.

Process QQ queries. Each query is one of the following two types.

  • Change the ii-th digit of YY to xx.
  • Output the ii-th digit of ZZ. If no such ZZ exists, output −1-1.

Digits are counted from the left.

Input

The first line contains two integers XX and YY, separated by a space.

The second line contains QQ.

Each of the next QQ lines is a query in one of the following forms. The first integer is the query type.

  • 1 ii xx: Change the ii-th digit of YY to xx.
  • 2 ii: Output the ii-th digit of ZZ. If no such ZZ exists, output −1-1.

At least one query of type 2 is given.

Output

For each query of type 2, print the answer on its own line.

Constraints

1≤X,Y<101000001\le X, Y<10^{100000} 1≤Q≤1000001\le Q\le 100000 XX and YY have the same number of digits. The first digits of XX and YY are not 0. For a query of type 1, 1≤i≤1\le i\le the number of digits of YY and 0≤x≤90\le x\le 9. If i=1i=1, then x≠0x\ne 0. For a query of type 2, 1≤i≤1\le i\le the number of digits of YY.

Examples3

  1. Example 1

    Input
    3304 1615
    6
    2 3
    2 4
    1 1 3
    2 2
    1 2 4
    2 1
    
    Expected output
    3
    4
    0
    3
    
  2. Example 2

    Input
    838046 780357
    10
    2 1
    2 2
    1 2 4
    2 3
    2 4
    1 4 5
    2 5
    2 6
    1 1 9
    2 2
    
    Expected output
    8
    0
    3
    4
    6
    8
    -1
    
  3. Example 3

    Input
    2950 9052
    4
    2 1
    2 2
    2 3
    2 4
    
    Expected output
    9
    0
    5
    2