import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

auto go (string s, string c)
{
	char [] stack = "**".dup;
	foreach (ref e; s.filter !(e => c.canFind (e)))
	{
		stack.assumeSafeAppend ();
		stack ~= e;
		if (stack[$ - 2..$] == c)
		{
			stack.popBackN (2);
		}
	}
	return stack.length.to !(int) - 2;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln ((s.length - s.go ("()") - s.go ("[]")) / 2);
	}
}
