#include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

void solve(int p,int q){
    vector<string> a(p),b(q);
    rep(i,0,p) cin >> a[i];
    rep(i,0,q) cin >> b[i];
    vector<vector<int>> correct;
    rep(r,1,21){
        rep(c,1,21){
            rep(s,1,21){
                vector<int> count={0,0,0};
                bool ok=true;
                rep(i,0,p){
                    rep(j,0,a[i].size()){
                        if(a[i][j]!='.'){
                            if(j!=count[0]*r+count[1]*c+count[2]*s) ok=false;
                            break;                       
                        }
                    }
                    if(!ok) break;
                    rep(j,0,a[i].size()){
                        if(a[i][j]=='(') ++count[0];
                        if(a[i][j]=='{') ++count[1];
                        if(a[i][j]=='[') ++count[2];
                        
                        if(a[i][j]==')') --count[0];
                        if(a[i][j]=='}') --count[1];
                        if(a[i][j]==']') --count[2];                                 
                    }
                }
                if(ok) correct.push_back({r,c,s});
            }
        }
    }
    
    vector<int> count={0,0,0};
    rep(i,0,q){
        int indent;
        set<int> st;
        rep(j,0,correct.size()){
            int tmp=0;
            rep(k,0,3) tmp+=correct[j][k]*count[k];
            indent=tmp;
            st.insert(tmp);
        }
        
        if(st.size()>=2) cout << -1;
        else cout << indent;
        
        if(i!=q-1) cout << " ";
        else cout << endl;
        
        rep(j,0,b[i].size()){
            if(b[i][j]=='(') ++count[0];
            if(b[i][j]=='{') ++count[1];
            if(b[i][j]=='[') ++count[2];
            
            if(b[i][j]==')') --count[0];
            if(b[i][j]=='}') --count[1];
            if(b[i][j]==']') --count[2];                                 
        }
    }
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    for(;;){
        int p,q;
        cin >> p >> q;
        if(!p and !q) break;
        solve(p,q);
    }
    return 0;
}