1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstdlib>
5 #include <cstring>
6 using namespace std;
7
8 int n, m;
9 int a[10];
10 bool vis[10];
11 int x, k;
12 int ans;
13 bool fl;
14
15 inline void dfs(int now, int sum)
16 {
17 if (fl) return;
18 if (now == n + 1)
19 {
20 if ((sum + x) % k == 0) {ans = sum;fl = 1;}
21 return;
22 }
23
24 for (register int i = 1 ; i <= n ; i ++)
25 {
26 if (vis[i]) continue;
27 vis[i] = 1;
28 dfs(now +1, sum * 10 + a[i]);
29 vis[i] = 0;
30 }
31
32 }
33
34 int main()
35 {
36 cin >> n >> m;
37 for (register int i=1;i<=n;i++) scanf("%d",&a[i]);
38 sort (a + 1, a + 1 + n);
39 while (m--)
40 {
41 ans = -233;
42 scanf("%d%d", &x, &k);
43 memset(vis, 0, sizeof vis);
44 fl = 0;
45 dfs(1, 0);
46 if (ans == -233) puts("None");
47 else printf("%d\n", ans);
48 }
49 return 0;
50 }