// Cheese-Cracker: cheese-cracker.github.io

dchar[] stringify(ll num){
    dchar[] res;
    while(num > 0){
        res ~= to!dchar('0' + (num % 10));
        num /= 10;
    }
    res.reverse;
    return res;
}

dchar[][] pow2;
void preProcess(){
    ll num = 1;
    for(int i = 0; i <= 62; ++i){
        pow2 ~= stringify(num);
        num *= 2;
    }
}

ll common(dchar[] w, dchar[] p2){
    int ptr = 0;
    foreach(c; w){
        if(p2[ptr] == c){
            ++ptr;
        }
        if(ptr == p2.length.to!long){
            return ptr.to!long;
        }
    }
    return ptr.to!long;
}

void theCode(){
    auto w = scan!(dchar[]);
    ll mindist = 32;
    for(int i = 0; i < 62; ++i){
        ll com = common(w, pow2[i]);
        ll dist = w.length.to!long + pow2[i].length.to!long - 2 * com;
        mindist = min(dist, mindist);
    }
    writeln(mindist);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");
