#include <bits/stdc++.h>

using namespace std;

long long n, a[100008], i, r, m, q = 1, w = 0, p = 0, x, y;

string s;

int main() {
    cin >> s;
    cin >> n;
    a[0] = 1;
    for (i = 1; i <= s.length(); i ++) {
        if (s[i - 1] <= s[i - 2]) a[i] = a[i - 1];
        else a[i] = a[i - 1] + 1;
    }
    for (i = 1; i <= n; i ++) {
        cin >> x >> y;
        cout << a[y] - a[x - 1];
    }

    return 0;
}