#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;


using Item = tuple<int,int,int>;
map<Item,int> P2N;
map<char,int> C2I = {{'(',0},{'[',1},{'{',2},{')',0},{']',1},{'}',2}};
string ParF = "([{",ParE = ")]}";

void sort_unique(vector<int>& V){
    if(V.empty()) return;
    sort(V.begin(),V.end());
    V.erase(unique(V.begin(),V.end()),V.end());
}
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll p,q;
    while(cin >> p >> q, p){
        P2N.clear();
        vector<string> Qs(q),Ps(p);
        for(auto& in:Ps) cin >> in;
        for(auto& in:Qs) cin >> in;
        
        int RCS[3] = {0,0,0};
        for(int i = 0; i < p; i++){
            int num = 0;
            while(Ps[i][num] == '.') num++;
            
            P2N[Item(RCS[0],RCS[1],RCS[2])] = num;
            
            for(int j = num; j < Ps[i].length(); j++){
                if(ParF.find(Ps[i][j]) != string::npos){
                    RCS[C2I[Ps[i][j]]]++;
                    continue;
                }
                if(ParE.find(Ps[i][j]) != string::npos){
                    RCS[C2I[Ps[i][j]]]--;
                    continue;
                }
            }
        }
        
        vector<int> Rcand,Ccand,Scand;
        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(auto p:P2N){
                        int Rn,Cn,Sn,num;
                        tie(Rn,Cn,Sn) = p.first;
                        num = p.second;
                        
                        if(Rn + Cn + Sn == 0) continue;
                        if(Rn * R + Cn * C + Sn * S == num) continue;
                        ok = false;
                        break;
                    }
                    if(!ok) continue;
                    Rcand.push_back(R);
                    Ccand.push_back(C);
                    Scand.push_back(S);
                }
            }
        }
        
        sort_unique(Rcand);
        sort_unique(Ccand);
        sort_unique(Scand);
        
        int DetectedRCS[3] = {-1,-1,-1};
        if(Rcand.size() == 1) DetectedRCS[0] = Rcand.front();
        if(Ccand.size() == 1) DetectedRCS[1] = Ccand.front();
        if(Scand.size() == 1) DetectedRCS[2] = Scand.front();
        
        
        vector<int> ans(q);
        RCS[0] = RCS[1] = RCS[2] = 0;
        for(int i = 0; i < q; i++){
            
            if(RCS[0] + RCS[1] + RCS[2] == 0){
                ans[i] = 0;
            }else{
                if(P2N.count(Item(RCS[0],RCS[1],RCS[2]))){
                    ans[i] = P2N[Item(RCS[0],RCS[1],RCS[2])];
                }else{
                    bool none = false;
                    for(int j = 0; j < 3;j++){
                        if(DetectedRCS[j] * RCS[j] < 0){
                            ans[i] = -1;
                            none = true;
                            break;
                        }else{
                            ans[i] += DetectedRCS[j] * RCS[j];
                        }
                    }
                }
            }
            for(int j = 0; j < Qs[i].length(); j++){
                if(ParF.find(Qs[i][j]) != string::npos){
                    RCS[C2I[Qs[i][j]]]++;
                    continue;
                }
                if(ParE.find(Qs[i][j]) != string::npos){
                    RCS[C2I[Qs[i][j]]]--;
                    continue;
                }
            }
        }
        for(int i = 0; i < q;i++){
            cout << ans[i];
            if(i==q-1) cout << endl;
            else cout << " ";
        }
    }
    return 0;
}



Measured memory_bytes: 3248