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

int a[100000];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    a[0] = 1;
    char l = s[0];
    for(int i = 1; i < s.size(); i++) {
        if(l > s[i]) {a[i] = a[i-1] + 1;}
        else {a[i] = a[i-1];}
        l = s[i];
    }
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        cout << a[y] - a[x] + 1 << "\n";
    }
}
