description: You are given an integer n. In 1 move, you can do one of the following actions:
  
    * erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty"); 
    * add one digit to the right. 
  
  
  
  The actions may be performed in any order any number of times.
  
  Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number 301 the digit 3, the result is the number 01 (not 1).
  
  You need to perform the minimum number of actions to make the number any power of 2 (i.e. there's an integer k (k ≥ 0) such that the resulting number is equal to 2^k). The resulting number must not have leading zeroes.
  
  E.g. consider n=1052. The answer is equal to 2. First, let's add to the right one digit 4 (the result will be 10524). Then let's erase the digit 5, so the result will be 1024 which is a power of 2.
  
  E.g. consider n=8888. The answer is equal to 3. Let's erase any of the digits 8 three times. The result will be 8 which is a power of 2.
  
  Input
  
  The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
  
  Each test case consists of one line containing one integer n (1 ≤ n ≤ 10^9).
  
  Output
  
  For each test case, output in a separate line one integer m — the minimum number of moves to transform the number into any power of 2.
  
  Example
  
  Input
  
  
  12
  1052
  8888
  6
  75
  128
  1
  301
  12048
  1504
  6656
  1000000000
  687194767
  
  
  Output
  
  
  2
  3
  1
  3
  0
  0
  2
  1
  3
  4
  9
  2
  
  Note
  
  The answer for the first test case was considered above.
  
  The answer for the second test case was considered above.
  
  In the third test case, it's enough to add to the right the digit 4 — the number 6 will turn into 64.
  
  In the fourth test case, let's add to the right the digit 8 and then erase 7 and 5 — the taken number will turn into 8.
  
  The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move.
  
  In the seventh test case, you can delete first of all the digit 3 (the result is 01) and then the digit 0 (the result is 1).
solution: /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
   
  /**
   *
   * @author wilso
   */
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.*;
  public class codeforces {
      static final long MOD = 1_000_000_007;
      static final long MOD2 = 998_244_353;
      static final long X = 10000000000L;
      public static void main(String[] args) {
          FastScanner sc = new FastScanner();
          PrintWriter pw = new PrintWriter(System.out);
  //        Scanner sc = new Scanner(System.in);
  //        int tc = sc.ni();
          int tc = sc.ni();
          List<String> powers = new ArrayList();
          for (long x = 1; x > 0; x *= 2) {
              powers.add(Long.toString(x));
          }
  //        System.out.println(powers.toString());
          
          for (int rep = 0; rep < tc; rep++) {
              String s = sc.next();
              int ret = Integer.MAX_VALUE;
              for (String cand : powers) {
                  int amt = find(s, cand);
                  ret = Math.min(ret, s.length() - amt + cand.length() - amt);
              }
              pw.println(ret);
              
              
          }
              
          
          pw.close();
      }
      static int find(String a, String b) {
          int pointer = 0;
          int ret = 0;
          for (int i = 0; i < b.length(); i++) {
              while (pointer < a.length() && a.charAt(pointer) != b.charAt(i)) {
                  pointer++;
              }
              if (pointer >= a.length()) break;
              pointer++;
              ret++;
          }
          return ret;
      }
      //method extracted from uwi
      public static int[] sieveEratosthenes(int n) {
              if (n <= 32) {
                      int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
                      for (int i = 0; i < primes.length; i++) {
                              if (n < primes[i]) {
                                      return Arrays.copyOf(primes, i);
                              }
                      }
                      return primes;
              }
  
              int u = n + 32;
              double lu = Math.log(u);
              int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
              ret[0] = 2;
              int pos = 1;
  
              int[] isnp = new int[(n + 1) / 32 / 2 + 1];
              int sup = (n + 1) / 32 / 2 + 1;
  
              int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
              for (int tp : tprimes) {
                      ret[pos++] = tp;
                      int[] ptn = new int[tp];
                      for (int i = (tp - 3) / 2; i < tp << 5; i += tp)
                              ptn[i >> 5] |= 1 << (i & 31);
                      for (int j = 0; j < sup; j += tp) {
                              for (int i = 0; i < tp && i + j < sup; i++) {
                                      isnp[j + i] |= ptn[i];
                              }
                      }
              }
  
              // 3,5,7
              // 2x+3=n
              int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4,
                              13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 };
              int h = n / 2;
              for (int i = 0; i < sup; i++) {
                      for (int j = ~isnp[i]; j != 0; j &= j - 1) {
                              int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
                              int p = 2 * pp + 3;
                              if (p > n)
                                      break;
                              ret[pos++] = p;
                              if ((long) p * p > n)
                                      continue;
                              for (int q = (p * p - 3) / 2; q <= h; q += p)
                                      isnp[q >> 5] |= 1 << q;
                      }
              }
  
              return Arrays.copyOf(ret, pos);
      }
      static long factorial(long x) {
          if (x <= 1) return 1;
          return (x * (factorial(x-1) % MOD) % MOD);
      }
      static long solve(long X, long[] rates, long[] times) {
        long l = 0;
        long r = (long)(3e9);
        while (l < r) {
            long mid = l + (r-l) / 2;
            if (valid(X, rates, times, mid)) {
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
  //      System.out.println(valid(X, rates, times, 7));
        return l;
      }
      static boolean valid(long X, long[] rates, long[] times, long cand) {
          long ret = 0;
          for (int i = 0; i < rates.length; i++) {
              long diff = cand - times[i];
              if (diff < 0) continue;
              
              long amt = diff / rates[i];
  //            amt++;
              ret += amt;
              
          }
          return ret >= X;
      }
      
  
   
   
      static int greaterThan(long[] arr, long target) {
  	int lo = 0, hi = arr.length- 1;
  	while(lo < hi) { // observe this time it is "<" operator, not "<="
  		int mid = lo + (hi-lo)/2;
  		if(arr[mid] <= target) lo = mid+1;
  		else hi = mid;
  	}
  	return arr[lo] > target ? lo : -1;
  }
      static int smallerThan(long[] arr, long target) {
  	int lo = 0, hi = arr.length- 1;
  	while(lo < hi) {
  		int mid = lo + (hi-lo+1)/2; // Note that I used ceil here otherwise it could go in infinite loop
  		if(arr[mid] < target) lo = mid;
  		else hi = mid-1;
  	}
  	return arr[lo] < target ? lo : -1;
  }
    
      static int[] reduce(int[] temp) {
          int[] frac = new int[] {temp[0], temp[1]};
          int curr = gcd(frac[0], frac[1]);
          while (curr != 1) {
              frac[0] /= curr;
              frac[1] /= curr;
              curr = gcd(frac[0], frac[1]);
          }
          return frac;
      }
      static int summation(int x) {
          return x * (x+1) / 2;
      }  
      static long summation(long x) {
          return x*(x+1) / 2;
      }
      static long pow(long num, long exp, long mod){
          long ans=1;
          for(int i=1;i<=exp;i++){
              ans=(ans*num)%mod;
          }
          return ans;
      }
      static boolean isPrime(int n)
      {
   
          if (n <= 1)
              return false;
          else if (n == 2)
              return true;
          else if (n % 2 == 0)
              return false;
          for (int i = 3; i <= Math.sqrt(n); i += 2) {
              if (n % i == 0)
                  return false;
          }
          return true;
      }
      static int gcd(int a, int b)
      {
          if (a == 0)
              return b; 
          return gcd(b % a, a); 
      }
      static long gcd(long a, long b) {
          if (a == 0) return b;
          return gcd(b % a, a);
      }
      static int lcm(int a, int b)
      {
          return (a / gcd(a, b)) * b;
      }
      public static void sort(int[] arr) {
          Random rgen = new Random();
          for (int i = 0; i < arr.length; i++) {
              int r = rgen.nextInt(arr.length);
              int temp = arr[i];
              arr[i] = arr[r];
              arr[r] = temp;
          }
          Arrays.sort(arr);
      }
      public static void sort(long[] arr) {
          Random rgen = new Random();
          for (int i = 0; i < arr.length; i++) {
              int r = rgen.nextInt(arr.length);
              long temp = arr[i];
              arr[i] = arr[r];
              arr[r] = temp;
          }
          Arrays.sort(arr);
      }
      static int charint(char c) {
          return Integer.parseInt(String.valueOf(c));
      }
      
      /* */
      //printing methods
      /* */
      //WOW!
      /* */
      public static void printArr(PrintWriter pw, int[] arr) {
          StringBuilder sb = new StringBuilder();
          for (int x : arr) {
              sb.append(x + "");
          }
          sb.setLength(sb.length() - 1);
          pw.println(sb.toString());
      }
      public static void printArr2d(PrintWriter pw, int[][] arr) {
          StringBuilder sb = new StringBuilder();
          for (int[] row : arr) {
              for (int x : row) {
                  sb.append(x + " ");
              }
              sb.setLength(sb.length() - 1);
              sb.append("\n");
          }
          pw.println(sb.toString());
      }
  }
   
  class FastScanner {
      BufferedReader br;
      StringTokenizer st;
   
      public FastScanner() {
          br = new BufferedReader(new InputStreamReader(System.in), 32768);
          st = null;
      }
   
      String next() {
          while (st == null || !st.hasMoreElements()) {
              try {
                  st = new StringTokenizer(br.readLine());
              } catch (IOException e) {
                  e.printStackTrace();
                  
              }
          }
          return st.nextToken();
      }
   
      int ni() {
          return Integer.parseInt(next());
      }
   
      int[] intArray(int N) {
          int[] ret = new int[N];
          for (int i = 0; i < N; i++)
              ret[i] = ni();
          return ret;
      }
   
      long nl() {
          return Long.parseLong(next());
      }
   
      long[] longArray(int N) {
          long[] ret = new long[N];
          for (int i = 0; i < N; i++)
              ret[i] = nl();
          return ret;
      }
   
      double nd() {
          return Double.parseDouble(next());
      }
   
      String nextLine() {
          String str = "";
          try {
              str = br.readLine();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return str;
      }
  }
   
  class MultiSet {
      TreeMap<Long, Integer> map = new TreeMap<>();
      private int size = 0;
      public MultiSet() {
      }
      public void add(Long val) {
          map.put(val, map.getOrDefault(val, 0) + 1);
          size++;
      }
      public void remove(Long val) {
          map.put(val, map.get(val) - 1);
          if (map.get(val) == 0) {
              map.remove(val);
          }
          size--;
      }
      public int size() {
          return size;
      }
      public Long higher(Long val) {
          return map.higherKey(val);
      }
      public Long lower(Long val) {
          return map.lowerKey(val);
      }
      public Long ceiling(Long val) {
          return map.ceilingKey(val);
      }
      public Long floor(Long val) {
          return map.floorKey(val);
      }
      public Long first() {
          return map.firstKey();
      }
      public Long last() {
          return map.lastKey();
      }
      public boolean isEmpty() {
          return map.isEmpty();
      }
  }