#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;


vector<pair<char, ll>> RLE(string& s) {
    ll cnt = 0;
    vector<pair<char, ll>> res;
    for (ll i = 0; i < s.size(); i++) {
        cnt++;
        if (i == (ll)s.size() - 1) {
            res.push_back(make_pair(s[i], cnt));
            break;
        }
        if (s[i] != s[i + 1]) {
            res.push_back(make_pair(s[i], cnt));
            cnt = 0;
        }
    }
    return res;
}
void solve() {
    int p, q; cin >> p >> q; if (p == 0 and q == 0) exit(0);
    int b[3] = {};
    int r[3] = {-1, -1, -1};
    char c1[3] = { '(','{','[' };
    char c2[3] = { ')','}',']' };
    string s; getline(cin, s);
    vector<bool> tw(3, false);
    vector<vector<int>> tvv, vv;
    for (int kkt = 0; kkt < p; kkt++) {
        getline(cin, s);
        auto vp = RLE(s);
        int cnt = 0; if (vp[0].first == '.') cnt = vp[0].second;
        tvv.push_back({ b[0], b[1], b[2], cnt });
        for (int i = 0; i < s.size(); i++) {
            for (int j = 0; j < 3; j++) if (s[i] == c1[j]) b[j]++;
            for (int j = 0; j < 3; j++) if (s[i] == c2[j]) b[j]--;
        }

    }
    for (int i = 1; i <= 20; i++) for (int j = 1; j <= 20; j++) for (int k = 1; k <= 20; k++) {
        bool ok = true;
        for (auto& v : tvv) {
            if (i * v[0] + j * v[1] + k * v[2] != v[3]) { ok = false; break; }
        }
        if (ok) vv.push_back({ i,j,k });
    }
    b[0] = b[1] = b[2] = 0;
    vector<int> res;
    for (int i = 0; i < q; i++) {
        getline(cin, s);
        bool ok = true;
        int cnt = -1;
        for (auto& v: vv) {
            int c = 0; for (int i = 0; i < 3; i++) c += v[i] * b[i];
            if (cnt == -1) cnt = c;
            else if (cnt != c) ok = false;
        }
        if (ok) res.emplace_back(cnt);
        else res.emplace_back(-1);
        for (int i = 0; i < s.size(); i++) {
            for (int j = 0; j < 3; j++) if (s[i] == c1[j]) b[j]++;
            for (int j = 0; j < 3; j++) if (s[i] == c2[j]) b[j]--;
        }
    }
    for (int i = 0; i < res.size(); i++) cout << res[i] << " \n"[i + 1 == res.size()];
}

int main()
{
    
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int kkt = 89;
    while (kkt) {
        solve();
    }
    return 0;
}


Measured memory_bytes: 3292