#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <cmath>
#define INF 1000000
const int MOD = 1e9 + 7;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
using namespace std;
typedef long long ll;
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	while (cin >> n >> m, n | m)
	{
		string master[15], str[15];
		int num[15] = {};
		for (int i = 0; i < n; i++)
		{
			cin >> master[i];
			int pos = 0;
			while (master[i][pos] == '.')
			{
				num[i]++;
				pos++;
			}
		}
		for (int i = 0; i < m; i++)
			cin >> str[i];
		int R1[15] = {}, C1[15] = {}, S1[15] = {};
		int R2[15] = {}, C2[15] = {}, S2[15] = {};
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < master[i].size(); j++)
			{
				if (master[i][j] == '(')
					R1[i]++;
				if (master[i][j] == '{')
					C1[i]++;
				if (master[i][j] == '[')
					S1[i]++;
				if (master[i][j] == ')')
					R1[i]--;
				if (master[i][j] == '}')
					C1[i]--;
				if (master[i][j] == ']')
					S1[i]--;
			}
			R1[i + 1] = R1[i];
			C1[i + 1] = C1[i];
			S1[i + 1] = S1[i];
		}
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < str[i].size(); j++)
			{
				if (str[i][j] == '(')
					R2[i]++;
				if (str[i][j] == '{')
					C2[i]++;
				if (str[i][j] == '[')
					S2[i]++;
				if (str[i][j] == ')')
					R2[i]--;
				if (str[i][j] == '}')
					C2[i]--;
				if (str[i][j] == ']')
					S2[i]--;
			}
			R2[i + 1] = R2[i];
			C2[i + 1] = C2[i];
			S2[i + 1] = S2[i];
		}
		vector<int> vec(15, INF);
		vec[0] = 0;
		for (int r = 1; r <= 20; r++)
		{
			for (int c = 1; c <= 20; c++)
			{
				for (int s = 1; s <= 20; s++)
				{
					bool flag = true;
					for(int i = 0; i < n - 1; i++)
					{
						if (R1[i] * r + C1[i] * c + S1[i] * s != num[i + 1])
							flag = false;
					}
					if(flag)
					{
						for(int i = 0; i < m - 1; i++)
						{
							int tmp = R2[i] * r + C2[i] * c + S2[i] * s ;
							if(vec[i + 1] == -1 || vec[i + 1] == tmp)
								continue;
							if(vec[i + 1] != tmp && vec[i + 1] != INF)
								vec[i + 1] = -1;
							else
								vec[i + 1] = tmp;
						}
					}
				}	
			}
		}
		for(int i = 0; i < m; i++)
		{
			if(i == 0)
				cout << vec[i];
			else
				cout << " " << vec[i];
		}
		cout << endl;
	}
	return 0;
}

