#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define MAX 20
using namespace std;
int p,q;
string P[MAX],Q[MAX];
int leading[2][MAX];
 
bool check(int R,int C,int S)
{
  int r,c,s;
  r = c = s = 0;
  rep(i,p)
    {
      //cout << leading[0][i] << "  = " <<R*r + c*C + s*S << endl;
      if(leading[0][i] != R*r + c*C + s*S)return false;
 
      rep(j,P[i].size())
    {
      if(P[i][j] == '(')r++;
      else if(P[i][j] == '{')c++;
      else if(P[i][j] == '[')s++;
       
      if(P[i][j] == ')')r--;
      else if(P[i][j] == '}')c--;
      else if(P[i][j] == ']')s--;
    }
    }
  return true;
}
 
int main()
{
  while(cin >> p >> q,p|q)
    {
      vector<int> Rv,Cv,Sv;
      rep(i,p)
    {
      cin >> P[i];
      int cnt = 0;
      rep(j,P[i].size())
        {
          if(P[i][j] == '.')cnt++;
          else              break;
        }
      leading[0][i] = cnt;
    }
 
      rep(i,q)
    {
      cin >> Q[i];
      int cnt = 0;
      rep(j,Q[i].size())
        {
          if(Q[i][j] == '.')cnt++;
          else              break;
        }
      leading[1][i] = cnt;
    }
 
      //check(9,5,2);
      //return 0;
      REP(R,1,21)
    {
      REP(C,1,21)
        {
          REP(S,1,21)
        {
          if(check(R,C,S))
            {
              Rv.push_back(R);
              Cv.push_back(C);
              Sv.push_back(S);
            }
        }
        }
    }
 
      int totalCost = 0;
      int r,s,c;
      r = s = c = 0;
      rep(i,q)
    {
      set<int> st;
      rep(j,Rv.size())
        {
          totalCost = r*Rv[j] + c*Cv[j] + s*Sv[j];
          st.insert(totalCost);
        }
 
      if(st.empty() || (int)st.size() >= 2)
        {
          cout << -1;
        }
      else
        {
          set<int>::iterator it = st.begin();
          cout << (*it);
        }
      if(i == q-1)cout << endl;
      else cout << " ";
 
      rep(j,Q[i].size())
        {
          if(Q[i][j] == '(')r++;
          else if(Q[i][j] == '{')c++;
          else if(Q[i][j] == '[')s++;
           
          if(Q[i][j] == ')')r--;
          else if(Q[i][j] == '}')c--;
          else if(Q[i][j] == ']')s--;
        }
     
    }
    }
  return 0;
}