extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Index, IndexMut, Add, AddAssign, Sub, SubAssign, Mul, MulAssign};
use std::collections::VecDeque;
use std::f32::MAX;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
struct Bracket {
    round: usize, curly: usize, square: usize
}
impl Bracket {
    fn new() -> Bracket { Bracket {round: 0, curly: 0, square: 0}}
}
#[derive(Copy, Clone)]
struct IndentRule {
    bracket: Bracket, indent: usize
}
#[derive(Copy, Clone)]
struct Coefficient{
    r: usize, c: usize, s: usize
}
impl Display for Coefficient {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "Co(r {}, c {}, s {})", self.r, self.c, self.s)
    }
}
impl Coefficient {
    fn cal_indent(&self, bracket: &Bracket) -> usize {
        self.r * bracket.round + self.c * bracket.curly + self.s * bracket.square
    }
    fn is_match_indent_rule(&self, rule: &IndentRule) -> bool {
        self.cal_indent(&rule.bracket) == rule.indent
    }
}
fn count_indent(line: &String) -> usize {
    let chars = line.chars().collect::<Vec<_>>();
    for i in 0 .. line.len() {
        if chars[i] != '.' {
            return i
        }
    }
    line.len()
}
fn renew_count(line: &String, mut bracket: Bracket) -> Bracket {
    for c in line.chars() {
        match c {
            '(' => bracket.round += 1,
            ')' => bracket.round -= 1,
            '{' => bracket.curly += 1,
            '}' => bracket.curly -= 1,
            '[' => bracket.square += 1,
            ']' => bracket.square -= 1,
            _ => {}
        }
    }
    bracket
}

fn main() {
    loop {
        let_all!(p: usize, q: usize);
        if p == 0 && q == 0 {
            return
        }
        let mut rule = Vec::with_capacity(p);
        let mut bracket = Bracket::new();
        for _ in 0 .. p {
            let line = read_line!();
            let indent = count_indent(&line);
            rule.push(IndentRule{bracket: bracket, indent: indent});
            bracket = renew_count(&line, bracket);
        }
        let mut coeffs = Vec::new();
        for r in 1 .. 21 {
            for c in 1 .. 21 {
                for s in 1 .. 21 {
                    let coeff = Coefficient{r:r,c:c,s:s};
                    let mut is_match = true;
                    for rul in &rule {
                        if !coeff.is_match_indent_rule(rul) {
                            is_match = false;
                            break
                        }
                    }
                    if is_match {
                        coeffs.push(coeff);
                    }
                }
            }
        }
        let mut indents = Vec::new();
        bracket = Bracket::new();
        for _ in 0 .. q {
            let line = read_line!();
            let mut min = std::i32::MAX;
            let mut max = 0;
            for co in &coeffs {
                let indent = co.cal_indent(&bracket) as i32;
                if min > indent {
                    min = indent;
                }
                if max < indent {
                    max = indent;
                }
            }
            if min != max {
                indents.push(-1);
            }else {
                indents.push(min);
            }
            bracket = renew_count(&line, bracket);
        }
        print!("{}", indents[0]);
        for i in 1 .. indents.len() {
            print!(" {}", indents[i]);
        }
        println!();
    }
}



Measured memory_bytes: 5036