Stylish is a programming language whose syntax comprises names, that are sequences of Latin alphabet letters, three types of grouping symbols, periods ('.'), and newlines. Grouping symbols, namely round brackets ('(' and ')'), curly brackets ('{' and '}'), and square brackets ('[' and ']'), must match and be nested properly. Unlike most other programming languages, Stylish uses periods instead of whitespaces for the purpose of term separation. The following is an example of a Stylish program.


1 ( Welcome .to
2 ......... Stylish )
3 { Stylish .is
4 .....[.( a. programming . language .fun .to. learn )
5 .......]
6 ..... Maybe .[
7 ....... It. will .be.an. official . ICPC . language
8 .......]
9 .....}


As you see in the example, a Stylish program is indented by periods. The amount of indentation of a line is the number of leading periods of it.

Your mission is to visit Stylish masters, learn their indentation styles, and become the youngest Stylish master. An indentation style for well-indented Stylish programs is defined by a triple of integers, (R, C, S), satisfying 1 ≤ R, C, S ≤ 20. R, C and S are amounts of indentation introduced by an open round bracket, an open curly bracket, and an open square bracket, respectively.

In a well-indented program, the amount of indentation of a line is given by R(ro − rc) + C(co − cc) + S(so − sc), where ro, co, and so are the numbers of occurrences of open round, curly, and square brackets in all preceding lines, respectively, and rc, cc, and sc are those of close brackets. The first line has no indentation in any well-indented program.

The above example is formatted in the indentation style (R, C, S) = (9, 5, 2). The only grouping symbol occurring in the first line of the above program is an open round bracket. Therefore the amount of indentation for the second line is 9 * (1 − 0) + 5 * (0 − 0) + 2 *(0 − 0) = 9. The first four lines contain two open round brackets, one open curly bracket, one open square bracket, two close round brackets, but no close curly nor square bracket. Therefore the amount of indentation for the fifth line is 9 * (2 − 2) + 5 * (1 − 0) + 2 * (1 − 0) = 7.

Stylish masters write only well-indented Stylish programs. Every master has his/her own indentation style.

Write a program that imitates indentation styles of Stylish masters.



Input

The input consists of multiple datasets. The first line of a dataset contains two integers p (1 ≤ p ≤ 10) and q (1 ≤ q ≤ 10). The next p lines form a well-indented program P written by a Stylish master and the following q lines form another program Q. You may assume that every line of both programs has at least one character and at most 80 characters. Also, you may assume that no line of Q starts with a period.

The last dataset is followed by a line containing two zeros.

Output

Apply the indentation style of P to Q and output the appropriate amount of indentation for each line of Q. The amounts must be output in a line in the order of corresponding lines of Q and they must be separated by a single space. The last one should not be followed by trailing spaces. If the appropriate amount of indentation of a line of Q cannot be determined uniquely through analysis of P, then output -1 for that line.

Example

Input

5 4
(Follow.my.style
.........starting.from.round.brackets)
{then.curly.brackets
.....[.and.finally
.......square.brackets.]}
(Thank.you
{for.showing.me
[all
the.secrets]})
4 2
(This.time.I.will.show.you
.........(how.to.use.round.brackets)
.........[but.not.about.square.brackets]
.........{nor.curly.brackets})
(I.learned
how.to.use.round.brackets)
4 2
(This.time.I.will.show.you
.........(how.to.use.round.brackets)
.........[but.not.about.square.brackets]
.........{nor.curly.brackets})
[I.have.not.learned
how.to.use.square.brackets]
2 2
(Be.smart.and.let.fear.of
..(closed.brackets).go)
(A.pair.of.round.brackets.enclosing
[A.line.enclosed.in.square.brackets])
1 2
Telling.you.nothing.but.you.can.make.it
[One.liner.(is).(never.indented)]
[One.liner.(is).(never.indented)]
2 4
([{Learn.from.my.KungFu
...}])
((
{{
[[
]]}}))
1 2
Do.not.waste.your.time.trying.to.read.from.emptiness
(
)
2 3
({Quite.interesting.art.of.ambiguity
....})
{
(
)}
2 4
({[
............................................................]})
(
{
[
]})
0 0


