Joey and Divisibilty
Difficulty: Hard

Joey is crazy about divisibility. He wants you to give the smallest natural number
which is divisible by **n** and is solely made of digits from **0-d**, where **1<=d<=9**


__Example__

if Joey tells you that n=8 and d=1, then your output should be 1000 as
there is no natural number smaller than 1000 which is made of 1 and 0 only and is
divisible by 8.

__Input Format__

First line contains _**T**_, i.e the number of test cases. 
Each of the next _**T**_ lines contain 2 numbers, **n** and **d**.

__Output Format__

Output T lines, one for each test case, containing the desired answer for the corresponding test case.
Output -1 if an answer does not exist.


__Constraints__

1<=T<=50

1<=n<=100000

1<=d<=10


__Sample Input__

3

8 1

7 3

5 9

__Sample Output__

1000

21

5



Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):

    //#define LOCAL     //uncomment for testing from local file
    #include <iostream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <vector>
    #include <set>
    #include <map>
    #include <bitset>
    #include <climits>
    #include <ctime>
    #include <algorithm>
    #include <functional>
    #include <stack>
    #include <queue>
    #include <list>
    #include <deque>
    #include <sys/time.h>
    #include <iomanip>
    #include <cstdarg>
    #include <utility> //std::pair
    #include <cassert>
    #define tr(c,i) for(typeof(c.begin()) i = (c).begin(); i != (c).end(); i++) 
    #define pb push_back
    #define mp make_pair
    #define clr(x,y) memset(x,y,sizeof x)
    #define lld long long int
    using namespace std;


    class Graph{
    private:
        lld V;
        vector<lld> *adj;
    public:
        Graph(lld _V);
        void addEdge(lld u, lld v);
        void BFS(lld start, string &candidate);
    };

    Graph::Graph(lld _V){
        V = _V;
        adj = new vector<lld>[V];
    }

    void Graph::addEdge(lld u, lld v){
        adj[u].pb(v);
    }

    /*candidate stores the lowest natural number starting with start and divisible by n (or V)*/
    void Graph::BFS(lld start, string &candidate){ 
        queue<lld> nodes;
        lld parent[V], i, k, digit;
        bool visited[V];
        clr(visited, false);
        clr(parent,-1);
        nodes.push(start);
        visited[start] = true;
        while(!nodes.empty()){
            k = nodes.front();
            tr(adj[k], it){
                if(!visited[*it]){
                    visited[*it] = true;
                    parent[*it] = k;
                    nodes.push(*it);
                }
            }
            //if 0 is discovered
            if(visited[0]){
                break;
            }
            nodes.pop();
        }
        candidate="";
        i = 0;  //last state has to be 0
    /*build candidate string from parent array*/
        while(parent[i]!=-1)
        {
            /*just the reverse procedure of the way one added the edged in automata*/
            digit = -10*parent[i]+i;               
            while(digit<0)digit+=V;
            candidate = char('0'+digit)+candidate;
            i = parent[i];
        }
        candidate = char('0'+start)+candidate;
    }

    int main()
    {
    #ifdef LOCAL
        freopen("input.txt","r",stdin);
    #endif
         lld t,n,d,i,j,minLength,nextState;
         string candidate, answer;
         scanf("%lld",&t);
         while(t--){
             scanf("%lld %lld",&n, &d);
             if(n<=d){
                 cout<<n<<"\n"; 
                 continue;
             }
             Graph g(n);
             for(i=0;i<n;i++){
                 for(j=0;j<=d;j++){
                     /*if I'm in state i presently and the next digit is j, then the remainder of the number thus formed when divided by n becomes (10*i+j)%n*/
                     nextState = (10*i+j)%n;       
                     /*shouldn't care about self-loops*/
                     if(nextState!=i)              
                         g.addEdge(i, (10*i+j)%n);
                 }
             }

             minLength = -1;
             for(i=1;i<=d;i++){
                 g.BFS(i, candidate);
                 if(minLength>candidate.length()){
                     minLength = candidate.length();
                     answer = candidate;
                 }
             }
             /*can't think of any case for which answer is -1*/
             if(minLength<1)answer="-1";
             cout<<answer<<"\n";
         }
    }