/*
    不錯的一道題
    題意:給出一棵二叉樹,有權值,求改變最小的點使平衡

    其實,有些點不用改變,所以如果第k層的點a不用改變,則平衡時其根的值為a*2^k
    所以算出所有點,如果它不用改變時對應根的值,存在數(shù)組里
    然后求這個數(shù)組里相同個數(shù)最多的Max,答案就是tot-Max了

    還有,遇到[表示層次++,遇到]表示層次--
*/

#include
<cstdio>
#include
<cstring>
#include
<algorithm>
using namespace std;

const int MAXN=1000010;

char str[MAXN];
long long ans[MAXN];

int main(){
    
int T;
    scanf(
"%d",&T);
    
while(T--){
        scanf(
"%s",str);
        
int level=0,tot=0;
        
for(int i=0;str[i];i++){
            
if(str[i]=='[')level++;
            
else if(str[i]==']')level--;
            
else if(str[i]<='9'&&str[i]>='0'){
                
long long tmp=0;
                
for(;str[i]&&str[i]<='9'&&str[i]>='0';i++)
                    tmp
=tmp*10+str[i]-'0';
                i
--;
                tmp
=tmp<<level;
                ans[tot
++]=tmp;
            }

        }

        sort(ans,ans
+tot);
        
int Max=1,cnt=0;
        
for(int i=0;i<tot;i++){
            
if(i==0||ans[i]!=ans[i-1])cnt=1;
            
else {
                cnt
++;
                
if(Max<cnt)Max=cnt;
            }

        }

        printf(
"%d\n",tot-Max);
    }

    
return 0;
}