#格式化输入
s = input()
q = int(input())
#字符数值字典,前缀和数组初始化
dx = dict(zip(['P','p','G','g','m'],[3,2,1,0,0]))
ans, dp = 0, []
for c in s:
    ans += dx[c]
    dp.append(ans)
#输出结果
for _ in range(q):
    l, r = map(int,input().split())
    if l==1:
        print(dp[r-1])
    else:
        print(dp[r-1]-dp[l-2])