#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
void work() 
{
    string s ; cin >> s ;
    int n = s.size() ; 
    s = " " + s ; //构建前缀和数组用于查询,无需多言
    vector<int>pro(n , 0);
    for(int i = 1 ; i <= n ; i++)
    {
        pro[i] = pro[i - 1];
        if(s[i] == 'P')
        {
            pro[i] += 3 ; 
        }
        else if(s[i] == 'p')
        {
            pro[i] += 2 ; 
        }
        else if(s[i] == 'G')
        {
            pro[i] += 1 ; 
        }
        else
        {
            pro[i] += 0 ; 
        }
    }
    int q ; cin >> q ; 
    while(q--)
    {
        int l , r ; cin >> l >> r ; 
        cout << pro[r] - pro[l - 1] << endl ; 
    }
}
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--) 
    {
        work();
    }
    return 0;
}