//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define PRINT(c) cout<<(c)<<endl

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INF = 1000000;

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int p, q;
  while(cin>>p>>q, p){
	VS vs(p), target(q);
	REP(i,p) cin >> vs[i];
	REP(i,q) cin >> target[i];
	int cnt[16][3];
	int cnt2[16][3];
	VI in(p, 0);
	fill((int*)cnt, (int*)cnt+16*3, 0);
	fill((int*)cnt2, (int*)cnt2+16*3, 0);
	REP(i,p){
	  string& line = vs[i];
	  if(i > 0)
		REP(j,3)
		  cnt[i][j] = cnt[i-1][j];
	  int j;
	  for(j=0;j<SZ(line);++j)
		if(line[j] != '.') break;
		else ++in[i];
	  for(;j<SZ(line);++j)
		switch(line[j]){
		case '(': ++cnt[i][0]; break;
		case ')': --cnt[i][0]; break;
		case '{': ++cnt[i][1]; break;
		case '}': --cnt[i][1]; break;
		case '[': ++cnt[i][2]; break;
		case ']': --cnt[i][2]; break;
		}
	}
	REP(i,q){
	  string& line = target[i];
	  if(i > 0)
		REP(j,3)
		  cnt2[i][j] = cnt2[i-1][j];
	  for(int j=0;j<SZ(line);++j)
		switch(line[j]){
		case '(': ++cnt2[i][0]; break;
		case ')': --cnt2[i][0]; break;
		case '{': ++cnt2[i][1]; break;
		case '}': --cnt2[i][1]; break;
		case '[': ++cnt2[i][2]; break;
		case ']': --cnt2[i][2]; break;
		}
	}

	int tmp[3];
	VI ans(q, INF);
	FOR(r,1,21){
	  tmp[0] = r;
	  FOR(c,1,21){
		tmp[1] = c;
		FOR(s,1,21){
		  tmp[2] = s;
		  bool ok = true;
		  FOR(k,1,p){
			int ind = 0;
			REP(j,3)
			  ind += tmp[j]*cnt[k-1][j];
			if(in[k] != ind){
			  ok = false;
			  break;
			}
		  }

		  if(ok){
			ans[0] = 0;
			FOR(i,1,q){
			  int ind = 0;
			  REP(j,3)
				ind += tmp[j]*cnt2[i-1][j];
			  if(ans[i] == INF || ans[i] == ind)
				ans[i] = ind;
			  else
				ans[i] = -1;
			}
		  }
		}
	  }
	}
	REP(i,q) if(ans[i] == INF) ans[i] = -1;
	cout << ans[0];
	REP(i,q-1) cout << " " << ans[i+1];
	cout << endl;
  }

  return 0;
}