// Ignut

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    string s;
    cin >> s;

    vector <int> changes;
    for (int i = 1; i < s.size(); i ++)
        if (s[i] < s[i - 1])
            changes.push_back(i);
    /*
    for (int i = 0; i < changes.size(); i ++)
        cout << changes[i] << ' ';
    cout << "\n";
*/
    //changes = {0, 1, 1, 2, 2, 2, 3, 3};
    vector <int> rchanges = changes;
    reverse(rchanges.begin(), rchanges.end());
    for (int i = 0; i < rchanges.size(); i ++)
        rchanges[i] = -rchanges[i];
    /*
    for (int i = 0; i < rchanges.size(); i ++)
        cout << rchanges[i] << ' ';
    cout << "\n";
*/
    int q;
    cin >> q;
    while (q --) {
        int l, r;
        cin >> l >> r; l --; r --;
        int rb = lower_bound(rchanges.begin(), rchanges.end(), -r) - rchanges.begin();
        rb = rchanges.size() - rb - 1;
        int lb = lower_bound(changes.begin(), changes.end(), l) - changes.begin();
        //cout << rb << ' ' << lb << ' ';

        int ans = rb - lb + 2;
        if (changes[lb] == l)
            ans --;
        cout << ans << "\n";
    }
    return 0;   
}