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

int main() {
    int p, q;
    while (cin >> p >> q, p) {
        vector<vector<int>> a(p, vector<int>(3));
        vector<int> b(p);
        {
            map<char, int> cnt;
            for (int i = 0; i < p; i++) {
                a[i][0] = cnt['('] - cnt[')'];
                a[i][1] = cnt['{'] - cnt['}'];
                a[i][2] = cnt['['] - cnt[']'];
                string s; cin >> s;
                int& ind = b[i];
                while (ind < s.size() && s[ind] == '.') ind++;
                for (auto c: s) cnt[c]++;
            }
        }
        set<tuple<int, int, int>> se;
        for (int r = 1; r <= 20; r++) {
            for (int c = 1; c <= 20; c++) {
                for (int s = 1; s <= 20; s++) {
                    bool ok = true;
                    for (int i = 0; i < p; i++) {
                        ok &= a[i][0] * r + a[i][1] * c + a[i][2] * s == b[i];
                    }
                    if (ok) se.emplace(r, c, s);
                }
            }
        }
        vector<int> res(q); {
            map<char, int> cnt;
            for (int i = 0; i < q; i++) {
                set<int> ans;
                for (auto &t: se) {
                    int tmp = 0;
                    tmp += (cnt['('] - cnt[')']) * get<0>(t);
                    tmp += (cnt['{'] - cnt['}']) * get<1>(t);
                    tmp += (cnt['['] - cnt[']']) * get<2>(t);
                    ans.emplace(tmp);
                }
                res[i] = ans.size() == 1 ? *ans.begin() : -1;
                string s; cin >> s;
                for (auto c: s) cnt[c]++;
            }
        }
        for (int i = 0; i < q; i++) cout << res[i] << " \n"[i == q - 1];
    }
    return 0;
}
