#P6661. [POI 2019/2020 R1] Pomniejszenie / 削减
[POI 2019/2020 R1] Pomniejszenie / 削减
Background
Bajtek and Bitek are brothers (Bajtek is the older one), and they want to play a game.
Problem Description
The rules are: whoever writes the larger number wins.
Suppose Bajtek writes , and Bitek writes . and have the same length, and they may have leading zeros.
However, Bajtek wins every time (that is, always ), so Bajtek wants to lose once.
Now he can change exactly digits of so that becomes smaller than .
Find the maximum possible value of after modification such that .
If it is impossible to make smaller than , output -1.
Because the brothers really like this game, they will play rounds, meaning there will be modifications and checks.
Input Format
The first line contains an integer , the number of rounds.
The next lines each contain three integers , representing the number written by Bajtek, the number written by Bitek, and the allowed number of modifications.
Output Format
Output lines, each containing one integer: the maximum value of after modification such that .
If no modification of can make it smaller than , output -1.
4
555 333 1
0555 0551 3
0555 0333 4
9 9 1
255
0499
-1
8
Hint
Sample Explanation
The first two additional samples correspond to sample1/2.in and sample1/2.out in the additional files.
The third additional sample is sample3.zip.
Constraints
This problem uses bundled testdata.
Let be the length of and :
- Subtask 1 (18 pts): .
- Subtask 2 (20 pts): .
- Subtask 3 (20 pts): , .
- Subtask 4 (42 pts): no special constraints.
For of the testdata, , , and .
Notes
Translated from POI 2019 C Pomniejszenie。
Translated by ChatGPT 5