This page is still under construction.

Parts of this page are still being built. What you see may change.

Largest Subsequence Number

Time limit3sMemory limit128 MB

Summary
Pick digits from N in order, without a leading zero, to form the largest value that leaves remainder R when divided by Q.
Level

Medium6 of 10

Topics
Dynamic programming, String, Math
Solved
No attempts yet

Problem

You are given three numbers NN, QQ, and RR. Find MM that satisfies all of the following.

  • MM is positive.
  • The decimal representation of MM is a subsequence of the decimal representation of NN, so you can build MM by deleting zero or more digits of NN.
  • MM leaves a remainder of RR when divided by QQ.
  • MM is the largest value that satisfies the conditions.

A decimal representation does not start with 0, so the first digit you keep cannot be 0.

Input

The first line contains the number of test cases TT (1≤T≤2001 \le T \le 200). Each of the next TT lines contains one test case.

Each line has three integers separated by single spaces, NN, RR, and QQ, in this order (1≤N<1010001 \le N < 10^{1000}, 0≤R<Q≤10000 \le R < Q \le 1000). No number in the input has a leading zero.

Output

For each test case, print the MM described above on a single line, with no leading zeros. If no such MM exists, print Not found on a single line instead.

Hint

For N=840N = 840, R=0R = 0, Q=8Q = 8, the number 840840 is divisible by 88, so the largest MM is 840840.

For N=901N = 901, R=3R = 3, Q=8Q = 8, the subsequences of 901901 are 99, 00, 11, 9090, 0101, 9191, 901901. Among them 00 is not positive and 0101 has a leading zero, so both drop out. Of the rest, only 9191 leaves a remainder of 33 when divided by 88.

For N=123456789N = 123456789, R=10R = 10, Q=100Q = 100, no subsequence leaves a remainder of 1010 when divided by 100100.

Examples1

  1. Example 1

    Input
    3
    840 0 8
    901 3 8
    123456789 10 100
    
    Expected output
    840
    91
    Not found