#include <iostream>
#include <vector>
#include <set>
using namespace std;

int n1, n2;
string t1[12], t2[12];
vector<int> R, C, S;

bool passed(int r, int c, int s){
  int rcnt = 0;
  int ccnt = 0;
  int scnt = 0;

  for(int i = 0; i < n1; i++){
    int ind = 0;
    for(int j = 0; t1[i][j] == '.'; j++) ind++;

    if(r * rcnt + c * ccnt + s * scnt != ind){
      return false;
    }

    for(int j = 0; j < t1[i].size(); j++){
      switch(t1[i][j]){
      case '(': rcnt++; break;
      case '{': ccnt++; break;
      case '[': scnt++; break;
      case ')': rcnt--; break;
      case '}': ccnt--; break;
      case ']': scnt--; break;
      }
    }
  }

  return true;
}

void solve(){
  R.clear();
  C.clear();
  S.clear();

  for(int r = 1; r <= 20; r++){
    for(int c = 1; c <= 20; c++){
      for(int s = 1; s <= 20; s++){
        if(passed(r, c, s)){
          R.push_back(r);
          C.push_back(c);
          S.push_back(s);
        }
      }
    }
  }

  int rcnt = 0;
  int ccnt = 0;
  int scnt = 0;

  for(int i = 0; i < n2; i++){
    int ind = -1;

    for(int j = 0; j < R.size(); j++){
      int tind = R[j] * rcnt + C[j] * ccnt + S[j] * scnt;
      if(ind == -1){
        ind = tind;
      }
      else if(ind != tind){
        ind = -1;
        break;
      }
    }

    if(i == 0){
      cout << 0;
    }
    else{
      cout << " " << ind;
    }

    for(int j = 0; j < t2[i].size(); j++){
      switch(t2[i][j]){
      case '(': rcnt++; break;
      case '{': ccnt++; break;
      case '[': scnt++; break;
      case ')': rcnt--; break;
      case '}': ccnt--; break;
      case ']': scnt--; break;
      }
    }
  }
  cout << endl;
}

int main(){
  while(cin >> n1 >> n2, n1){
    for(int i = 0; i < n1; i++){
      cin >> t1[i];
    }
    for(int i = 0; i < n2; i++){
      cin >> t2[i];
    }
    solve();
  }
}