Problem:
_Stylish_ is a programming language whose syntax comprises _names_ , that are sequences of Latin alphabet letters, three types of _grouping symbols_ , periods ('.'), and newlines. Grouping symbols, namely round brackets ('(' and ')'), curly brackets ('{' and '}'), and square brackets ('[' and ']'), must match and be nested properly. Unlike most other programming languages, Stylish uses periods instead of whitespaces for the purpose of term separation. The following is an example of a Stylish program.                 1 ( Welcome .to 2 ......... Stylish ) 3 { Stylish .is 4 .....[.( a. programming . language .fun .to. learn ) 5 .......] 6 ..... Maybe .[ 7 ....... It. will .be.an. official . ICPC . language 8 .......] 9 .....}   As you see in the example, a Stylish program is indented by periods. The _amount of indentation_ of a line is the number of leading periods of it.  Your mission is to visit Stylish masters, learn their indentation styles, and become the youngest Stylish master. An indentation style for _well-indented_ Stylish programs is defined by a triple of integers, ( _R_ , _C_ , _S_ ), satisfying 1 ≤ _R_ , _C_ , _S_ ≤ 20\. _R_ , _C_ and _S_ are amounts of indentation introduced by an open round bracket, an open curly bracket, and an open square bracket, respectively.  In a well-indented program, the amount of indentation of a line is given by _R_ ( _r o_ − _r c_) + _C_ ( _c o_ − _c c_) + _S_ ( _s o_ − _s c_), where _r o_, _c o_, and _s o_ are the numbers of occurrences of open round, curly, and square brackets in all preceding lines, respectively, and _r c_, _c c_, and _s c_ are those of close brackets. The first line has no indentation in any well- indented program.  The above example is formatted in the indentation style ( _R_ , _C_ , _S_ ) = (9, 5, 2). The only grouping symbol occurring in the first line of the above program is an open round bracket. Therefore the amount of indentation for the second line is 9 * (1 − 0) + 5 * (0 − 0) + 2 *(0 − 0) = 9. The first four lines contain two open round brackets, one open curly bracket, one open square bracket, two close round brackets, but no close curly nor square bracket. Therefore the amount of indentation for the fifth line is 9 * (2 − 2) + 5 * (1 − 0) + 2 * (1 − 0) = 7.  Stylish masters write only well-indented Stylish programs. Every master has his/her own indentation style.  Write a program that imitates indentation styles of Stylish masters.

Incorrect submission (verdict: Wrong Answer):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        p,q = LI()
        if p == 0:
            break

        a = [S() for _ in range(p)]
        b = [S() for _ in range(q)]
        aa = [[0,0,0,0]]
        mc = 0
        for c in a:
            d = collections.Counter(c)
            t = aa[-1][:]
            t[0] += d['(']
            t[0] -= d[')']
            t[1] += d['{']
            t[1] -= d['}']
            t[2] += d['[']
            t[2] -= d[']']
            t[3] = 0
            for ct in c:
                if ct != '.':
                    break
                t[3] += 1
                if mc < t[3]:
                    mc = t[3]
            aa.append(t)
        k = []
        for c1,c2,c3 in itertools.product(range(1,min(mc+1,21)), repeat=3):
            f = True
            for ci in range(p):
                c = aa[ci]
                if c[0] * c1 + c[1] * c2 + c[2] * c3 != aa[ci+1][3]:
                    f = False
                    break
            if f:
                k.append((c1,c2,c3))
        bb = [[0,0,0]]
        for c in b:
            d = collections.Counter(c)
            t = bb[-1][:]
            t[0] += d['(']
            t[0] -= d[')']
            t[1] += d['{']
            t[1] -= d['}']
            t[2] += d['[']
            t[2] -= d[']']
            bb.append(t)

        r = [0]
        for c in bb[1:-1]:
            s = set()
            for c1,c2,c3 in k:
                s.add(c[0]*c1+c[1]*c2+c[2]*c3)
            if len(s) == 1:
                r.append(list(s)[0])
            else:
                r.append(-1)

        rr.append(' '.join(map(str,r)))


    return '\n'.join(map(str,rr))


print(main())




Fixed, accepted submission:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        p,q = LI()
        if p == 0:
            break

        a = [S() for _ in range(p)]
        b = [S() for _ in range(q)]
        aa = [[0,0,0,0]]
        mc = 0
        for c in a:
            d = collections.Counter(c)
            t = aa[-1][:]
            t[0] += d['(']
            t[0] -= d[')']
            t[1] += d['{']
            t[1] -= d['}']
            t[2] += d['[']
            t[2] -= d[']']
            t[3] = 0
            for ct in c:
                if ct != '.':
                    break
                t[3] += 1
                if mc < t[3]:
                    mc = t[3]
            aa.append(t)
        k = []
        for c1,c2,c3 in itertools.product(range(1,min(mc+1,21)), repeat=3):
            f = True
            for ci in range(p):
                c = aa[ci]
                if c[0] * c1 + c[1] * c2 + c[2] * c3 != aa[ci+1][3]:
                    f = False
                    break
            if f:
                k.append((c1,c2,c3))
        bb = [[0,0,0]]
        for c in b:
            d = collections.Counter(c)
            t = bb[-1][:]
            t[0] += d['(']
            t[0] -= d[')']
            t[1] += d['{']
            t[1] -= d['}']
            t[2] += d['[']
            t[2] -= d[']']
            bb.append(t)

        r = [0]
        for c in bb[1:-1]:
            s = set()
            for c1,c2,c3 in k:
                s.add(c[0]*c1+c[1]*c2+c[2]*c3)
            if len(s) == 1:
                r.append(list(s)[0])
            elif sum(c) == 0:
                r.append(0)
            else:
                r.append(-1)

        rr.append(' '.join(map(str,r)))


    return '\n'.join(map(str,rr))


print(main())