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: import java.util.*;
  import java.io.*;
  import java.util.stream.Stream;
  
  public class D
  {
      static InputReader in=new InputReader(System.in);
      static OutputWriter out=new OutputWriter(System.out);
      static StringBuilder sb=new StringBuilder();
      static int n;
      static int m1;
      static int m2;
  
      // Main Class Starts Here
      public static void main(String[] args)throws IOException
      {
          // Write your code.
  
          long powOf2[] = new long[64];
          for(int i = 0; i < 64; i++){
              powOf2[i] = (long)Math.pow(2, i);
          }
          // prln(Arrays.toString(powOf2));
  
          int t = in();
          while(t-->0) {
              int n = in();
              int min = 100;
              long ans = -1L;
              for(long pow: powOf2){
                  int ops = calcOps(n, pow);
                  if(min > ops){
                      min = ops;
                      ans = pow;
                  }
              }
              // prln(n+" <-> "+ans+" , "+min);
              app(min+"\n");
          }
          out.printLine(sb);
          out.close();
      }
  
      public static int calcOps(long n, long x){
          char txt[] = Long.toString(n).toCharArray();
          char pattern[] = Long.toString(x).toCharArray();
          int i = 0;
          int j = 0;
          while(i < txt.length && j < pattern.length){
              if(txt[i] == pattern[j]){
                  j++;
              }
              i++;
          }
                     // add         +     // remove
          return pattern.length - j + txt.length - j;
      }
  
      static class Pair{
          int x, y;
          public Pair(int x, int y){
              this.x = x;
              this.y = y;
          }
  
          @Override
          public String toString(){
              return "("+x+","+y+")";
          }
      }
  
      static class DisjointSet{
          int parent[];
          int rank[];
          int size;
  
          public DisjointSet(int n){
              this.parent = new int[n];
              this.rank = new int[n];
              this.size = n;
              for(int i = 0; i < n; i++)
                  parent[i] = i;
          }
  
          public int findParent(int x){
              if(parent[x] != x){
                  return parent[x] = findParent(parent[x]);
              }
              return parent[x];
          }
  
          public boolean inSameComponents(int x, int y){
              int parentX = findParent(x);
              int parentY = findParent(y);
              return parentX == parentY;
          }
  
          public int mergeComponents(int x, int y){
              int parentX = findParent(x);
              int parentY = findParent(y);
              if(parentX == parentY) {
                  return 0; // merge failed as in the same components hence no change in components
              }
              if(rank[parentX] < rank[parentY]){
                  parent[parentX] = parentY;
              }
              else if(rank[parentX] > rank[parentY]){
                  parent[parentY] = parentX;
              }
              else{
                  // merge component of y to x.
                  parent[parentY] = parentX;
                  rank[parentX]++;
              }
              return 1; // merge successful - decrease in merge components
          }
  
          public int countComponents(){
              int count = 0;
              for(int i = 0; i < size; i++)
                  if(parent[i] == i) count++;
              return count;
          }
  
          public TreeMap<Integer, List<Integer>> getComponents(){
              TreeMap<Integer, List<Integer>> componentMap = new TreeMap<>();
  
              for(int i = 0; i < size; i++){
                  List<Integer> componentElements = componentMap.getOrDefault(parent[i], new ArrayList<>());
                  componentElements.add(i);
                  componentMap.put(parent[i], componentElements);
              }
  
              return componentMap;
          }
  
          public Set<Integer> getComponentsWithNode(int x){
              HashSet<Integer> set = new HashSet<>();
              int parentX = findParent(x);
              for(int i = 0; i < n; i++){
                  if(parent[i] == parentX){
                      set.add(i);
                  }
              }
              return set;
          }
      }
  
      // segment tree.
      static class SegmentTree{
          int size;
          List<Integer> st[];
          int n;
          public SegmentTree(int a[], int n){
              int x = (int)Math.ceil((Math.log(n) / Math.log(2)));
              this.size = (int)Math.pow(2, 2 * x) - 1;
              st = new ArrayList[size];
              buildTree(0, n-1, 0, a);
              this.n = n;
          }
  
          public List<Integer> buildTree(int l, int r, int curIndex, int a[]){
              if(l == r){
                  st[curIndex] =  new ArrayList<>();
                  st[curIndex].add(a[l]);
                  return st[curIndex];
              }
              int mid = (l + r) / 2;
              List<Integer> leftList = buildTree(l, mid, 2 * curIndex + 1, a);
              List<Integer> rightList = buildTree(mid + 1, r, 2 * curIndex + 2, a);
              int lastLeftProduct = leftList.get(leftList.size() - 1);
              int firstRightProduct = rightList.get(0);
              int gcd = gcd(lastLeftProduct, firstRightProduct);
              if(gcd > 1){
                  st[curIndex] = new ArrayList<>(leftList);
                  st[curIndex].addAll(rightList);
              }
              else{
                  st[curIndex] = new ArrayList<>();
                  for(int i = 0; i < leftList.size() - 1; i++)
                      st[curIndex].add(leftList.get(i));
                  st[curIndex].add(lastLeftProduct * firstRightProduct);
                  for(int i = 1 ; i < rightList.size(); i++)
                      st[curIndex].add((rightList.get(i)));
              }
              return st[curIndex];
          }
  
          public int resolveQuery(int ql, int qr){
              List<Integer> ans = mergeRanges(0, n-1, ql, qr, 0);
              return ans.size();
          }
  
          public List<Integer> mergeRanges(int l, int r, int ql, int qr, int curIndex){
              if(l > qr || r < ql) return null;
              if(ql <= l && qr >= r){
                  return st[curIndex];
              }
              int mid = (l + r) / 2;
              List<Integer> leftList = mergeRanges(l, mid, ql, qr, 2 * curIndex + 1);
              List<Integer> rightList = mergeRanges(mid + 1, r, ql, qr, 2 * curIndex + 2);
              if(leftList != null && rightList != null){
                  int lastLeftProduct = leftList.get(leftList.size() - 1);
                  int firstRightProduct = rightList.get(0);
                  int gcd = gcd(lastLeftProduct, firstRightProduct);
                  List<Integer> mergedList = new ArrayList<>();
                  if(gcd > 1){
                      mergedList.addAll(leftList);
                      mergedList.addAll(rightList);
                  }
                  else{
                      for(int i = 0; i < leftList.size() - 1; i++)
                          mergedList.add(leftList.get(i));
                      mergedList.add(lastLeftProduct * firstRightProduct);
                      for(int i = 1 ; i < rightList.size(); i++)
                          mergedList.add((rightList.get(i)));
                  }
                  return mergedList;
              }
              else if(leftList != null) return leftList;
              else return rightList;
          }
  
          public void printTree(){
              for(int i = 0; i < st.length; i++)
                  prln(i+" , "+st[i]);
          }
      }
  
      public static String atsi(int a[]){
          return Arrays.toString(a);
      }
  
      public static String atsl(long a[]){
          return Arrays.toString(a);
      }
  
      public static long gcd(long a, long b){
          if(b == 0) return a;
          return gcd(b, a % b);
      }
  
      public static int gcd(int a, int b){
          if(b == 0) return a;
          return gcd(b, a % b);
      }
  
      public static int[] an(int n) {
          int ar[]=new int[n];
          for(int i=0;i<n;i++)
              ar[i]=in();
          return ar;
      }
  
      public static long[] lan(int n) {
          long ar[]=new long[n];
          for(int i=0;i<n;i++)
              ar[i]=lin();
          return ar;
      }
  
      public static String atos(Object ar[]) {
          return Arrays.toString(ar);
      }
  
      public static int in() {
          return in.readInt();
      }
  
      public static long lin() {
          return in.readLong();
      }
  
      public static String sn() {
          return in.readString();
      }
  
      public static void prln(Object o) {
          out.printLine(o);
      }
  
      public static void prn(Object o) {
          out.print(o);
      }
  
      public static void app(Object o) {
          sb.append(o);
      }
  
  
      static class InputReader {
          private InputStream stream;
          private byte[] buf = new byte[1024];
          private int curChar;
          private int numChars;
          private SpaceCharFilter filter;
  
          public InputReader(InputStream stream) {
              this.stream = stream;
          }
  
          public int read() {
              if (numChars == -1)
                  throw new InputMismatchException();
              if (curChar >= numChars) {
                  curChar = 0;
                  try {
                      numChars = stream.read(buf);
                  } catch (IOException e) {
                      throw new InputMismatchException();
                  }
                  if (numChars <= 0)
                      return -1;
              }
              return buf[curChar++];
          }
  
          public int readInt() {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-') {
                  sgn = -1;
                  c = read();
              }
              int res = 0;
              do {
                  if (c < '0' || c > '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public long readLong() {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              int sgn = 1;
              if (c == '-') {
                  sgn = -1;
                  c = read();
              }
              long res = 0;
              do {
                  if (c < '0' || c > '9')
                      throw new InputMismatchException();
                  res *= 10;
                  res += c - '0';
                  c = read();
              } while (!isSpaceChar(c));
              return res * sgn;
          }
  
          public String readString() {
              int c = read();
              while (isSpaceChar(c))
                  c = read();
              StringBuilder res = new StringBuilder();
              do {
                  res.appendCodePoint(c);
                  c = read();
              } while (!isSpaceChar(c));
              return res.toString();
          }
  
          public boolean isSpaceChar(int c) {
              if (filter != null)
                  return filter.isSpaceChar(c);
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
          }
  
          public String next() {
              return readString();
          }
  
          public interface SpaceCharFilter {
              public boolean isSpaceChar(int ch);
          }
      }
  
      static class OutputWriter {
          private final PrintWriter writer;
  
          public OutputWriter(OutputStream outputStream) {
              writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
          }
  
          public OutputWriter(Writer writer) {
              this.writer = new PrintWriter(writer);
          }
  
          public void print(Object...objects) {
              for (int i = 0; i < objects.length; i++) {
                  if (i != 0)
                      writer.print(' ');
                  writer.print(objects[i]);
              }
          }
  
          public void printLine(Object...objects) {
              print(objects);
              writer.println();
          }
  
          public void close() {
              writer.close();
          }
      }
  }