Problem: You are given a string s, consisting of brackets of two types: '(', ')', '[' and ']'.

A string is called a regular bracket sequence (RBS) if it's of one of the following types: 

  * empty string; 
  * '(' + RBS + ')'; 
  * '[' + RBS + ']'; 
  * RBS + RBS. 



where plus is a concatenation of two strings.

In one move you can choose a non-empty subsequence of the string s (not necessarily consecutive) that is an RBS, remove it from the string and concatenate the remaining parts without changing the order.

What is the maximum number of moves you can perform?

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.

Each of the next t lines contains a non-empty string, consisting only of characters '(', ')', '[' and ']'. The total length of the strings over all testcases doesn't exceed 2 ⋅ 10^5.

Output

For each testcase print a single integer — the maximum number of moves you can perform on a given string s.

Example

Input


5
()
[]()
([)]
)]([
)[(]


Output


1
2
2
0
1

Note

In the first example you can just erase the whole string.

In the second example you can first erase the brackets on positions 1 and 2: "[]()", then "()" is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two.

In the third example you can first erase the brackets on positions 1 and 3: "([)]". They form an RBS "()". Then "[]" is left, so you can erase it whole.

In the fourth example there is no subsequence that is an RBS, so you can't perform a move at all.

In the fifth example you can erase the brackets on positions 2 and 4: ")[(]" and get ")(" as a result. You can erase nothing from it.

Solution (in PYTHON3):

Solution:
"""
usefull snippets:
    - map(int, input().split())
    - map(int, sys.stdin.readline().split()))
    - int(input())
    - int(sys.stdin.readline().strip())
    - sys.stdout.write()
    - sys.stdout.write(" ".join(map(str, c) # writes c - collection of ints
"""
# import collections
import sys
import math
from bisect import bisect_left
from collections import defaultdict

# recursion increase
# sys.setrecursionlimit(10000)

# number with big precision
# from decimal import getcontext, Decimal
# getcontext().prec = 34


# longest common prefix
def get_lcp(s, suffix_array):
    s = s + "$"
    n = len(s)
    lcp = [0] * (n)
    pos = [0] * (n)
    for i in range(n - 1):
        pos[suffix_array[i]] = i
    k = 0
    for i in range(n - 1):
        if k > 0:
            k -= 1
        if pos[i] == n - 1:
            lcp[n - 1] = -1
            k = 0
            continue
        else:
            j = suffix_array[pos[i] + 1]
            while max([i + k, j + k]) < n and s[i + k] == s[j + k]:
                k += 1
            lcp[pos[i]] = k

    return lcp


def get_suffix_array(word):
    suffix_array = [("", len(word))]

    for position in range(len(word)):
        sliced = word[len(word) - position - 1 :]
        suffix_array.append((sliced, len(word) - position - 1))

    suffix_array.sort(key=lambda x: x[0])
    return [item[1] for item in suffix_array]


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def bin_search(collection, element):
    i = bisect_left(collection, element)
    if i != len(collection) and collection[i] == element:
        return i
    else:
        return -1


def main():
    t = int(input())

    for _ in range(t):
        s = str(input())
        c1 = 0
        c2 = 0
        ans = 0
        for i in range(len(s)):
            if s[i] == '(':
                c1 += 1
            elif s[i] == ')':
                if c1 > 0:
                    ans += 1
                    c1 -= 1
            elif s[i] == '[':
                c2 += 1
            else:
                if c2 > 0:
                    ans += 1
                    c2 -=1

        print(ans)


if __name__ == "__main__":
    main()