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

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

	string s;
    cin >> s;
    ll a[20000] = {};
    a[0] = 1;
    for (int i = 1; i < s.size(); i++)
        if (s[i] < s[i - 1])
            a[i] = a[i - 1] + 1;
        else
            a[i] = a[i - 1];
    
    /*for (int i = 0; i < s.size(); i++)
        cout << a[i] << ' ';
    cout << endl;*/

    ll q;
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        ll x, y;
        cin >> x >> y;
        if (x == 1)
            cout << a[y - 1] << "\n";
        else
        {
            /*if (s[x - 1] < s[x - 2])
                cout << a[y - 1] - a[x - 1] << "\n";
            else
                cout << a[y - 1] - a[x - 1] + 1 << "\n";*/
            cout << a[y - 1] - a[x - 1] + 1 << "\n";
        }
    }
}
