#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;

typedef tuple<int,int,int,int> T;

int cnt(string s){
    int ret=0;
    for(auto c:s){
        if(c=='.') ret++;
        else break;
    }
    return ret;
}

int main(){
    int P,Q;
    while(cin>>P>>Q,P){
        vector<T> V;
        for(int i=0,r=0,c=0,s=0;i<P;i++){
            string str;
            cin>>str;
            if(cnt(str))V.emplace_back(cnt(str),r,c,s);
            // cout<<cnt(str)<<' '<<r<<' '<<c<<' '<<s<<' '<<str<<endl;
            for(auto cc:str){
                switch(cc){
                    case '(':
                        r++;
                        break;
                    case ')':
                        r--;
                        break;
                    case '{':
                        c++;
                        break;
                    case '}':
                        c--;
                        break;
                    case '[':
                        s++;
                        break;
                    case ']':
                        s--;
                        break;
                    default:
                        break;
                }
            }
        }
        for(auto t:V){
            int a,b,c,d;
            tie(a,b,c,d)=t;
            // cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
        }
        bool bR[21]={},bC[21]={},bS[21]={};
        // fill(bS,bS+21,false);
        for(int R=1;R<=20;R++){
            for(int C=1;C<=20;C++){
                for(int S=1;S<=20;S++){
                    bool flag = true;
                    for(auto t:V){
                        int cnt,r,c,s;
                        tie(cnt,r,c,s)=t;
                        if(R*r+C*c+S*s!=cnt){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        bR[R]=bC[C]=bS[S]=true;
                        // cout<<"R: "<<R<<" C: "<<C<<" S: "<<S<<endl;
                    }
                }
            }
        }
        int R,C,S,cnt;
        cnt=0;
        for(int i=1;i<=20;i++){
            if(bR[i]){
                R=i;
                cnt++;
            }
        }
        if(cnt!=1) R=-1;
        cnt=0;
        for(int i=1;i<=20;i++){
            if(bC[i]){
                C=i;
                cnt++;
            }
        }
        if(cnt!=1) C=-1;
        cnt=0;
        for(int i=1;i<=20;i++){
            if(bS[i]){
                S=i;
                cnt++;
            }
        }
        if(cnt!=1) S=-1;
        // cout<<"R: "<<R<<" C: "<<C<<" S: "<<S<<endl;
        for(int i=0,r=0,s=0,c=0;i<Q;i++){
            // cout<<"r: "<<r<<" s: "<<s<<" c: "<<c<<endl;
            if(i)cout<<' ';
            if((R==-1&&r)||(S==-1&&s)||(C==-1&&c)){
                int d=-1;
                for(auto t:V){
                    int dot,rr,cc,ss;
                    tie(dot,rr,cc,ss)=t;
                    //???????????????????????????
                    if((rr*r+ss*s+cc*c)*(rr*r+ss*s+cc*c)==(rr*rr+ss*ss+cc*cc)*(r*r+s*s+c*c)){
                        if(rr){
                            d=round(double(r)/rr*dot);
                        }else if(ss){
                            d=round(double(s)/ss*dot);
                        }else if(cc){
                            d=round(double(c)/cc*dot);
                        }else{
                            d=0;
                        }
                        break;
                    }
                }
                cout<<d;
            }else{
                cout<<R*r+S*s+C*c;
            }
            // cout<<endl;
            string str;
            cin>>str;
            for(auto cc:str){
                switch(cc){
                    case '(':
                        r++;
                        break;
                    case ')':
                        r--;
                        break;
                    case '{':
                        c++;
                        break;
                    case '}':
                        c--;
                        break;
                    case '[':
                        s++;
                        break;
                    case ']':
                        s--;
                        break;
                    default:
                        break;
                }
            }
        }
        cout<<endl;
    }

    return 0;
}

Measured memory_bytes: 3152