#include <bits/stdc++.h>
using namespace std;
 
constexpr int maxn = 5e3 + 10, maxc = 5e3 + 10;
 
char s[maxc] = {}, v[maxn][2 * maxc] = {};
int n, q, sol[2 * maxn] = {}, ord[2][2 * maxn] = {}, sumcif_init = 0, nrs[10], *starts[10];
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    cin >> n >> q;
    for(int i = 0; i < maxc; ++i)
        fill(v[i], v[i] + q, 9);
 
    for(int i = q; i < n + q; ++i){
        (cin >> ws).getline(s, maxc);
        for(int j = strlen(s) - 1, jj = 0; j >= 0; --j, ++jj)
            sumcif_init += v[jj][i] = s[j] - '0';
    }
 
    fill(sol, sol + q, sumcif_init);
 
    for(int i = 0; i < q; ++i){
        (cin >> ws).getline(s, maxc);
        for(int j = strlen(s) - 1, jj = 0; j >= 0; --j, ++jj)
            sol[i] += n * (s[j] - '0'),
            v[jj][i] = '9' - s[j];
    }
 
    for(int i = 0; i < n + q; ++i)
        ord[1][i] = n + q - 1 - i;
 
    for(int i = 0; i < maxc; ++i){
        fill(nrs, nrs + 10, 0);
        for(int j = 0; j < n + q; ++j)
            ++nrs[v[i][j]];
 
        starts[0] = ord[i & 1];
        for(int t = 1; t < 10; ++t)
            starts[t] = starts[t - 1] + nrs[t - 1];
 
        for(int j = 0; j < n + q; ++j)
            *starts[v[i][ord[~i & 1][j]]]++ = ord[~i & 1][j];
 
        for(int j = n + q - 1, nr_carries = 0; j >= 0; --j)
            if(ord[i & 1][j] >= q) ++nr_carries;
            else sol[ord[i & 1][j]] -= 9 * nr_carries;
    }
 
    for(int i = 0; i < q; ++i)
        cout << sol[i] << '\n';
}
