#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#define ll       long long
#define endl     "\n"
using namespace std;
ll z[100003];
string s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll i, n, m, r, l;
    cin >> s >> n;
    m = s.size();
    for (i = 1; i < m; i++)
    {
        if (s[i - 1] > s[i])
        {
            z[i]++;
        }
        z[i] += z[i - 1];
    }
    for (i = 0; i < n; i++)
    {
        cin >> l >> r;
        cout << z[r - 1] - z[l - 1] + 1 << endl;
    }
    for (i = n; i >= 0; i--)
    {
        
    }
    return 0;
}

