#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }







vector<pair<char, int> > runlength(string str) {
    vector<pair<char, int> > ret;
    int cnt = 1;
    int n = sz(str);
    rep3(i, 1, n) {
        if (str[i] == str[i - 1]) {
            cnt++;
        } else {
            ret.eb(make_pair(str[i - 1], cnt));
            cnt = 1;
        }
    }
    ret.eb(make_pair(str[n - 1], cnt));
    return ret;
}




int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    
    int m, n;
    while (cin >> m && m) {
        cin >> n;
        string s[m], t[n];
        rep(i, m) cin >> s[i];
        rep(i, n) cin >> t[i];
        
        vector<vector<int> > par(m + 1, vector<int>(3));
        char ch[] = {'(', '{', '[', ')', '}', ']'};
        
        rep(i, m) {
            rep(j, sz(s[i])) {
                rep(k, 6) {
                    if (s[i][j] == ch[k]) {
                        k < 3 ? par[i + 1][k % 3]++ : par[i + 1][k % 3]--;
                        break;
                    }
                }
            }
        }
        rep(i, m) rep(j, 3) par[i + 1][j] += par[i][j];
        // cout << endl; rep(i, m) { rep(j, 3) cout << par[i][j] << " "; cout << endl; }
        
        vector<int> d(m);
        rep(i, m) {
            auto v = runlength(s[i]);
            if (v[0].F != '.') continue;
            d[i] = v[0].S;
        }
        // cout << endl; rep(i, m) cout << d[i] << " "; cout << endl;
        
        vector<tuple<int, int, int> > rcs;
        rep3(R, 1, 21) rep3(C, 1, 21) rep3(S, 1, 21) {
            int flg = 0;
            rep(i, m) {
                
                if (R * par[i][0] + C * par[i][1] + S * par[i][2] != d[i]) {
                    flg = 1;
                    break;
                }
            }
            if (flg == 0) {
                rcs.eb(tie(R, C, S));
            }
        }
        rep(i, sz(rcs)) {
            int aa, bb, cc;
            tie(aa, bb, cc) = rcs[i];
            // cout << aa << " " << bb << " " << cc << endl;
        }
        
        
        par.resize(n + 1, vector<int>(3));
        rep(i, n + 1) rep(j, 3) par[i][j] = 0;
        rep(i, n) {
            rep(j, sz(t[i])) {
                rep(k, 6) {
                    if (t[i][j] == ch[k]) {
                        k < 3 ? par[i + 1][k % 3]++ : par[i + 1][k % 3]--;
                        break;
                    }
                }
            }
        }
        rep(i, n) rep(j, 3) par[i + 1][j] += par[i][j];
        // rep(i, n) { rep(j, 3) cout << par[i][j] << " "; cout << endl; }
        
        vector<int> ans;
        ans.eb(0);
        rep3(i, 1, n) {
            vector<int> tmp;
            rep(j, sz(rcs)) {
                int aa, bb, cc;
                tie(aa, bb, cc) = rcs[j];
                tmp.eb(aa * par[i][0] + bb * par[i][1] + cc * par[i][2]);
            }
            sort(all(tmp));
            tmp.erase(unique(all(tmp)), tmp.end());
            if (sz(tmp) == 1) ans.eb(tmp[0]);
            else ans.eb(-1);
        }
        
        rep(i, n) { if (i) cout << " "; cout << ans[i]; } cout << endl;
    }
}





Measured memory_bytes: 3268