#include <bits/stdc++.h>
using namespace std;

string s;
int mas[21000], n, q, l, r;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    for (int i=1; i< s.size(); i++)
    {
        if (s[i-1] > s[i]) mas[i+1]= mas[i] + 1;
        else mas[i+1] = mas[i];
    }
    cin >> q;
    for (int i=1; i<=q; i++)
    {
        cin >> l >> r;
        cout<< mas[r] - mas[l] + 1 <<"\n";
    }
}
