import std.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;

void solve(Cond[] cs, int[][] qs) {
    //writeln("solve: ", cs, qs);
    bool match(int x, int y, int z) {
        foreach (c; cs) {
            if (c.c[0] * x + c.c[1] * y + c.c[2] * z != c.v) {
                return false;
            }
        }
        return true;
    }
    int[][] cands; // 考えられるR, C, Sの組すべて
    void prepare() {
        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                for (int k = 1; k <= 20; k++) {
                    if (match(i, j, k)) {
                        cands ~= [i, j, k];
                    }
                }
            }
        }
    }
    prepare;

    int[] ans;
    foreach (q; qs) {
        int[] pos;
        foreach (cand; cands) {
            pos ~= cand[0] * q[0] + cand[1] * q[1] + cand[2] * q[2];
        }
        if (pos.sort.uniq.array.length == 1) {
            ans ~= pos[0];
        } else {
            ans ~= -1;
        }
    }
    ans.map!(to!string).join(" ").writeln;
}

struct Cond {
    int[] c;
    int v;
}

void main() {
    int p, q;
    while (readf("%d %d\n", &p, &q), p || q) {
        string[] P = new string[p];
        string[] Q = new string[q];
        foreach (i; 0 .. p) 
            P[i] = readln.chomp;
        foreach (i; 0 .. q) 
            Q[i] = readln.chomp;

        int[] c = new int[3];
        Cond[] conds;
        foreach (i; 0 .. p) {
            int dots = cast(int)P[i].countUntil!(a => a != '.');
            conds ~= Cond(c.dup, dots);
            foreach (j; 0 .. P[i].length) {
                switch (P[i][j]) {
                    case '(': c[0]++; break;
                    case ')': c[0]--; break;
                    case '{': c[1]++; break;
                    case '}': c[1]--; break;
                    case '[': c[2]++; break;
                    case ']': c[2]--; break;
                    default: // DO NOTHING
                }
            }
        }

        c[] = 0;
        int[][] queries;
        foreach (i; 0 .. q) {
            queries ~= c.dup;
            foreach (j; 0 .. Q[i].length) {
                switch (Q[i][j]) {
                    case '(': c[0]++; break;
                    case ')': c[0]--; break;
                    case '{': c[1]++; break;
                    case '}': c[1]--; break;
                    case '[': c[2]++; break;
                    case ']': c[2]--; break;
                    default: // DO NOTHING
                }
            }
        }
        solve(conds, queries);
    }
}