#include <bits/stdc++.h>
#include<iostream>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    string s;
    int q, l, r, an, num = 1;
    cin >> s;
    string s1;
    for (int j = 1; j < s.size()+1; j++) {
        s1 += num;
        if (s[j] < s[j - 1]) {
            num++;
        }
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> l >> r;
        l--; r--;
        an = s1[r] - s1[l] + 1;
        cout << an << '\n';
    }
}