#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ft first
#define sd second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define point pair <int, int>

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

