#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}


int main(void) {
    int p,q;
    string t = "(){}[]";
    while(cin >> p >> q, p){
        vi v(6,0);
        vector<vi> x(p,vi(4));
        rep(i,p){
            string in;
            cin >> in;
            rep(j,3) x[i][j] = v[2*j] - v[2*j+1];
            int j = 0;
            while(in[j] == '.')j++;
            x[i][3] = j;
            //rep(j,4)cout << " " << x[i][j];cout << endl;
            rep(j,in.size())rep(k,6)if(in[j] == t[k])v[k]++;
        }
        vector<vi> par(3);
        loop(r,1,21)loop(c,1,21)loop(s,1,21){
            bool b = true;
            rep(i,p){
                if(r*x[i][0] + c*x[i][1] + s*x[i][2] != x[i][3]) b = false;
            }
            if(b){
                //cout << r << " " << c << " " << s << endl;
                par[0].push_back(r);
                par[1].push_back(c);
                par[2].push_back(s);
            }
        }
        rep(i,3){
            sort(all(par[i]));
            par[i].erase(unique(all(par[i])),par[i].end());
            //cout << i << endl;
            //rep(j,par[i].size())cout << " " << par[i][j];cout << endl;
            if(par[i].size() > 1)par[i][0] = -10000;
            else if(par[i].size() == 0)par[i].push_back(-10000);
        }
        v = vi(6,0);
        rep(i,q){
            int ans = 0;
            rep(j,3)ans += par[j][0]*(v[2*j] - v[2*j+1]);
            if(ans < 0){
                bool c = true;
                rep(j,p){
                    int cnt = 0;
                    rep(k,3)if(x[j][k] == v[2*k] - v[2*k+1])cnt++;
                    if(cnt == 3) ans = x[j][3];
                }
                if(ans < 0)ans = -1;
            }
            cout << ans;
            if(i < q-1) cout << " ";
            else cout << endl;
            string in;
            cin >> in;
            rep(j,in.size())rep(k,6)if(in[j] == t[k])v[k]++;
        }


    }
}

