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 whereas Bitie . 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 he wants to change exactly digits in his own number so that this yields a number smaller than . 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 . Help him out in this task!
Input
In the first input line, there is a single positive integer , specifying the number of game instances to consider.
Each of the lines that follow contains three nonnegative integers , , and . The numbers and have the same length and may have leading zeros. The number k is positive and no larger than the (common) length of and . Moreover, .
Output
Exactly lines should be printed to the output, the -th of which should hold the answer to the -th instance from input. The answer for given and is the (single) integer such that:
- the number has the same length as and (and may have leading zeros),
- ,
- the number may be obtained from by changing exactly of its digits,
- is maximum subject to above constraints.
If there is no integer that satisfies these conditions, the number 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: ; equals , whereas is constantly ; ;
-
Additional Sample 2: ; in each test and the only digit appearing in either or (apart from leading zeros) is ; ;
-
Additional Sample 3: ; in each test , the only digit (apart from leading zeros) appearing in is , and the only one in is ; .
Constraints
The set of tests consists of the following subsets. Within each subset, there may be several tests. We denote the length of and by .
All tests satisfy .
Subtask # | Additional Constraints | Score |
---|---|---|