#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

using ll = long long;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    string s;
    int q;
    cin >> s >> q;
    int n = s.size();
    vector<int> next(n);
    for (int i = 0; i + 1 < n; ++i) next[i + 1] = s[i + 1] < s[i];
    partial_sum(next.begin(), next.end(), next.begin());
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        cout << next[r - 1] - next[l - 1] + 1 << '\n';
    }
    return 0;
}
