#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>
#include <cstdint>


/* ----- Type Alias ----- */
using Int = long long int;
using Real = long double;
using String = std::string;
template <class... Ts>
using Tuple = std::tuple<Ts...>;

template <class T>
using Vector = std::vector<T>;
template <class T>
using Queue = std::queue<T>;
template <class T>
using Stack = std::stack<T>;
template <class T>
using Deque = std::deque<T>;

template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using Set = std::set<T>;
template <class T, class U>
using Map = std::map<T, U>;

template <class T, class... Us>
using Func = std::function<T(Us...)>;

template <class T>
T genv(T v) { return v; }

template <class T, class... Ts>
auto genv(size_t l, Ts... ts) {
    return Vector<decltype(genv<T>(ts...))>(l, Vector<T>(ts...));
}

template <class Cost = Int>
struct Edge {
    Int src, dst;
    Cost cost;
    Edge(Int src = -1, Int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = Int>
using Edges = Vector<Edge<Cost>>;
template <class Cost = Int>
using Graph = Vector<Vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

struct Fout {
    Int precision;
    Fout(Int precision) : precision(precision) {}
};
std::ostream& operator<<(std::ostream& os, const Fout& fio) {
    os << std::fixed << std::setprecision(fio.precision);
    return os;
}


/* ----- Constants ----- */
// constexpr Int INF = std::numeric_limits<Int>::max() / 1000;
// constexpr Int MOD = 1000000007;
// constexpr Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

Int parse(const String& s, Vector<Int>& cnt) {
    Int i;
    for (i = 0; s[i] == '.'; ++i) {}

    for (char c : s) {
        switch (c) {
            case '(':
                ++cnt[0];
                break;
            case ')':
                --cnt[0];
                break;
            case '{':
                ++cnt[1];
                break;
            case '}':
                --cnt[1];
                break;
            case '[':
                ++cnt[2];
                break;
            case ']':
                --cnt[2];
                break;
            default:
                break;
        }
    }
    return i;
}

bool solve() {
    Int n, m;
    std::cin >> n >> m;
    if (n == 0) return false;

    Vector<Tuple<Vector<Int>, Int>> lines(n);
    Vector<Int> cnt(3, 0);
    for (auto& l : lines) {
        String s;
        std::cin >> s;

        Vector<Int> prev = cnt;
        Int dots = parse(s, cnt);
        l = std::make_tuple(prev, dots);
    }

    Vector<Vector<Int>> pos;
    Vector<Int> x(3);
    for (x[0] = 1; x[0] <= 20; ++x[0]) {
        for (x[1] = 1; x[1] <= 20; ++x[1]) {
            for (x[2] = 1; x[2] <= 20; ++x[2]) {
                bool judge = true;

                for (auto l : lines) {
                    Vector<Int> v;
                    Int s;
                    std::tie(v, s) = l;

                    for (Int i = 0; i < 3; ++i) s -= v[i] * x[i];
                    if (s != 0) judge = false;
                }

                if (judge) pos.push_back(x);
            }
        }
    }

    fill(cnt.begin(), cnt.end(), 0);
    for (Int i = 0; i < m; ++i) {
        String s;
        std::cin >> s;

        Vector<Int> prev = cnt;
        parse(s, cnt);

        Set<Int> ans;
        for (const auto& x : pos) {
            Int sum = 0;
            for (Int i = 0; i < 3; ++i) sum += x[i] * prev[i];
            ans.insert(sum);
        }

        std::cout << (ans.size() == 1 ? *ans.begin() : -1) << " \n"[i == m - 1];
    }
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}



Measured memory_bytes: 3392