#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

using ll = long long;
using ld = long double;

ll n, m, k, q, x, y, z, a, b, c, d;
string s;

void solve() {
    cin >> s;
    n = s.size();
    vector<ll> bg, en;
    bg.push_back(0);
    for (int i = 1; i < n; i++) {
        if (s[i] < s[i - 1]) {
            en.push_back(i - 1);
            bg.push_back(i);
        }
    }
    en.push_back(n - 1);
    // for (int i = 0; i < bg.size(); i++) {
    //     cout << bg[i] << ' ' << en[i] << '\n';
    // }
    cin >> q;
    while (q--) {
        cin >> x >> y;
        x--;
        y--;
        ll i1 = upper_bound(bg.begin(), bg.end(), x) - bg.begin() - 1;
        ll i2 = lower_bound(en.begin(), en.end(), y) - en.begin();
        // cout << *lower_bound(bg.begin(), bg.end(), x) << ' ';
        // cout << *lower_bound(en.begin(), en.end(), y) << '\n';
        cout << i2 - i1 + 1 << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout << setprecision(10);
    solve();
}
