#include <bits/stdc++.h>

using namespace std;

int main()
{
    string data;
    int q;
    cin >> data >> q;
    
    int n = data.size();
    vector <int> result(n, 0);
    for (int i = 1; i < n; ++i){
        if (data[i] < data[i - 1]) result[i] = result[i - 1] + 1;
        else result[i] = result[i - 1];
    }
    
    int l, r;
    while (q--){
        cin >> l >> r;
        
        --l, --r;
        cout << result[r] - result[l] + 1 << endl;
    }
}