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

int main() {
    string s;
    vector <long long> v;
    long long q, l, r, c;
    cin >> s;
    cin >> q;
    v.resize(s.length());
    c = 1;
    v[0] = 1;
    for (int i = 1; i < s.length(); i++) {
        if (s[i-1] > s[i]) c++;
        v[i] = c;
    }
    for (int i = 0; i < q; i++) {
        cin >> l >> r;
        cout << v[r-1] - v[l-1] + 1 << endl;
    }
}
