This page is still under construction.

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

Smallest Hex Multiple

Time limit2sMemory limit256 MB

Summary
Find the smallest multiple of N whose base-16 representation uses only the allowed digits, or report that none exists.
Level

Medium6 of 10

Topics
BFS, Graph, Shortest path, Number theory
Solved
No attempts yet

Problem

You are given an integer NN and a list of DD hexadecimal digits. Find the smallest positive integer XX that is divisible by NN and whose base-16 representation uses only the given digits.

Input

The first line contains the number of test cases TT (1≤T≤1001 \le T \le 100).

Each test case takes two lines. The first line contains NN and DD in base 10, separated by a space (1≤N≤2000001 \le N \le 200000, 1≤D≤161 \le D \le 16). The second line contains the allowed hexadecimal digits d1,d2,…,dDd_1, d_2, \dots, d_D separated by spaces. Each digit is one of 0 to 9 and a to f, the digits are distinct, and they are given in increasing order.

Output

For each test case, print one line. Print, in base 16, the smallest positive integer XX that is divisible by NN and uses only the given digits. Write a to f in lowercase and print no leading zeros. If no such number exists, print no solution.

The answer can be very long. It does not always fit in a 64-bit integer, so build it as a string.

Examples2

  1. Example 1

    Input
    4
    1 3
    a b c
    2 8
    1 3 5 7 9 b d f
    1207 3
    1 a f
    33910 4
    0 c e f
    
    Expected output
    a
    no solution
    1aa1aa
    c0ffee
    
  2. Example 2

    Input
    3
    16 2
    0 1
    1 1
    0
    15 16
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    
    Expected output
    10
    no solution
    f