Output

0 9 14 16
0 9
0 -1
0 2
0 0
0 2 4 6
0 -1
0 -1 4
0 20 40 60
#include <iostream>
#include <vector>
 
using namespace std;
 
#define INF 1e9
 
int main(){
    int P,Q;
    while(cin >> P >> Q, (P | Q)){
	int ind[10];
	int ro[10],rc[10],co[10],cc[10],so[10],sc[10];
	int ro2[10],rc2[10],co2[10],cc2[10],so2[10],sc2[10];
	string s;
	for(int i = 0 ; i < 10 ; i++){
	    ind[i] = 0;
	    ro[i] = rc[i] = 0;
	    co[i] = cc[i] = 0;
	    so[i] = sc[i] = 0;
	}
	for(int i = 0 ; i < P ; i++){
	    cin >> s;
	    bool rd = true;
	    for(int j = 0 ; j < (int)s.size() ; j++){
		if(s[j] == '(') ro[i]++; 
		if(s[j] == ')') rc[i]++; 
		if(s[j] == '{') co[i]++; 
		if(s[j] == '}') cc[i]++; 
		if(s[j] == '[') so[i]++;
		if(s[j] == ']') sc[i]++;
		if(s[j] != '.') rd = false;
		if(rd){ ind[i]++; }
	    }
	}
	for(int i = 1 ; i < P ; i++){
	    ro[i] += ro[i-1];
	    rc[i] += rc[i-1];
	    co[i] += co[i-1];
	    cc[i] += cc[i-1];
	    so[i] += so[i-1];
	    sc[i] += sc[i-1];
	}
	for(int i = 0 ; i < 10 ; i++){
	    ro2[i] = rc2[i] = 0;
	    co2[i] = cc2[i] = 0;
	    so2[i] = sc2[i] = 0;
	}
	for(int i = 0 ; i < Q ; i++){
	    cin >> s;
	    for(int j = 0 ; j < (int)s.size() ; j++){
		if(s[j] == '(') ro2[i]++; 
		if(s[j] == ')') rc2[i]++; 
		if(s[j] == '{') co2[i]++; 
		if(s[j] == '}') cc2[i]++; 
		if(s[j] == '[') so2[i]++; 
		if(s[j] == ']') sc2[i]++; 
	    }
	}
	for(int i = 1 ; i < Q ; i++){
	    ro2[i] += ro2[i-1];
	    rc2[i] += rc2[i-1];
	    co2[i] += co2[i-1];
	    cc2[i] += cc2[i-1];
	    so2[i] += so2[i-1];
	    sc2[i] += sc2[i-1];
	}
	vector<int> res(10,INF);
	res[0] = 0;
	for(int i = 1 ; i < Q ; i++){
	    vector<int> v;
	    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(int j = 1 ; j < P ; j++){
			    int rd = ro[j-1]-rc[j-1];
			    int cd = co[j-1]-cc[j-1];
			    int sd = so[j-1]-sc[j-1];
			    if(R*rd+C*cd+S*sd != ind[j]){
				ok = false;
				break;
			    }
			}
			if(ok){
			    int rd = ro2[i-1]-rc2[i-1];
			    int cd = co2[i-1]-cc2[i-1];
			    int sd = so2[i-1]-sc2[i-1];
			    int val = R*rd+C*cd+S*sd;
			    if(res[i] == INF){
				res[i] = val;
			    }else{
				if(res[i] != val && res[i] != -1){
				    res[i] = -1;
				}
			    }
			}
		    }
		}
	    }
	}
	for(int i = 0 ; i < Q ; i++){
	    if(i) cout << " "; 
	    cout << res[i];
	}
	cout << endl;
    }
    return 0;
}