#include <iostream>
#include <string>
#include <tuple>
#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);
        }

        // note: amount of indentation = "aoi"
        // count aoi and number of brackets
        auto processLine = [](const string& line) {
            auto aoi = 0;
            auto groupingNotFound = true;
            auto r = make_pair(0, 0); // round brackets
            auto c = make_pair(0, 0); // curly brackets
            auto s = make_pair(0, 0); // square brackets
            for (char i : line) {
                if (groupingNotFound && i == '.') {
                    aoi += 1;
                }
                else {
                    groupingNotFound = false;
                    if (i == '(')
                        r.first += 1;
                    else if (i == ')') {
                        r.second += 1;
                    }
                    else if (i == '{') {
                        c.first += 1;
                    }
                    else if (i == '}') {
                        c.second += 1;
                    }
                    else if (i == '[') {
                        s.first += 1;
                    }
                    else if (i == ']') {
                        s.second += 1;
                    }
                }
            }
            return make_tuple(aoi, r, c, s);
        };

        // compute well-indented aoi by triple and current number of brackets
        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);
        };

        // helper function to add pair
        auto addPair = [](auto& a, const auto& b) {
            a.first += b.first;
            a.second += b.second;
        };

        // compute possible triples
        auto computeParamCandidates = [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>> candidates;
            // all combinations
            for (int R = 1; R <= 20; ++R) {
                for (int C = 1; C <= 20; ++C) {
                    for (int S = 1; S <= 20; ++S) {
                        candidates.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 all candidates
                for (auto&& i : candidates) {
                    // if we can compute proper aoi with candidates i
                    if (computeAoi(i, r, c, s) == aoi) {
                        // keep as candidates
                        temp.push_back(move(i));
                    }
                }
                swap(candidates, temp);
                // the aoi determined by number of brackets in preceding lines
                // so we add them in the last of iteration
                addPair(r, cr);
                addPair(c, cc);
                addPair(s, cs);
            }
            return candidates;
        };

        // compute triple candidates by input P
        const auto candidates_ = computeParamCandidates(P);

        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) {
            auto aoiCandidate = computeAoi(candidates_[0], r, c, s);
            for (size_t i = 1; i < candidates_.size(); ++i) {
                auto temp = computeAoi(candidates_[i], r, c, s);
                // there are some possibilities of aoi in candidates
                // so return -1
                if (temp != aoiCandidate) {
                    return -1;
                }
            }
            return aoiCandidate;
        };

        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);
        }
        // pretty print
        for (size_t i = 0; i < results.size(); ++i) {
            cout << results[i];
            if (i != results.size() - 1) {
                cout << ' ';
            }
        }
        cout << endl;
    }
}


Measured memory_bytes: 3144