#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << '(' << p.first << ", " << p.second << ')';
	return os;
}

template<class Tuple, unsigned Index>
void print_tuple(ostream& os, const Tuple& t) {}

template<class Tuple, unsigned Index, class Type, class... Types>
void print_tuple(ostream& os, const Tuple& t) {
	if(Index > 0) os << ", ";
	os << get<Index>(t);
	print_tuple<Tuple, Index + 1, Types...>(os, t);
}

template<class... Types>
ostream& operator<<(ostream& os, const tuple<Types...>& t) {
	os << '(';
	print_tuple<tuple<Types...>, 0, Types...>(os, t);
	return os << ')';
}

template<class Iterator>
ostream& dump_range(ostream&, Iterator, const Iterator&);

template<typename T>
ostream& operator<<(ostream& os, vector<T> c) {
	return dump_range(os, c.cbegin(), c.cend());
}

template<class Iterator>
ostream& dump_range(ostream& os, Iterator first, const Iterator& last) {
	os << '[';
	for(int i = 0; first != last; ++i, ++first) {
		if(i) os << ", ";
		os << *first;
	}
	return os << ']';
}

const int MAX_RCS = 20;

int calc_indent(const string& s) {
	for(int i = 0; i < static_cast<int>(s.size()); ++i) {
		if(s[i] != '.') return i;
	}

	return s.size();
}

void calc_nest(int& nest_r, int& nest_c, int& nest_s, const string& line) {
	for(int i = 0; i < static_cast<int>(line.size()); ++i) {
		switch(line[i]) {
		case '(': ++nest_r; break;
		case ')': --nest_r; break;
		case '{': ++nest_c; break;
		case '}': --nest_c; break;
		case '[': ++nest_s; break;
		case ']': --nest_s; break;
		}
	}
}

bool ok(int r, int c, int s, const map<tuple<int, int, int>, int>& conditions) {
	for(const pair<tuple<int, int, int>, int>&e : conditions) {
		const tuple<int, int, int>& nest = e.first;
		const int indent = r * get<0>(nest) + c * get<1>(nest) + s * get<2>(nest);
		if(indent != e.second) return false;
	}

	return true;
}

inline int gcd(int a, int b, int c) {
	return __gcd(a, __gcd(b, c));
}

int get_value(int r, int c, int s, const map<tuple<int, int, int>, int>& conditions) {
	const int g = max(1, gcd(r, c, s));
	const tuple<int, int, int> t(r / g, c / g, s / g);
	if(conditions.count(t))
		return g * conditions.at(t);
	return -1;
}

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

	for(int p, q; cin >> p >> q && p;) {
		map<tuple<int, int, int>, int> conditions;
		int nest_r = 0, nest_c = 0, nest_s = 0;
		for(int i = 0; i < p; ++i) {
			string line;
			cin >> line;
			const int g = max(1, gcd(nest_r, nest_c, nest_s));
			conditions[make_tuple(nest_r / g, nest_c / g, nest_s / g)] = calc_indent(line) / g;
			calc_nest(nest_r, nest_c, nest_s, line);
		}

		unordered_set<int> R, C, S;
		for(int r = 1; r <= MAX_RCS; ++r) {
			for(int c = 1; c <= MAX_RCS; ++c) {
				for(int s = 1; s <= MAX_RCS; ++s) {
					if(ok(r, c, s, conditions)) {
						R.insert(r);
						C.insert(c);
						S.insert(s);
					}
				}
			}
		}

		nest_r = 0, nest_c = 0, nest_s = 0;
		vector<int> ans(q);
		for(int i = 0; i < q; ++i) {
			string line;
			cin >> line;
			const int indent = calc_indent(line);

			if(!((nest_r && R.size() != 1) ||
					  (nest_c && C.size() != 1) ||
					  (nest_s && S.size() != 1))) {
				ans[i] = abs(nest_r * (*R.begin()) + nest_c * (*C.begin()) + nest_s * (*S.begin()) - indent);
			}
			else if(ans[i] = get_value(nest_r, nest_c, nest_s, conditions), ans[i] != -1) {
				ans[i] = abs(ans[i] - indent);
			}
			else if(S.size() == 1 && (ans[i] = get_value(nest_r, nest_c, 0, conditions), ans[i] != -1)) {
				ans[i] = abs(ans[i] + (*S.begin()) - indent);
			}
			else if(C.size() == 1 && (ans[i] = get_value(nest_r, 0, nest_s, conditions), ans[i] != -1)) {
				ans[i] = abs(ans[i] + (*C.begin()) - indent);
			}
			else if(R.size() == 1 && (ans[i] = get_value(0, nest_c, nest_s, conditions), ans[i] != -1)) {
				ans[i] = abs(ans[i] + (*R.begin()) - indent);
			}
			calc_nest(nest_r, nest_c, nest_s, line);
		}

		for(int i = 0; i < q; ++i) {
			cout << ans[i] << (i == q - 1 ? "\n" : " ");
		}
	}

	return EXIT_SUCCESS;
}

Measured memory_bytes: 1224