#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
LL arr[100000];
LL sum(LL l, LL r) {
    if(l > r) return 0;
    if(l == 0) return arr[r];
    return arr[r] - arr[l - 1];
}
int main() {
    string s;
    cin >> s;
    LL n = s.length();
    LL mas[n];
    for(int i = 0; i < n; ++i) mas[i] = 0;
    for(int i = 1; i < n; ++i) if(s[i] < s[i - 1]) mas[i - 1] = 1;
    if(n == 1) mas[0] = 1;
    arr[0] = mas[0];
    for(int i = 1; i < n; ++i) arr[i] = arr[i - 1] + mas[i];
    LL q;
    cin >> q;
    while(q--) {
        LL l, r;
        cin >> l >> r;
        l--; r--;
        LL s = sum(l, r - 1);
        cout << s + 1 << "\n"; 
    }
    return 0;;
}