/* paiza POH! Vol.1
* result:
* http://p...content-available-to-author-only...a.jp/poh/ec-campaign/result/7712b5305e22862e2d41039cd4448c23
* author: Leonardone @ NEETSDKASU
*/
#include <stdio.h>
#include <stdlib.h>
#define PMAX (1000000)
#define PC(c) putchar(c)
#define PD(d) printf("%d\n", d)
#define PS(s) printf("%s\n", s)
#define SUMCHECK(tmp, sum, i, u, m) \
sum = i + u; \
if (sum > tmp) { \
tmp = sum; \
if (tmp == m) break; \
}
char inputs[5000000] = {0};
int list[PMAX + 1] = {0};
char str[10] = {'\n'};
char *inp = inputs;
inline int gc(void) {
register int c, n = 0;
do {
c = (int)*inp;
++inp;
} while ((c < '0') || (c > '9'));
do {
n = n * 10 + (c - '0');
c = (int)*inp;
++inp;
} while ((c >= '0') && (c <= '9'));
return n;
}
inline void pi(register int n) {
register int t, i = 1;
do {
t = n / 10;
str[i] = n - t * 10 + '0';
++i; n = t;
} while (n > 0);
do {
PC(str[--i]);
} while (i);
}
int main(void) {
register int j, i, u, sum, tmp;
int n, d, m, p, lm;
fread(inputs
, sizeof(char), 5000000, stdin
);
n = gc();
d = gc();
j = n + 1;
while (--j) {
++list[gc()];
}
++list[PMAX];
++list[9];
++list[0];
j = d + 1;
while (--j) {
i = (m = gc()) - 1;
u = 10;
tmp = 0;
while(!list[i]) --i;
if (i > 9) {
lm = m >> 1;
do {
if ((u = m - i) > i) break;
if (u > 9) {
if (i == u) {
if (list[i] > 1) {
SUMCHECK(tmp, sum, i, u, m);
} else {
--u;
while (!list[u]) --u;
if (u > 9) {
SUMCHECK(tmp, sum, i, u, m);
}
}
} else {
while (!list[u]) --u;
if (u > 9) {
SUMCHECK(tmp, sum, i, u, m);
}
}
}
--i;
while (!list[i]) --i;
} while (i >= lm);
}
pi(tmp);
}
return 0;
}
LyogcGFpemEgUE9IISBWb2wuMQogKiByZXN1bHQ6CiAqIGh0dHA6Ly9wLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5hLmpwL3B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EwICsgJzAnOwoJCSsraTsgbiA9IHQ7Cgl9IHdoaWxlIChuID4gMCk7CglkbyB7CgkJUEMoc3RyWy0taV0pOwoJfSB3aGlsZSAoaSk7Cn0KCmludCBtYWluKHZvaWQpIHsKICAgIHJlZ2lzdGVyIGludCBqLCBpLCB1LCBzdW0sIHRtcDsKICAgIGludCBuLCBkLCBtLCBwLCBsbTsKICAgIAogICAgZnJlYWQoaW5wdXRzLCBzaXplb2YoY2hhciksIDUwMDAwMDAsIHN0ZGluKTsKICAgIAogICAgbiA9IGdjKCk7CiAgICBkID0gZ2MoKTsKICAgIAogICAgaiA9IG4gKyAxOwogICAgd2hpbGUgKC0taikgewogICAgICAgICsrbGlzdFtnYygpXTsKICAgIH0KICAgIAogICAgKytsaXN0W1BNQVhdOwogICAgKytsaXN0WzldOwogICAgKytsaXN0WzBdOwogICAgaiA9IGQgKyAxOwogICAgd2hpbGUgKC0taikgewogICAgICAgIGkgPSAobSA9IGdjKCkpIC0gMTsKICAgICAgICB1ID0gMTA7CiAgICAgICAgdG1wID0gMDsKICAgICAgICB3aGlsZSghbGlzdFtpXSkgLS1pOwogICAgICAgIGlmIChpID4gOSkgewogICAgICAgIAlsbSA9IG0gPj4gMTsKICAgICAgICAJZG8gewogICAgICAgIAkJaWYgKCh1ID0gbSAtIGkpID4gaSkgYnJlYWs7CiAgICAgICAgCQlpZiAodSA+IDkpIHsKCSAgICAgICAgCQlpZiAoaSA9PSB1KSB7CgkgICAgICAgIAkJCWlmIChsaXN0W2ldID4gMSkgewoJICAgICAgICAJCQkJU1VNQ0hFQ0sodG1wLCBzdW0sIGksIHUsIG0pOwoJICAgICAgICAJCQl9IGVsc2UgewoJICAgICAgICAJCQkJLS11OwoJCSAgICAgICAgCQkJd2hpbGUgKCFsaXN0W3VdKSAtLXU7CgkJICAgICAgICAJCQlpZiAodSA+IDkpIHsKCQkgICAgICAgIAkJCQlTVU1DSEVDSyh0bXAsIHN1bSwgaSwgdSwgbSk7CgkJICAgICAgICAJCQl9CgkgICAgICAgIAkJCX0KCSAgICAgICAgCQl9IGVsc2UgewoJICAgICAgICAJCQl3aGlsZSAoIWxpc3RbdV0pIC0tdTsKCSAgICAgICAgCQkJaWYgKHUgPiA5KSB7CgkgICAgICAgIAkJCQlTVU1DSEVDSyh0bXAsIHN1bSwgaSwgdSwgbSk7CgkgICAgICAgIAkJCX0KCSAgICAgICAgCQl9CiAgICAgICAgCQl9CiAgICAgICAgCQktLWk7CiAgICAgICAgCQl3aGlsZSAoIWxpc3RbaV0pIC0taTsKICAgICAgICAJfSB3aGlsZSAoaSA+PSBsbSk7CiAgICAgICAgfQogICAgICAgIHBpKHRtcCk7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cgo=