#include <iostream>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int p, q;
        cin >> p >> q;
        if (p == 0 && q == 0) {
            return 0;
        }

        vector<string> P;
        for (int i = 0; i < p; ++i) {
            string a;
            cin >> a;
            P.push_back(a);
        }

        auto processLine = [](const string& line) {
            auto aoi = 0;
            auto groupingNotFound = true;
            auto cr = make_pair(0, 0);
            auto cc = make_pair(0, 0);
            auto cs = make_pair(0, 0);
            for (char i : line) {
                if (groupingNotFound && i == '.') {
                    aoi += 1;
                }
                else {
                    groupingNotFound = false;
                    if (i == '(')
                        cr.first += 1;
                    else if (i == ')') {
                        cr.second += 1;
                    }
                    else if (i == '{') {
                        cc.first += 1;
                    }
                    else if (i == '}') {
                        cc.second += 1;
                    }
                    else if (i == '[') {
                        cs.first += 1;
                    }
                    else if (i == ']') {
                        cs.second += 1;
                    }
                }
            }
            return make_tuple(aoi, cr, cc, cs);
        };
        auto computeAoi = [](const tuple<int, int, int>& param,
                             const pair<int, int>& r, const pair<int, int>& c,
                             const pair<int, int>& s) {
            int R, C, S;
            tie(R, C, S) = param;
            return R * (r.first - r.second) + C * (c.first - c.second) +
                   S * (s.first - s.second);
        };
        auto addPair = [](auto& a, const auto& b) {
            a.first += b.first;
            a.second += b.second;
        };

        auto candidates = [processLine, computeAoi,
                           addPair](const vector<string>& v) {
            auto r = make_pair(0, 0);
            auto c = make_pair(0, 0);
            auto s = make_pair(0, 0);
            vector<tuple<int, int, int>> remainCandidates;
            for (int R = 1; R <= 20; ++R) {
                for (int C = 1; C <= 20; ++C) {
                    for (int S = 1; S <= 20; ++S) {
                        remainCandidates.emplace_back(R, C, S);
                    }
                }
            }
            for (auto&& line : v) {
                int aoi;
                pair<int, int> cr, cc, cs;
                tie(aoi, cr, cc, cs) = processLine(line);

                vector<tuple<int, int, int>> temp;
                for (auto&& j : remainCandidates) {
                    if (computeAoi(j, r, c, s) == aoi) {
                        temp.push_back(move(j));
                    }
                }
                swap(remainCandidates, temp);
                addPair(r, cr);
                addPair(c, cc);
                addPair(s, cs);
            }
            return remainCandidates;
        };
        const auto candidates_ = candidates(P);
        auto normalize = [](const vector<tuple<int, int, int>>& v) {
            tuple<unordered_set<int>, unordered_set<int>, unordered_set<int>> n;
            unordered_set<int> r, c, s;
            tie(r, c, s) = n;
            for (auto&& i : v) {
                int rr, cc, ss;
                tie(rr, cc, ss) = i;
                r.insert(rr);
                c.insert(cc);
                s.insert(ss);
            }
            return n;
        };
        const auto nn = normalize(candidates_);

        vector<string> Q;
        for (int i = 0; i < q; ++i) {
            string a;
            cin >> a;
            Q.push_back(a);
        }

        auto computeAoiByCandidates = [&candidates_,
                                       computeAoi](const pair<int, int>& r,
                                                   const pair<int, int>& c,
                                                   const pair<int, int>& s) {
            bool first = true;
            int current = -1;
            for (auto&& candidate : candidates_) {
                if (first) {
                    current = computeAoi(candidate, r, c, s);
                    first = false;
                }
                else {
                    auto r_ = computeAoi(candidate, r, c, s);
                    if (r_ != current) {
                        return -1;
                    }
                    else {
                        current = r_;
                    }
                }
            }
            return current;
        };

        auto r = make_pair(0, 0);
        auto c = make_pair(0, 0);
        auto s = make_pair(0, 0);
        vector<int> results;
        for (auto&& line : Q) {
            int aoi;
            pair<int, int> cr, cc, cs;
            tie(aoi, cr, cc, cs) = processLine(line);
            results.push_back(computeAoiByCandidates(r, c, s));
            addPair(r, cr);
            addPair(c, cc);
            addPair(s, cs);
        }
        for (size_t i = 0; i < results.size(); ++i) {
            cout << results[i];
            if (i == results.size() - 1) {
                cout << endl;
            }
            else {
                cout << " ";
            }
        }
    }
}


Measured memory_bytes: 3284