loj#P3234. 「POI2019 R1」Pomniejszenie

「POI2019 R1」Pomniejszenie

Description

Source Problem: POI XXVII - I etap (Pomniejszenie)

Bytie and Bitie are playing a simple game – the person who writes down the greater number wins. The brothers already wrote their numbers on sheets of paper: Bytie wrote AA whereas Bitie BB. Bytie, who is older and more experienced, always picks the larger number, but this time he wants to let his little brother win. So he peeked discretely at Bitie’s sheet and, knowing BB he wants to change exactly kk digits in his own number AA so that this yields a number smaller than BB. To make it less discernible that he threw (i.e., intentionally lost) the game, Bytie wants to obtain the largest possible number that is less than BB. Help him out in this task!

Input

In the first input line, there is a single positive integer tt, specifying the number of game instances to consider.

Each of the tt lines that follow contains three nonnegative integers AA, BB, and kk. The numbers AA and BB have the same length and may have leading zeros. The number k is positive and no larger than the (common) length of AA and BB. Moreover, ABA \geq B.

Output

Exactly tt lines should be printed to the output, the ii-th of which should hold the answer to the ii-th instance from input. The answer for given AA and BB is the (single) integer CC such that:

  • the number CC has the same length as AA and BB (and may have leading zeros),
  • C<BC < B,
  • the number CC may be obtained from AA by changing exactly kk of its digits,
  • CC is maximum subject to above constraints.

If there is no integer CC that satisfies these conditions, the number 1−1 should be printed.

4
555 333 1
0555 0551 3
0555 0333 4
9 9 1
255
0499
-1
8

Additional Samples

See files pom/pom*.in and pom/pom*.out for additional samples.

  • Additional Sample 1: t=100t = 100; AA equals 20000,20001,20 000, 20 001, \ldots, whereas BB is constantly 2000020 000; k=1k = 1;

  • Additional Sample 2: t=100t = 100; in each test n=5000n = 5000 and the only digit appearing in either AA or BB (apart from leading zeros) is 99; k=4901,,5000k = 4901, \ldots , 5000;

  • Additional Sample 3: t=100t = 100; in each test n=100000n = 100 000, the only digit (apart from leading zeros) appearing in AA is 99, and the only one in BB is 22; k=1,,100k = 1, \ldots , 100.

Constraints

The set of tests consists of the following subsets. Within each subset, there may be several tests. We denote the length of AA and BB by nn.

All tests satisfy 1t1001 \leq t \leq 100.

Subtask # Additional Constraints Score
11 1n51 \le n \le 5 1818
22 1n50001 \le n \le 5000 2020
33 1n105,k=11 \le n \le 10^5, k=1
44 1n1051 \le n \le 10^5 4242