#include <bits/stdc++.h>

using namespace std;

int main()
{
	int N , M;
	while(cin >> N >> M , N || M)
	{
		int a = 0 , b = 0 , c = 0 , d = 0;
		map<vector<int> , int>data;
		for(size_t i = 0; i < N; i++)
		{
			string S;
			cin >> S;
			int now = 0;
			while(now < S.size() && S[now] == '.')
			{
				now++;
				d++;
			}
			int memo = 1;
			for(size_t i = 1; i < 20; i++)
			{
				if(a%i == 0 && b%i == 0 && c%i == 0 && d%i == 0)
				{
					memo = i;
				}
			}
			vector<int>memo2 = { a / memo,b / memo,c / memo };
			data[memo2] = d / memo;

			d = 0;
			while(now < S.size())
			{
				switch(S[now])
				{
					case '(':a++; break;
					case ')':a--; break;
					case '{':b++; break;
					case '}':b--; break;
					case '[':c++; break;
					case ']':c--; break;
					default:
						break;
				}
				now++;
			}
		}/*
		cout << "!" << endl;
		for(auto x : data)
		{
			cout << x.first[0] << " " << x.first[1] << " " << x.first[2] << " " << " " << x.second << endl;
		}*/


		//vector<vector<vector<bool>>>candidate(20 , vector<vector<bool>>(20 , vector<bool>(20)));
		vector<vector<int>>xx;
		for(int i = 1; i <= 20; i++)
		{
			for(int j = 1; j <= 20; j++)
			{
				for(int k = 1; k <= 20; k++)
				{
					bool f = 1;
					for(auto x : data)
					{
						if(x.first[0] * i + x.first[1] * j + x.first[2] * k != x.second)
						{
							f = 0;
							break;
						}
					}
					//candidate[i-1][j-1][k-1]=f;
					if(f)xx.push_back(vector<int>{i - 1 , j - 1 , k - 1});
					//if(f)cout << i << " " << j << " " << k << "  " << f << endl;
				}
			}
		}
		/*
		for(auto x : xx)
		{
			for(auto y : x)
			{
				cout << y + 1 << " ";
			}
			cout << endl;
		}*/
		vector<set<int>>ans(M);
		for(size_t i = 0; i < M; i++)
		{
			string S;
			cin >> S;
			int now = 0;
			while(now < S.size() && S[now] == '.')
			{
				now++;
				d++;
			}
			for(auto x : xx)
			{
				ans[i].insert(( x[0] + 1 )*a + ( x[1] + 1 )*b + ( x[2] + 1 )*c);
			}

			d = 0;
			while(now < S.size())
			{
				switch(S[now])
				{
					case '(':a++; break;
					case ')':a--; break;
					case '{':b++; break;
					case '}':b--; break;
					case '[':c++; break;
					case ']':c--; break;
					default:
						break;
				}
				now++;
			}
		}
		for(size_t i = 0; i < M; i++)
		{
			if(ans[i].size() == 1)
			{
				for(auto x : ans[i])
				{
					cout << x;
				}
			} else
			{
				cout << -1;
			}
			if(i==M-1)
			{
				cout<<endl;
			} else
			{
				cout<<" ";
			}
		}
	}
}