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

int main() {
    long long n, h = 0, a, b;
    string S;
	cin >> S;
	long long A[S.length() + 1], B[S.length() + 1];
	A[0] = 0;
	B[0] = 0;
	for(long long i = 0; i < S.length(); ++i) {
	    if(S[i] <= S[i + 1]) {
	        B[h] = i + 1;
	    }
	    else {
	        ++h;
	        A[h] = i + 1;
            B[h] = i + 1;
	    }
	}/*
	for(long long i = 0; i < h; ++i) {
	    cout << " " << A[i] << " " << B[i] << "\n";
	}*/
	cin >> n;
	for(long long i = 0; i < n; ++i) {
	    cin >> a >> b;
	    long long c = 0;
	    while(A[c + 1] <= a - 1) {
	        ++c;
	    }
	    long long d = h;
	    while(B[d - 1] >= b - 1) {
	        --d;
	    }
	    cout << d - c + 1 << "\n";
	}
}
