require 'set'

### constant

MAX = 20

### subroutines

def count_line(line, r, c, s)
  pos = 0
  while line[pos] == '.'
    pos += 1
  end
  idt = pos
  
  while pos < line.length
    case line[pos]
    when '('
      r += 1
    when ')'
      r -= 1
    when '{'
      c += 1
    when '}'
      c -= 1
    when '['
      s += 1
    when ']'
      s -= 1
    end

    pos += 1
  end

  [idt, r, c, s]
end

### main

loop do
  pn, qn = gets.split.map(&:to_i)
  break if (pn | qn) == 0

  r = c = s = 0
  mat = []
  v = []
  
  pn.times do
    line = gets.strip

    mat << [r, c, s]
    idt, r, c, s = count_line(line, r, c, s)
    v << idt
  end
  mat.shift
  v.shift
  pn -= 1
  #p [mat, v]

  r = c = s = 0
  mat0 = []

  qn.times do
    line = gets.strip
    mat0 << [r, c, s]
    idt, r, c, s = count_line(line, r, c, s)
  end
  #p mat0

  idts = Array.new(qn){Set.new}

  for r in (1..MAX)
    for c in (1..MAX)
      for s in (1..MAX)
        ok = true
        for i in (0...pn)
          idt = mat[i][0] * r + mat[i][1] * c + mat[i][2] * s
          if idt != v[i]
            ok = false
            break
          end
        end

        if ok
          for i in (0...qn)
            idts[i] << mat0[i][0] * r + mat0[i][1] * c + mat0[i][2] * s
          end
        end
      end
    end
  end
  #p idts

  puts idts.map{|is| is.size != 1 ? -1 : is.first}.join(' ')
end