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

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

struct rule{
  int a;
  int b;
  int c;
  int k;
};

int main(){
  while(1){
    int p,q;
    cin >> p >> q;
    if(p==0) return 0;
    vector<rule> r(p);
    string s;
    int a=0,b=0,c=0;
    rep(i,p){
      cin >> s;
      bool ok=true;
      int id=0;
      rep(j,s.length()){
        if(ok){
          if(s[j]=='.'){
            id++;
          }else{
            ok=false;
          }
        }
      }
      r[i]={a,b,c,id};
      rep(j,s.length()){
        if(s[j]=='('){
          a++;
        }else if(s[j]==')'){
          a--;
        }else if(s[j]=='{'){
          b++;
        }else if(s[j]=='}'){
          b--;
        }else if(s[j]=='['){
          c++;
        }else if(s[j]==']'){
          c--;
        }
      }
    }
    vector<rule> t(q);
    rep(i,q){
      cin >> s;
      bool ok=true;
      int id=0;
      t[i]={a,b,c,0};
      rep(j,s.length()){
        if(s[j]=='('){
          a++;
        }else if(s[j]==')'){
          a--;
        }else if(s[j]=='{'){
          b++;
        }else if(s[j]=='}'){
          b--;
        }else if(s[j]=='['){
          c++;
        }else if(s[j]==']'){
          c--;
        }
      }
    }
    vector<int> ans(q,-1);
    repp(x,1,21){
      repp(y,1,21){
        repp(z,1,21){
          rep(i,p){
            if(x*r[i].a+y*r[i].b+z*r[i].c!=r[i].k){
              goto next;
            }
          }
          rep(i,q){
            if(ans[i]==-1){
              ans[i]=x*t[i].a+y*t[i].b+z*t[i].c;
            }else{
              if(ans[i]!=x*t[i].a+y*t[i].b+z*t[i].c){
                ans[i]=-2;
              }
            }
          }
          next:;
        }
      }
    }
    rep(i,q){
      cout << (ans[i]>=0?ans[i]:-1);
      if(i<q-1) cout << " ";
      else cout << endl;
    }
  }
  return 0;
}

