#include <iostream>
#include <iomanip>
#include <cmath>
#include <sstream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
scanf("%d\n",&n);
while(n--){
int p=0,pair=0;
while(1){
char c=getchar();
if(c=='\n')
break;
if(c=='p')
p++;
else if(c=='q'&&p>0){
p--;
pair++;
}
}
printf("%d\n",pair);
}
return 0;
}