// cheese-cracker [2022-01-31]

void solve(){
    auto s = scan!(dchar[]);
    long on = 0;
    long off = 0;
    long res = 0;
    foreach(c; s){
        on += (c == '1');
        off += (c == '0');
        if(max(on, off) > min(on, off) && max(on, off) > 1){
            res = max(res, min(on, off));
        }
    }
    writeln(res);
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
