#include <bits/stdc++.h>
#define ll long long 
#define ull unsigned ll
#define dbl double

using namespace std;

const int N = 2 * 1e4 + 10;
int ind[N];
char pr[N];
int ans[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s;
    cin >> s;
    int cnt = 0;
    ind[0] = 1;
    for (int i = 1; i < s.length(); ++i)
    {
        if (s[i] != s[i - 1])
        {
            ++cnt;
            pr[cnt] = s[i - 1];
        }
        ind[i] = cnt + 1;
    }
    ++cnt;
    pr[cnt] = s[s.length()  - 1];
    ind[s.length() - 1] = cnt;

    /*for (int i = 0; i < s.length(); ++i)
        cout << ind[i] << " ";
    cout << endl;*/

    //cout << pr[1];
    for (int i = 2; i <= cnt; ++i)
    {
        //cout << pr[i];
        ans[i] = ans[i - 1];
        if (pr[i - 1] > pr[i])
            ++ans[i];
    }
    /*cout << endl;
    for (int i = 1; i <= cnt; ++i)
        cout << ans[i] << " ";
    cout << endl;*/

    int q;
    cin >> q;
    for (int qq = 0; qq < q; ++qq)
    {
        int l, r;
        cin >> l >> r;
        int start = ind[l- 1];
        int finish = ind[r - 1];
        //cout << start << " " << finish << "|";
        cout << ans[finish] - ans[start] + 1 << endl;
    }
}
