#include <bits/stdc++.h>
using namespace std;
using T = tuple<int, int, int, int>;

int main()
{
	int p, q;
	string s;
	while (cin >> p >> q, p | q) {
		vector<int> cnt(3);
		vector<T> base;
		for (int i = 0; i < p; i++) {
			cin >> s;
			int ind = 0;
			while (ind < (int)s.size() && s[ind] == '.') ind++;
			base.emplace_back(cnt[0], cnt[1], cnt[2], ind);
			for (auto c : s) {
				switch (c)
				{
				case '(': cnt[0]++; break;
				case ')': cnt[0]--; break;
				case '{': cnt[1]++; break;
				case '}': cnt[1]--; break;
				case '[': cnt[2]++; break;
				case ']': cnt[2]--; break;
				default:
					break;
				}
			}
		}
		vector<vector<int>> kouho;
		for (int r = 1; r <= 20; r++) {
			for (int c = 1; c <= 20; c++) {
				for (int s = 1; s <= 20; s++) {
					bool ok = true;
					for (auto t : base) {
						if (get<0>(t) * r + get<1>(t) * c + get<2>(t) * s != get<3>(t)) {
							ok = false;
						}
					}
					if (ok) {
						kouho.push_back({ r, c, s });
					}
				}
			}
		}
		cnt[0] = cnt[1] = cnt[2] = 0;
		vector<int> res(q);
		for (int i = 0; i < q; i++) {
			cin >> s;
			vector<int> tmp;
			for (auto t : kouho) {
				int x = 0;
				for (int j = 0; j < 3; j++) {
					x += t[j] * cnt[j];
				}
				tmp.push_back(x);
			}
			sort(tmp.begin(), tmp.end());
			tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
			res[i] = tmp.size() == 1 ? tmp[0] : -1;
			for (auto c : s) {
				switch (c)
				{
				case '(': cnt[0]++; break;
				case ')': cnt[0]--; break;
				case '{': cnt[1]++; break;
				case '}': cnt[1]--; break;
				case '[': cnt[2]++; break;
				case ']': cnt[2]--; break;
				default:
					break;
				}
			}
		}
		for (int i = 0; i < q; i++) {
			cout << res[i] << " \n"[i + 1 == q];
		}
	}
	return 0;
}