#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 AA, and Bitek writes BB. AA and BB have the same length, and they may have leading zeros.

However, Bajtek wins every time (that is, always ABA \ge B), so Bajtek wants to lose once.

Now he can change exactly kk digits of AA so that AA becomes smaller than BB.

Find the maximum possible value of AA after modification such that A<BA < B.

If it is impossible to make AA smaller than BB, output -1.

Because the brothers really like this game, they will play tt rounds, meaning there will be tt modifications and checks.

Input Format

The first line contains an integer tt, the number of rounds.
The next tt lines each contain three integers A,B,kA, B, k, representing the number written by Bajtek, the number written by Bitek, and the allowed number of modifications.

Output Format

Output tt lines, each containing one integer: the maximum value of AA after modification such that A<BA < B.
If no modification of AA can make it smaller than BB, 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 nn be the length of AA and BB:

  • Subtask 1 (18 pts): 1n51 \le n \le 5.
  • Subtask 2 (20 pts): 1n50001 \le n \le 5000.
  • Subtask 3 (20 pts): 1n1051 \le n \le 10^5, k=1k = 1.
  • Subtask 4 (42 pts): no special constraints.

For 100%100\% of the testdata, 1t1001 \le t \le 100, 1kn1051 \le k \le n \le 10^5, and ABA \ge B.

Notes

Translated from POI 2019 C Pomniejszenie

Translated by ChatGPT 5