// 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);

    int q;
    cin >> q;
    while (q --) {
        int l, r;
        cin >> l >> r;
        auto ub = upper_bound(changes.begin(), changes.end(), l);
        auto lb = lower_bound(changes.begin(), changes.end(), r);

        cout << lb - ub + 1 << "\n";
    }
    return 0;   
}