#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <set>
#include <map>
using namespace std;

struct data {
    int r, c, s, i;
    bool operator<(data const& d) {
        return r < d.r;
    }
};

int main() {
    int p, q;
    while(cin >> p >> q, p) {
        vector<data> v(p);
        data d = {};
        map<tuple<int, int, int>, int> res2;
        for(int i=0; i<p; ++i) {
            string s;
            cin >> s;
            bool is_in = true;
            for(int j=0; j<s.size(); ++j) {
                if(s[j] == '.' && is_in) {
                    d.i++;
                } else {
                    if(is_in) {
                        is_in = false;
                        v[i] = d;
                        res2[make_tuple(d.r, d.c, d.s)] = d.i;
                        d.i = 0;
                    }
                    if(s[j] == '(') {
                        d.r++;
                    } else if(s[j] == '{') {
                        d.c++;
                    } else if(s[j] == '[') {
                        d.s++;
                    } else if(s[j] == ')') {
                        d.r--;
                    } else if(s[j] == ']') {
                        d.s--;
                    } else if(s[j] == '}') {
                        d.c--;
                    }
                }
            }
        }
        vector<tuple<int, int, int>> res;
        for(int r=1; r<=20; ++r) {
            for(int c=1; c<=20; ++c) {
                for(int s=1; s<=20; ++s) {
                    auto check = [r, c, s](data const& d) -> bool {
                        return d.r * r + d.c * c + d.s * s == d.i;
                    };
                    if(all_of(v.begin(), v.end(), check)) {
                        res.push_back(make_tuple(r, c, s));
                    }
                }
            }
        }
        set<int> sr, sc, ss;
        for(int i=0; i<res.size(); ++i) {
            int r, c, s;
            tie(r, c, s) = res[i];
            sr.insert(r);
            sc.insert(c);
            ss.insert(s);
        }
        const int R = (sr.size() == 1 ? *sr.begin() : -1);
        const int C = (sc.size() == 1 ? *sc.begin() : -1);
        const int S = (ss.size() == 1 ? *ss.begin() : -1);
        d.r = d.c = d.s = 0;
        //cout << R << " " << C << " " << S << endl;
        for(int i=0; i<q; ++i) {
            string s;
            cin >> s;
            //cout << "dr: " << d.r << "  dc: " << d.c << "   ds: " << d.s << endl;
            if(d.r > 0 && R == -1 || d.c > 0 && C == -1 || d.s > 0 && S == -1) {
                auto t = make_tuple(d.r, d.c, d.s);
                if(res2.count(t) == 1) {
                    cout << res2[t];
                } else {
                    cout << -1;
                }
            } else {
                int res = 0;
                if(d.r > 0 && R != -1) {
                    res += d.r * R;
                }
                if(d.c > 0 && C != -1) {
                    res += d.c * C;
                }
                if(d.s > 0 && S != -1) {
                    res += d.s * S;
                }
                cout << res;
            }
            if(i != q-1) {
                cout << " ";
            } else {
                cout << endl;
            }
            for(int j=0; j<s.size(); ++j) {
                if(s[j] == '(') {
                    d.r++;
                } else if(s[j] == '{') {
                    d.c++;
                } else if(s[j] == '[') {
                    d.s++;
                } else if(s[j] == ')') {
                    d.r--;
                } else if(s[j] == ']') {
                    d.s--;
                } else if(s[j] == '}') {
                    d.c--;
                }
            }
        }
    }
}


Measured memory_bytes: 3216