#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include<stdlib.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
//DP,两种状态,lock,notlock的两种状态;
int min(int a,int b)
{
return a<b?a:b;
}
#define N 110
int main()
{
int t,i;
int lock[N];
int notlock[N];
char s[N];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int n=strlen(s);
if(s[0]>='A'&&s[0]<='Z')
{
notlock[0]=2;
lock[0]=2;
}
else
{
notlock[0]=1;
lock[0]=2;
}
for(i=1;i<n;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
lock[i]=min(lock[i-1]+1,notlock[i-1]+2);
notlock[i]=min(lock[i-1]+2,notlock[i-1]+2);
}
else
{
lock[i]=min(notlock[i-1]+2,lock[i-1]+2);
notlock[i]=min(notlock[i-1]+1,lock[i-1]+2);
}
}
int q=min(lock[n-1]+1,notlock[n-1]);
printf("%d
",q);
}
return 0;
}