#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"

map<char, int>mp;
pair<int, vector<int>>check(const string st) {
	int a = 0;
	int num = 0;
	vector<int>kakkos(3);
	while (st[a] == '.') {
		num++;
		a++;
	}
	while (a != st.size()) {
		if (mp[st[a]] <= 12 && mp[st[a]] >= 10) {
			kakkos[mp[st[a]] - 10]++;
		}
		else if (mp[st[a]] <= 22 && mp[st[a]] >= 20) {
			kakkos[mp[st[a]] - 20]--;
		}
		a++;
	}
	return make_pair(num, kakkos);
}

/* verified AOJ1327 One-Dimensional Cellular Automaton
~Matrix~
??????????°????????¨?????????§??????
vector iostream cmath cassert ??????
Matrix a(VV), b(VV);
?????????:a*b
????????????:l*a
???:a+b
???:a-b
??¢???:a.transport()
?????????:a.cofactor()
?????????:a.det()
a???i???j??????:a.get(i,j)???
a???i??????j?????????k?????£??\:set(i,j,k)
????????????a[i][j] (a[i][j]=k)
a???x???:a.pow(x)
n*n?????¬??????E:Matrix(n)
m*n 0??????:Matrix(m,n)
m*n ??¨????´????p?????????:Matrix(m,n,p)
Row???row??§????????????(m*1??????):Matrix(row)
**(??´??°??????)**
????§????:a.triangulate()
?????????:a.rank()
?????????:a.inverse()
//(????????????a.det()?????????a.pre_inverse()??§?±??????? ??´??°???)
??¬??????????¶???????:a.rowReduction()
//??£???????¬?????¨????????§£??????
***
*/

#include <vector>
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

typedef int Elem;
typedef vector<Elem> Row;
typedef vector<Row> VV;

typedef long double ld;
const ld EPS = 1e-11;

const bool isZero(const Elem e) {
	return abs(e) < EPS;
}


int main() {
	mp['('] = 10;
	mp['{'] = 11;
	mp['['] = 12;
	mp[')'] = 20;
	mp['}'] = 21;
	mp[']'] = 22;
	while (1) {
		int P, Q; cin >> P >> Q;
		if (!P)break;
		vector<vector<int>>nowkakkos;
		vector<int>nums;
		vector<int>kakkos(3);
		for (int i = 0; i < P; ++i) {
			nowkakkos.push_back(kakkos);
			string st; cin >> st;
			auto p(check(st));
			nums.push_back(p.first);
			for (int j = 0; j < 3; ++j) {
				kakkos[j] += p.second[j];
			}
		}
		
		set<vector<int>>sets;
		{
			vector<int>ams(3);
			for (ams[0] = 1; ams[0] <= 20; ++ams[0]) {
				for (ams[1] = 1; ams[1] <= 20; ++ams[1]) {
					for (ams[2] = 1; ams[2] <= 20; ++ams[2]) {
						bool aok = true;
						for (int i = 0; i < P; ++i) {
							int sum = 0;
							for (int j = 0; j < 3; ++j) {
								sum += nowkakkos[i][j] * ams[j];
							}
							if (sum != nums[i])aok = false;
						}
						if (aok)sets.emplace(ams);
					}
				}
			}
		}
		{
			vector<int>nkakkos(3);
			for (int i = 0; i < Q; ++i) {
				
				
				set<int>anss;
				for (auto s : sets) {
						int sum = 0;
						for (int j = 0; j < 3; ++j) {
							sum += nkakkos[j] * s[j];
						}
						anss.emplace(sum);
					
				}
				if (anss.size() != 1) {
					cout << -1;
				}
				else {
					cout <<* anss.begin();
				}
				if (i != Q - 1)cout << " ";
				else cout << endl;
				{
					string st; cin >> st;
					auto p(check(st));
					for (int j = 0; j < 3; ++j) {
						nkakkos[j] += p.second[j];
					}
				}
			}
		}
		
	}
	return 0;
}

Measured memory_bytes: 3884