#include <iostream>
#include <vector>
#include <string>

using namespace std;

typedef vector<int> V;

class Constants {
    public:
        int r, c, s;
        Constants(int r=0, int c=0, int s=0) {
            this->r = r;
            this->c = c;
            this->s = s;
        }
        Constants operator+(Constants consts) {
            Constants retval;
            retval.r = this->r+consts.r;
            retval.c = this->c+consts.c;
            retval.s = this->s+consts.s;
            return retval;
        }
};

class Eq {
    private:
        int n;
        Constants consts[20];
        int indentations[20];
    public:
        Eq() {
            this->n = 0;
        }
        void append(Constants consts, int indentation) {
            this->consts[n] = consts;
            this->indentations[n] = indentation;
            n++;
        }
        int compute_indentation(Constants consts) {
            int prev_indentation = -1;
            bool has_found = false;
            for (int r=1; r<=20; r++) {
                for (int c=1; c<=20; c++) {
                    for (int s=1; s<=20; s++) {
                        bool is_valid = true;
                        for (int i=0; i<this->n; i++) {
                            int y =  r*this->consts[i].r
                                    +c*this->consts[i].c
                                    +s*this->consts[i].s;
                            if (y != indentations[i]) {
                                is_valid = false;
                                break;
                            }
                        }
                        if (is_valid) {
                            int indentation = r*consts.r+c*consts.c+s*consts.s;
                            if (has_found) {
                                if (indentation != prev_indentation) {
                                    return -1;
                                }
                            }
                            else {
                                prev_indentation = indentation;
                                has_found = true;
                            }
                        }
                    }
                }
            }
            return prev_indentation;
        }
};

Constants count(string s) {
    Constants consts;
    for (int i=0; i<(int)s.length(); i++) {
        switch(s[i]) {
            case '(':
                consts.r++;
                break;
            case '{':
                consts.c++;
                break;
            case '[':
                consts.s++;
                break;
            case ')':
                consts.r--;
                break;
            case '}':
                consts.c--;
                break;
            case ']':
                consts.s--;
                break;
        }
    }
    return consts;
}

V solve(int p, int q, string pstr[], string qstr[]) {
    Constants consts = Constants();
    Eq e;
    V indentations;
    for (int i=0; i<p; i++) {
        int indentation = 0;
        for (int j=0; j<(int)pstr[i].length(); j++) {
            if (pstr[i][j] != '.') {
                break;
            }
            indentation++;
        }
        e.append(consts, indentation);
        consts = consts+count(pstr[i]);
    }
    consts = Constants();
    for (int i=0; i<q; i++) {
        indentations.push_back(e.compute_indentation(consts));
        consts = consts+count(qstr[i]);
    }
    return indentations;
}

void output(V indentations) {
    for (int i=0; i<(int)indentations.size(); i++) {
        cout << indentations[i];
        if (i < (int)indentations.size()-1) {
            cout << ' ';
        }
        else {
            cout << endl;
        }
    }
}

int main() {
    int p, q;
    string pstr[10], qstr[10];
    while (true) {
        cin >> p >> q;
        if (p == 0) {
            return 0;
        }
        for (int i=0; i<p; i++) {
            cin >> pstr[i];
        }
        for (int i=0; i<q; i++) {
            cin >> qstr[i];
        }
        output(solve(p, q, pstr, qstr));
    }
}

Measured memory_bytes: 1240