nC = int(input("").strip())

for loop in range(nC):

    s = input("").strip()

    res = 0

    # position stack
    pos = []

    # height stack
    height = []

    for i in range(len(s)):

        if s[i] == '(':
            pos.append(i)
            height.append(0)

        else:

            mylen = i - pos.pop()
            curH = height.pop()
            res += ((curH+1)*2 + mylen)

            if len(height) > 0:
                tmp = height.pop()
                height.append(max(tmp, curH+1))

    print(res)
