#include<bits/stdc++.h>
#define ld long double
#define ll long long int
#define float long double
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define ALL(a) a.begin(),a.end()
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DEBUG(x) cout<<" x is "<<x<<"\n"
using namespace std;
//inline size_t key( int a,int b,int c,int d) {return a*b*c*d-7707;}
//unordered_map<size_t,bool >vismap;
/*int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 2 Direction
/*int dx[] = {1,0} , dy[] = {0,1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
ll S,M,SM,MS,SMS;
string s;
const ll N=1000000+10;
const ll sqN=320;
int ans[N];
struct query{
ll l;
ll r;
ll idx;
} Q[N];
bool comp(query &q1,query &q2){
if(q1.l/sqN !=q2.l/sqN){
return q1.l <q2.l;
}
if((q1.l/sqN) &1){
return q1.r <q2.r;
}
return q1.r>q2.r;
}
inline void addFront(ll i){
if(s[i]=='s'){
SMS+=MS;
SM+=M;
S++;
}
else if(s[i]=='m'){
MS+=S;
M++;
}
}
inline void addRear(ll i){
if(s[i]=='s'){
SMS+=SM;
S++;
MS+=M;
}
else if(s[i]=='m'){
SM+=S;
M++;
}
}
inline void remFront(ll i){
if(s[i]=='s'){
SMS-=MS;
SM-=M;
S--;
}
else if(s[i]=='m'){
MS-=S;
M--;
}
}
inline void remRear(ll i){
if(s[i]=='s'){
SMS-=SM;
S--;
MS-=M;
}
else if(s[i]=='m'){
SM-=S;
M--;
}
}
int main(){
fast
cin>>s;
ll q,l,r;
cin>>q;
for(ll i=0;i<q;i++){
cin>>l>>r;
l--;r--;
Q[i].l=l;
Q[i].r=r;
Q[i].idx=i;
}
sort(Q,Q+q,comp);
l=0,r=-1;
for(ll i=0;i<q;i++){
ll L=Q[i].l;
ll R=Q[i].r;
if(L>R){
ans[Q[i].idx]=0;
continue;
}
while(l<L){
remFront(l);
l++;
}
while(l>L){
l--;
addFront(l);
}
while(r<R){
r++;
addRear(r);
}
while(r>R){
remRear(r);
r--;
}
ans[Q[i].idx]=SMS;
}
for(ll i=0;i<q;i++){
cout<<ans[i]<<"\n";
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCiNkZWZpbmUgbGQgbG9uZyBkb3VibGUKI2RlZmluZSBsbCBsb25nIGxvbmcgaW50CiNkZWZpbmUgZmxvYXQgbG9uZyBkb3VibGUKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtayBtYWtlX3BhaXIKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwojZGVmaW5lIEFMTChhKSBhLmJlZ2luKCksYS5lbmQoKQojZGVmaW5lIGZhc3QgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiNkZWZpbmUgREVCVUcoeCkgY291dDw8IiB4IGlzICI8PHg8PCJcbiIKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vaW5saW5lIHNpemVfdCBrZXkoIGludCBhLGludCBiLGludCBjLGludCBkKSB7cmV0dXJuIGEqYipjKmQtNzcwNzt9Ci8vdW5vcmRlcmVkX21hcDxzaXplX3QsYm9vbCA+dmlzbWFwOwoKCi8qaW50IGR4W10gPSB7MSwtMSwwLDB9ICwgZHlbXSA9IHswLDAsMSwtMX07ICovICAvLyAyIERpcmVjdGlvbgovKmludCBkeFtdID0gezEsMH0gLCBkeVtdID0gezAsMX07ICAgICAqLyAgLy8gNCBEaXJlY3Rpb24KLyogaW50IGR4W10gPSB7MSwtMSwwLDAsMSwxLC0xLC0xfSAsIGR5W10gPSB7MCwwLDEsLTEsMSwtMSwxLC0xfTsgKi8gLy8gOCBEaXJlY3Rpb24KLyogaW50IGR4W10gPSB7MSwtMSwxLC0xLDIsMiwtMiwtMn0gLCBkeVtdID0gezIsMiwtMiwtMiwxLC0xLDEsLTF9OyAqLyAvLyBLbmlnaHQgRGlyZWN0aW9uCi8qIGludCBkeFtdID0gezIsLTIsMSwxLC0xLC0xfSAsIGR5W10gPSB7MCwwLDEsLTEsMSwtMX07ICovIC8vIEhleGFnb25hbCBEaXJlY3Rpb24KCmxsIFMsTSxTTSxNUyxTTVM7CnN0cmluZyBzOwoKY29uc3QgbGwgTj0xMDAwMDAwKzEwOwpjb25zdCBsbCAgc3FOPTMyMDsKaW50IGFuc1tOXTsKCgoKc3RydWN0IHF1ZXJ5ewogICAgbGwgbDsKICAgIGxsIHI7CiAgICBsbCBpZHg7Cn0gUVtOXTsKCgpib29sIGNvbXAocXVlcnkgJnExLHF1ZXJ5ICZxMil7CiAgICBpZihxMS5sL3NxTiAhPXEyLmwvc3FOKXsKICAgICAgICByZXR1cm4gcTEubCA8cTIubDsKICAgIH0KICAgIGlmKChxMS5sL3NxTikgJjEpewogICAgICAgIHJldHVybiBxMS5yIDxxMi5yOwogICAgfQogICAgcmV0dXJuIHExLnI+cTIucjsKfQoKCmlubGluZSB2b2lkIGFkZEZyb250KGxsIGkpewogICAgaWYoc1tpXT09J3MnKXsKICAgICAgICBTTVMrPU1TOwogICAgICAgIFNNKz1NOwogICAgICAgIFMrKzsKICAgIH0KICAgIGVsc2UgaWYoc1tpXT09J20nKXsKICAgICAgICBNUys9UzsKICAgICAgICBNKys7CiAgICB9Cn0KCmlubGluZSB2b2lkIGFkZFJlYXIobGwgaSl7CiAgICBpZihzW2ldPT0ncycpewogICAgICAgIFNNUys9U007CiAgICAgICAgUysrOwogICAgICAgIE1TKz1NOwogICAgfQoKICAgIGVsc2UgaWYoc1tpXT09J20nKXsKICAgICAgICBTTSs9UzsKICAgICAgICBNKys7CiAgICB9Cn0KCmlubGluZSB2b2lkIHJlbUZyb250KGxsIGkpewogICAgICBpZihzW2ldPT0ncycpewogICAgICAgIFNNUy09TVM7CiAgICAgICAgU00tPU07CiAgICAgICAgUy0tOwogICAgfQogICAgZWxzZSBpZihzW2ldPT0nbScpewogICAgICAgIE1TLT1TOwogICAgICAgIE0tLTsKICAgIH0KfQoKaW5saW5lIHZvaWQgcmVtUmVhcihsbCBpKXsKICAgIGlmKHNbaV09PSdzJyl7CiAgICAgICAgU01TLT1TTTsKICAgICAgICBTLS07CiAgICAgICAgTVMtPU07CiAgICB9CiAgICBlbHNlIGlmKHNbaV09PSdtJyl7CiAgICAgICAgU00tPVM7CiAgICAgICAgTS0tOwogICAgfQp9CgoKCmludCBtYWluKCl7CiAgICBmYXN0CgogICBjaW4+PnM7CgogICAgbGwgcSxsLHI7CiAgICBjaW4+PnE7CiAgICBmb3IobGwgaT0wO2k8cTtpKyspewogICAgICAgIGNpbj4+bD4+cjsKCiAgICAgICAgbC0tO3ItLTsKICAgICAgICBRW2ldLmw9bDsKICAgICAgICBRW2ldLnI9cjsKICAgICAgICBRW2ldLmlkeD1pOwogICAgfQoKICAgIHNvcnQoUSxRK3EsY29tcCk7CgogICAgbD0wLHI9LTE7CgogICAgZm9yKGxsIGk9MDtpPHE7aSsrKXsKCiAgICAgICAgbGwgTD1RW2ldLmw7CiAgICAgICAgbGwgUj1RW2ldLnI7CiAgICAgICAgaWYoTD5SKXsKICAgICAgICAgICAgYW5zW1FbaV0uaWR4XT0wOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CiAgICAgICAgd2hpbGUobDxMKXsKICAgICAgICAgICAgcmVtRnJvbnQobCk7CiAgICAgICAgICAgIGwrKzsKICAgICAgICB9CgogICAgICAgIHdoaWxlKGw+TCl7CiAgICAgICAgICAgIGwtLTsKICAgICAgICAgICAgYWRkRnJvbnQobCk7CiAgICAgICAgfQoKICAgICAgICB3aGlsZShyPFIpewogICAgICAgICAgICByKys7CiAgICAgICAgICAgIGFkZFJlYXIocik7CiAgICAgICAgfQoKICAgICAgICB3aGlsZShyPlIpewogICAgICAgICAgICByZW1SZWFyKHIpOwogICAgICAgICAgICByLS07CiAgICAgICAgfQoKICAgICAgICBhbnNbUVtpXS5pZHhdPVNNUzsKCiAgICB9CgogICAgZm9yKGxsIGk9MDtpPHE7aSsrKXsKICAgICAgICBjb3V0PDxhbnNbaV08PCJcbiI7CiAgICB9CgoKCiAgICByZXR1cm4gMDsKfQo=