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 JAVA):

Solution:
import java.io.*;
//      import java.sql.SQLOutput;
import java.sql.SQLOutput;
import java.util.*;



public class Solution {

//	static
//	{
//		System.out.println("beta");
//	}
//	
	
	public static long next(long  n)
	{
		int min = 11;
		int max = -1;
		
		long nn = n;
		
		while(nn>=1)
		{
			int dig = (int) (nn%10);
			if(dig<min)min=dig;
			if(dig>max)max=dig;
			
			nn/=10;
		}
		
		return n+(min*max);
		
	}
	
	public static boolean zero(long nn)
	{
		while(nn>=1)
		{
			int dig = (int) (nn%10);
			if(dig==0)return true;
			nn/=10;
		}
		
		return false;
	}
	
	
	public static void method(int arr[])
	{
		arr[0]=1;
	}
	
	
	public static int dsum(int x)
	{
		int rt=0;
		int n = x;

		while(n>=1)
		{
			int rm = n%10;
			rt+=rm;
			n=n/10;
		}
		
		return rt;
	}
	
    public static void main(String[] args) throws IOException
    {
    	
    	
    	//here
    	int t = sc.nextInt();
    	
    	
    	while(t-->0)
    	{
    		String st = sc.nextLine();
    		
    		int n =  st.length();
    		
    		Stack<Character> st1 = new Stack<Character>();
    		Stack<Character> st2 = new Stack<Character>();
    		
    		int ct1=0;
    		
    		for(int i=0;i<n;i++)
    		{
    			char ch = st.charAt(i);
    			
    			if(ch=='('||ch==')')
    			{
    				
    				if(st1.isEmpty())
    				{
    					st1.push(ch);
    				}
    				else
    				{
    					if(ch==')'&&st1.peek()=='(')
    					{
    						ct1++;
    						st1.pop();
    					}
    					else
    					{
    						st1.push(ch);
    					}
    				}
    				
    				
    			}
    		}
    		
    		int ct2=0;
    		for(int i=0;i<n;i++)
    		{
    			char ch = st.charAt(i);
    			
    			if(ch=='['||ch==']')
    			{
    				
    				if(st2.isEmpty())
    				{
    					st2.push(ch);
    				}
    				else
    				{
    					if(ch==']'&&st2.peek()=='[')
    					{
    						ct2++;
    						st2.pop();
    					}
    					else
    					{
    						st2.push(ch);
    					}
    				}
    				
    				
    			}
    		}
    		
    		pw.println(ct1+ct2);
    		
    		
    	}
    	
    	
	    pw.close();
		
    }
    	
    	
    
    
    static int gcd(int a, int b) 
    { 
      if (b == 0) 
        return a; 
      return gcd(b, a % b);  
    } 
      








    static  class MyScanner
    {

        BufferedReader br;
        StringTokenizer st;

        public MyScanner()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next()
        {

            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }

    }


    static MyScanner sc = new  MyScanner();
    static PrintWriter pw = new PrintWriter(System.out, true);

    static class Comp implements Comparator<Point>
    {

        @Override
        public int compare(Point arg0, Point arg1) {

            if(arg0.x>arg1.x)return 1;
            if(arg0.x<arg1.x)return -1;
            return 0;
        }

    }



    static class Point
    {
        int x,y,ind;

        public Point(int a, int b)
        {
            x=a;
            y=b;
            ind=0;
        }

        @Override
        public boolean equals(Object o)
        {

            // If the object is compared with itself then return true
            if (o == this) {
                return true;
            }
    			 
    			        /* Check if o is an instance of Complex or not
    			          "null instanceof [type]" also returns false */
            if (!(o instanceof Point)) {
                return false;
            }

            // typecast o to Complex so that we can compare data members
            Point c = (Point) o;

            // Compare the data members and return accordingly
            return x==c.x&&y==c.y;

        }

    }






}