// Arup Guha
// 7/9/2021
// Solution to 2021 SI@UCF Contest #2 (Both Groups) Problem: Matching Parentheses

import java.util.*;

public class parens {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
			
			char[] list = stdin.next().toCharArray();
			
			// Just doing 2 parallel stacks instead of a Stack of objects.
			Stack<Integer> pos = new Stack<Integer>();
			Stack<Integer> depth = new Stack<Integer>();
			
			// Store result here.
			long res = 0;
			
			// Go character by character.
			for (int i=0; i<list.length; i++) {
				
				// New paren.
				if (list[i] == '(') {
					
					// This new pair is at depth 0.
					pos.push(i);
					depth.push(0);
				}
				
				// End a pair.
				else {
					
					// Get the final dimensions of this bracket.
					int start = pos.pop();
					int height = depth.pop() + 1;
					
					// One horizontal line, two vertical lines.
					res += (i-start) + 2*height;
					
					
					// We are not the lowest level, so update the max depth.
					if (depth.size() > 0) {
						int curD = depth.pop();
						depth.push(Math.max(curD, height));
					}
				}
			} // end i
			
			// Ta da!
			System.out.println(res);
		}
	}
}