public static void main ( String args [ ] ) {
  Scanner s = new Scanner ( System . in ) ;
  int t = s . nextInt ( ) ;
  while ( t -- != 0 ) {
    String str = s . next ( ) ;
    int n = str . length ( ) ;
    int prefix0 [ ] = new int [ n ] ;
    int prefix1 [ ] = new int [ n ] ;
    int suffix0 [ ] = new int [ n ] ;
    int suffix1 [ ] = new int [ n ] ;
    prefix0 [ 0 ] = str . charAt ( 0 ) == '0' ? 1 : 0 ;
    prefix1 [ 0 ] = str . charAt ( 0 ) - '0' ;
    for ( int i = 1 ;
    i < str . length ( ) ;
    i ++ ) {
      prefix0 [ i ] = prefix0 [ i - 1 ] ;
      prefix1 [ i ] = prefix1 [ i - 1 ] ;
      if ( str . charAt ( i ) == '0' ) {
        prefix0 [ i ] ++ ;
      }
      else {
        prefix1 [ i ] ++ ;
      }
    }
    suffix0 [ n - 1 ] = str . charAt ( n - 1 ) == '0' ? 1 : 0 ;
    suffix1 [ n - 1 ] = str . charAt ( n - 1 ) - '0' ;
    for ( int i = n - 2 ;
    i >= 0 ;
    i -- ) {
      suffix0 [ i ] = suffix0 [ i + 1 ] ;
      suffix1 [ i ] = suffix1 [ i + 1 ] ;
      if ( str . charAt ( i ) == '0' ) {
        suffix0 [ i ] ++ ;
      }
      else {
        suffix1 [ i ] ++ ;
      }
    }
    int ans = Integer . MAX_VALUE ;
    for ( int i = 0 ;
    i < n ;
    i ++ ) {
      int opt = Math . min ( n - prefix0 [ i ] - suffix1 [ i ] , n - prefix1 [ i ] - suffix0 [ i ] ) ;
      ans = Math . min ( ans , opt ) ;
    }
    System . out . println ( ans ) ;
  }
}

public static void main ( String args [ ] ) { Scanner s = new Scanner ( System . in ) ; int t = s . nextInt ( ) ; while ( t -- != 0 ) { String str = s . next ( ) ; int n = str . length ( ) ; int prefix0 [ ] = new int [ n ] ; int prefix1 [ ] = new int [ n ] ; int suffix0 [ ] = new int [ n ] ; int suffix1 [ ] = new int [ n ] ; prefix0 [ 0 ] = str . charAt ( 0 ) == '0' ? 1 : 0 ; prefix1 [ 0 ] = str . charAt ( 0 ) - '0' ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { prefix0 [ i ] = prefix0 [ i - 1 ] ; prefix1 [ i ] = prefix1 [ i - 1 ] ; if ( str . charAt ( i ) == '0' ) { prefix0 [ i ] ++ ; } else { prefix1 [ i ] ++ ; } } suffix0 [ n - 1 ] = str . charAt ( n - 1 ) == '0' ? 1 : 0 ; suffix1 [ n - 1 ] = str . charAt ( n - 1 ) - '0' ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix0 [ i ] = suffix0 [ i + 1 ] ; suffix1 [ i ] = suffix1 [ i + 1 ] ; if ( str . charAt ( i ) == '0' ) { suffix0 [ i ] ++ ; } else { suffix1 [ i ] ++ ; } } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int opt = Math . min ( n - prefix0 [ i ] - suffix1 [ i ] , n - prefix1 [ i ] - suffix0 [ i ] ) ; ans = Math . min ( ans , opt ) ; } System . out . println ( ans ) ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier) dimensions: (dimensions))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (character_literal)) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (character_literal)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal))) consequence: (block (expression_statement (update_expression (array_access array: (identifier) index: (identifier))))) alternative: (block (expression_statement (update_expression (array_access array: (identifier) index: (identifier)))))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (character_literal)) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (character_literal)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal))) consequence: (block (expression_statement (update_expression (array_access array: (identifier) index: (identifier))))) alternative: (block (expression_statement (update_expression (array_access array: (identifier) index: (identifier)))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (binary_expression left: (identifier) right: (array_access array: (identifier) index: (identifier))) right: (array_access array: (identifier) index: (identifier))) (binary_expression left: (binary_expression left: (identifier) right: (array_access array: (identifier) index: (identifier))) right: (array_access array: (identifier) index: (identifier))))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))))