#include #include #include #include #include #include #include #include #include #include #include #include #define N 222 #define M 2000100 #define LL long long #define INF 0x3f3f3f3f #define FF(i,a,b) for(int i=a;i<=b;++i) #define RR(i,a,b) for(int i=a;i>=b;--i) #define FJ(i,a,b) for(int i=a;i>1) #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define zero(x) (((x)>0?(x):-(x)) #define MP make_pair #define SZ size() #define IN freopen("in.txt","r",stdin) #define OUT freopen("out.txt","w",stdout) using namespace std; const int MOD=1000000007; const double PI=acos(-1.0); const double EPS=1e-8; int sgn(double x){return(x>EPS)-(x<-EPS);} char s[111000]; int main(){ int _;SC(_); FF(ca,1,_){ SS(s); int l=strlen(s); LL t=0; LL ans=0; FJ(i,0,l){ if(s[i]=='q')++t; else { ans=ans+(1+t)*t/2; t=0; } } if(t>0)ans=ans+(1+t)*t/2; printf("%I64d\n",ans); } return 0; }