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

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

	int n;
    struct lkio {
        int x, y;
    };
    lkio v[100008];
    string s;
    cin >> s;
	cin >> n;
    int m = s.size();
    int a = 0, b = 0;
    int i;
    int u = 0;
    for(i = 1; i < m; i++){
        if(s[i] < s[i - 1]){
            v[u].x = a + 1;
            v[u].y = i;
            a = v[u].y;
            u++;
        }
    }
    b = v[u - 1].y;
    b++;
    v[u].x = b;
    v[u].y = m;
    a = 0;
    b = 0;
    int c = 0;
    int j;
    for(i = 1; i <= n; i++){
        cin >> a >> b;
        c = 0;
        for(j = 0; j <= u; j++){
            if(v[j].y >= a && v[j].x <= b) {
                c++;
            }
        }
        cout << c << "\n";
    }
}
