#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr,__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

struct Cond {
    int a, b, c;
    int v;
};

const double EPS = 1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;

vec gauss_jordan(const mat& A, const vec& b) {
    int n = A.size();
    mat B(n, vec(n+1));
    REP(i, n) REP(j, n) B[i][j] = A[i][j];
    REP(i, n) B[i][n] = b[i];

    REP(i, n) {
        int pivot = i;
        FOR(j, i, n) {
            if (abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
        }
        swap(B[i], B[pivot]);

        if (abs(B[i][i]) < EPS) return vec();

        FOR(j, i+1, n+1) B[i][j] /= B[i][i];
        REP(j, n) {
            if (i != j) {
                FOR(k, i+1, n+1) B[j][k] -= B[j][i] * B[i][k];
            }
        }
    }
    vec x(n);
    REP(i, n) x[i] = B[i][n];
    return x;
}

int main() {
    int p, q;
    while (cin >> p >> q, p|q) {
        vector<string> P(p);
        vector<string> Q(q);
        REP(i, p) cin >> P[i];
        REP(i, q) cin >> Q[i];

        vi level(3);
        vector<Cond> conds;
        REP(i, p) {
            int indent = 0;
            REP(j, P[i].length()) {
                if (indent != -1 && P[i][j] == '.') {
                    indent++;
                } else {
                    if (indent > 0) {
                        conds.push_back({level[0], level[1], level[2], indent});
//                        LOG("%d %d %d = %d\n", level[0], level[1], level[2], indent);
                    }
                    indent = -1;
                }
                switch (P[i][j]) {
                case '(':
                    level[0]++;
                    break;
                case ')':
                    level[0]--;
                    break;
                case '{':
                    level[1]++;
                    break;
                case '}':
                    level[1]--;
                    break;
                case '[':
                    level[2]++;
                    break;
                case ']':
                    level[2]--;
                    break;
                }
            }
        }

        vector<set<int>> var(3);

        FOR(i, 1, 21) {
            FOR(j, 1, 21) {
                FOR(k, 1, 21) {
                    bool ok = true;
                    for (Cond c : conds) {
                        if (i * c.a + j * c.b + k * c.c != c.v) {
                            ok = false;
                            break;
                        }
                    }
                    if (ok) {
                        var[0].insert(i);
                        var[1].insert(j);
                        var[2].insert(k);
                    }
                }
            }
        }

        level = vi(3);

        cout << 0;
        FOR(i, 0, q) {
            if (i > 0) {
                bool determined = true;
                int ans = 0;
                REP(j, 3) {
//                    LOG("==> %d\n", level[j]);
                    if (level[j] > 0) {
                        if (var[j].size() != 1) {
                            determined = false;
                            break;
                        }
//                        LOG("--> %d %d\n", level[j], (*var[j].begin()));
                        ans += level[j] * (*var[j].begin());
                    }
                }
                if (determined) {
                    cout << " " << ans;
                } else {
                    bool found = false;
                    for (Cond c : conds) {
                        if (c.a == level[0] && c.b == level[1] && c.c == level[2]) {
                            cout << " " << c.v;
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        cout << " -1";
                    }
                }
            }

            REP(j, Q[i].length()) {
                switch (Q[i][j]) {
                case '(':
                    level[0]++;
                    break;
                case ')':
                    level[0]--;
                    break;
                case '{':
                    level[1]++;
                    break;
                case '}':
                    level[1]--;
                    break;
                case '[':
                    level[2]++;
                    break;
                case ']':
                    level[2]--;
                    break;
                }
            }
        }
        cout << endl;
    }
}

Measured memory_bytes: 3168