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: /*
   * Java Coding Template for Competitive Programming
   * Author: Kartik Gola
   * Utilities Source: https://github.com/kartikgola/well-known-problems
   * Copyright (c) 2021 | https://kartikgola.com
   */
  
  import java.io.*;
  import java.util.*;
  import java.lang.*;
  
  public class D {
  
      private static int f(String s, String x) {
          int match = 0;
          int i = 0, j = 0;
          while (i < s.length() && j < x.length()) {
              if (s.charAt(i) == x.charAt(j)) {
                  ++match;
                  ++i;
                  ++j;
              } else {
                  ++i;
              }
          }
          return s.length()-match+x.length()-match;
      }
  
      public static void main(String[] args) throws Exception {
          // Comment setIO(); if using AtCoder
          setIO();
          int t = nextInt();
          List<Long> al = new ArrayList<>();
          for (long i = 0; (1l << i) < (long)1e18; ++i)
              al.add(1l << i);
  
          while (t-- > 0) {
              int ans = Integer.MAX_VALUE;
              String s = nextString();
              for (long x: al)
                  ans = Math.min(ans, f(s, Long.toString(x)));
              System.out.println(ans);
          }
      }
  
      /* -----------------------------Common Utilities--------------------------- */
      private static class Pair<T, K> {
          T key;
          K value;
          public Pair(T key, K value) {
              this.key = key;
              this.value = value;
          }
      }
  
  
      /* -----------------------------String Utilities--------------------------- */
      private static boolean isPalindrome(String s) { return isPalindrome(s, 0, s.length()-1); }
  
      private static boolean isPalindrome(String s, int start, int end) {
          for (int i = start, j = end; i < j; ++i, --j)
              if (s.charAt(i) != s.charAt(j))
                  return false;
          return true;
      }
  
  
      /* -----------------------------Math Utilities----------------------------- */
      // Returns seive of prime numbers upto (not including) the given number
      private static boolean[] primeNumbersSeive(int number) {
          boolean[] seive = new boolean[number];
          if ( number > 2 ) {
              for ( int num = 2; num * num <= number - 1; ++num ) {
                  if ( !seive[num] ) {
                      for ( int mult = num * 2; mult < seive.length; mult += num ) {
                          seive[mult] = true;
                      }
                  }
              }
          }
          return seive;
      }
  
      private static boolean isPalindrome(long number) {
          String str = Long.toString(number);
          for (int i = 0, j = str.length() - 1; i <= j; ++i, --j)
              if (str.charAt(i) != str.charAt(j)) return false;
          return true;
      }
  
      private static long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); }
  
      private static long gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
  
  
      /* -----------------------------Graph Utilities---------------------------- */
      private static class UnionFind {
  
          private int groups;
          private final int[] parent;
          private boolean zeroIndexed = true;
  
          public UnionFind(int size) {
              groups = size;
              parent = new int[size];
              Arrays.fill(parent, -1);
          }
  
          public UnionFind(int size, boolean zeroIndexed) {
              groups = size;
              if (zeroIndexed)
                  parent = new int[size];
              else
                  parent = new int[size+1];
              Arrays.fill(parent, -1);
              this.zeroIndexed = zeroIndexed;
          }
  
          public int getTotalGroups() {
              return this.groups;
          }
  
          public int find(int u) {
              if ( parent[u] < 0 )
                  return u;
              return parent[u] = find(parent[u]);
          }
  
          public boolean union(int u, int v) {
              int pu = find(u);
              int pv = find(v);
              if ( pu != pv ) {
                  // Weight of pu is more (more negative means higher weight)
                  if ( parent[pu] <= parent[pv] ) {
                      parent[pv] = pu;
                      parent[pu]--;
                  } else {
                      parent[pu] = pv;
                      parent[pv]--;
                  }
                  groups--;
                  return true;
              }
              // In case parents of 'u' and 'v' are same, we return false
              return false;
          }
      }
  
      private static class IntGraph {
          public final Map<Integer, Map<Integer, Integer>> adj = new HashMap<>();
          public List<int[]> edges;
          public final int size;
          public final boolean isDirected;
          public IntGraph(int size, boolean isDirected) {
              this.size = size;
              this.isDirected = isDirected;
              this.edges = new ArrayList<>(size);
              for (int u = 0; u < size; ++u)
                  adj.put(u, new HashMap<>());
          }
          public IntGraph(int size, boolean isDirected, int[][] edges) {
              this(size, isDirected);
              for (int[] edge: edges)
                  addEdge(edge);
          }
          public void addEdge(int[] edge) {
              int from = edge[0],
                      to = edge[1],
                      weight = edge.length == 3 ? edge[2] : 0;
              adj.get(from).put(to, weight);
              edges.add(new int[]{from, to, weight});
              if (!isDirected) {
                  adj.get(to).put(from, weight);
                  edges.add(new int[]{to, from, weight});
              }
          }
  
          private boolean isCyclic(int u, boolean[] visited, boolean[] path) {
              if (!visited[u]) {
                  visited[u] = true;
                  path[u] = true;
                  for (int v: adj.get(u).keySet()) {
                      if (!visited[v]) {
                          if (isCyclic(v, visited, path))
                              return true;
                      } else if (path[v])
                          return true;
                  }
              }
              path[u] = false;
              return false;
          }
          public boolean isCyclic() {
              boolean[] visited = new boolean[size];
              boolean[] path = new boolean[size];
              for (int u = 0; u < size; ++u) {
                  if (!visited[u] && isCyclic(u, visited, path)) {
                      return true;
                  }
              }
              return false;
          }
  
          public boolean isTree() {
              boolean[] visited = new boolean[size];
              boolean[] path = new boolean[size];
              if (isCyclic(0, visited, path))
                  return false;
              for (boolean vis: visited)
                  if (!vis) return false;
              return true;
          }
  
          public int[] topologicalSort() {
              int[] indegree = new int[size];
              int[] ans = new int[size];
              boolean[] visited = new boolean[size];
              Queue<Integer> q = new LinkedList<>();
              adj.values().stream().map(Map::keySet).flatMap(Collection::stream).forEach(u -> indegree[u]++);
              for (int u = 0; u < size; u++) {
                  if (indegree[u] == 0) {
                      q.add(u);
                  }
              }
              int j = 0;
              while (!q.isEmpty()) {
                  int u = q.poll();
                  ans[j++] = u;
                  for (int v: adj.get(u).keySet()) {
                      if (!visited[v] && --indegree[v] == 0) {
                          q.add(v);
                          visited[v] = true;
                      }
                  }
              }
              if (j < size)
                  return new int[]{};
              return ans;
          }
  
          public int[] dijkstra(int source) {
              int[] dist = new int[size];
              boolean[] visited = new boolean[size];
              Arrays.fill(dist, Integer.MAX_VALUE);
              dist[source] = 0;
              Queue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(v -> v[0]));
              pq.add(new int[]{0, source});
  
              while (!pq.isEmpty()) {
                  int d = pq.peek()[0],
                          u = pq.poll()[1];
                  if (visited[u])
                      continue;
                  visited[u] = true;
                  for (Map.Entry<Integer, Integer> e: adj.get(u).entrySet()) {
                      int v = e.getKey(),
                              w = e.getValue();
                      if (d + w < dist[v]) {
                          dist[v] = d + w;
                          pq.add(new int[]{dist[v], v});
                      }
                  }
              }
  
              return dist;
          }
  
          public int[] bellmanFord(int source) {
              int[] dist = new int[size];
              Arrays.fill(dist, Integer.MAX_VALUE);
              dist[source] = 0;
              for (int i = 0; i < size-1; ++i) {
                  for (int[] edge: edges) {
                      int from = edge[0], to = edge[1], weight = edge[2];
                      if (dist[from] != Integer.MAX_VALUE && dist[from] + weight < dist[to]) {
                          dist[to] = dist[from] + weight;
                      }
                  }
              }
              for (int[] edge: edges) {
                  int from = edge[0], to = edge[1], weight = edge[2];
                  if (dist[from] != Integer.MAX_VALUE && dist[from] + weight < dist[to]) {
                      // Graph has -ve weight cycle
                      return new int[]{};
                  }
              }
              return dist;
          }
  
          public int[][] floydWarshall() {
              int[][] dist = new int[size][size];
              for (int u = 0; u < size; ++u) {
                  Arrays.fill(dist[u], Integer.MAX_VALUE);
                  dist[u][u] = 0;
                  for (Map.Entry<Integer, Integer> e: adj.get(u).entrySet()) {
                      dist[u][e.getKey()] = e.getValue();
                  }
              }
              for (int k = 0; k < size; ++k) {
                  for (int u = 0; u < size; ++u) {
                      for (int v = 0; v < size; ++v) {
                          if (dist[u][k] != Integer.MAX_VALUE && dist[k][v] != Integer.MAX_VALUE
                                  && dist[u][k] + dist[k][v] < dist[u][v]) {
                              dist[u][v] = dist[u][k] + dist[k][v];
                          }
                      }
                  }
              }
              return dist;
          }
  
          public IntGraph kruskal() {
              IntGraph mst = new IntGraph(size, isDirected);
              edges.sort(Comparator.comparingInt(e -> e[2]));
              UnionFind uf = new UnionFind(size);
              for (int[] edge: edges) {
                  if (uf.union(edge[0], edge[2])) {
                      mst.addEdge(edge);
                  }
              }
              return mst;
          }
  
          public IntGraph prim() {
              boolean[] visited = new boolean[size];
              Queue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e[2]));
              IntGraph mst = new IntGraph(size, isDirected);
              int randomSource = new Random().nextInt(size);
              for (Map.Entry<Integer, Integer> edge: adj.get(randomSource).entrySet())
                  pq.add(new int[]{randomSource, edge.getKey(), edge.getValue()});
              visited[randomSource] = true;
  
              while (!pq.isEmpty()) {
                  int u = pq.peek()[0],
                          v = pq.peek()[1],
                          w = pq.poll()[2];
                  if (visited[v])
                      continue;
                  visited[v] = true;
                  mst.addEdge(new int[]{u, v, w});
                  for (Map.Entry<Integer, Integer> edge: adj.get(v).entrySet())
                      pq.add(new int[]{v, edge.getKey(), edge.getValue()});
              }
              return mst;
          }
      }
  
  
      /* -----------------------------Array Utilities--------------------------- */
      public static int bisectLeft(List<Integer> arr, int value) {
          if (arr.isEmpty()) return 0;
          int l = 0, r = arr.size()-1;
          while (l < r) {
              int m = l + (r-l) / 2;
              if (arr.get(m) == value) {
                  while (m-1 > -1 && arr.get(m-1) == value) --m;
                  return m;
              } else if (arr.get(m) < value) l = m+1;
              else r = m-1;
          }
          return arr.get(l) < value ? l+1 : l;
      }
  
      public static int bisectLeft(int[] arr, int value) {
          if (arr.length == 0) return 0;
          int l = 0, r = arr.length-1;
          while (l < r) {
              int m = l + (r-l) / 2;
              if (arr[m] == value) {
                  while (m-1 > -1 && arr[m-1] == value) --m;
                  return m;
              } else if (arr[m] < value) l = m+1;
              else  r = m-1;
          }
          return arr[l] < value ? l+1 : l;
      }
  
      public static int bisectRight(int[] arr, int value) {
          if (arr.length == 0) return 0;
          int l = 0, r = arr.length-1;
          while (l < r) {
              int m = l + (r-l) / 2;
              if (arr[m] == value) {
                  while (m+1 < arr.length && arr[m+1] == value) ++m;
                  return m+1;
              } else if (arr[m] < value) l = m+1;
              else r = m-1;
          }
          return arr[l] <= value ? l+1 : l;
      }
  
      public static int bisectRight(List<Integer> arr, int value) {
          if (arr.isEmpty())  return 0;
          int l = 0, r = arr.size()-1;
          while (l < r) {
              int m = l + (r-l) / 2;
              if (arr.get(m) == value) {
                  while (m+1 < arr.size() && arr.get(m+1) == value) ++m;
                  return m+1;
              } else if (arr.get(m) < value) l = m+1;
              else r = m-1;
          }
          return arr.get(l) <= value ? l+1 : l;
      }
  
  
      /* -----------------------------IO Utilities----------------------------- */
      private static BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
      private static StringTokenizer tkn = new StringTokenizer("");
  
      // "ONLINE_JUDGE" property
      // works on LeetCode, CodeForces, CodeChef
      // does not work on AtCoder
      private static void setIO() throws Exception {
          if (System.getProperty("ONLINE_JUDGE") == null) {
              rd = new BufferedReader(new FileReader("input.txt"));
              System.setOut(new PrintStream(new FileOutputStream("output.txt")));
          }
      }
  
      // Integer
      private static int nextInt() throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          return Integer.parseInt(tkn.nextToken());
      }
  
      private static int[] nextInts(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          int[] ans = new int[n];
          for (int i = 0; i < n; i++) ans[i] = Integer.parseInt(tkn.nextToken());
          return ans;
      }
  
      private static List<Integer> nextIntList(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          List<Integer> ans = new ArrayList<>(n);
          for (int i = 0; i < n; i++) ans.add(Integer.parseInt(tkn.nextToken()));
          return ans;
      }
  
      // Long
      private static long nextLong() throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          return Long.parseLong(tkn.nextToken());
      }
  
      private static long[] nextLongs(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          long[] ans = new long[n];
          for (int i = 0; i < n; i++) ans[i] = Long.parseLong(tkn.nextToken());
          return ans;
      }
  
      private static List<Long> nextLongList(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          List<Long> ans = new ArrayList<>(n);
          for (int i = 0; i < n; i++) ans.add(Long.parseLong(tkn.nextToken()));
          return ans;
      }
  
      // Double
      private static double nextDouble() throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          return Double.parseDouble(tkn.nextToken());
      }
  
      private static double[] nextDouble(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          double[] ans = new double[n];
          for (int i = 0; i < n; i++) ans[i] = Double.parseDouble(tkn.nextToken());
          return ans;
      }
  
      private static List<Double> nextDoubleList(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          List<Double> ans = new ArrayList<>(n);
          for (int i = 0; i < n; i++) ans.add(Double.parseDouble(tkn.nextToken()));
          return ans;
      }
  
      // String
      private static String nextString() throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          return tkn.nextToken();
      }
  
      private static String[] nextStrings(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          String[] ans = new String[n];
          for (int i = 0; i < n; i++) ans[i] = tkn.nextToken();
          return ans;
      }
  
      private static List<String> nextStringList(int n) throws Exception {
          while (!tkn.hasMoreTokens()) tkn = new StringTokenizer(rd.readLine());
          List<String> ans = new ArrayList<>(n);
          for (int i = 0; i < n; i++) ans.add(tkn.nextToken());
          return ans;
      }
  
  }