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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	string s;
    cin >> s;
    long long q;
    cin >> q;
    vector<pair<long long, long long>>qandr;
    qandr.resize(q);
    vector <long long > a;
    long long l;
    long long r;
    for (long long i = 0; i < q; i++)
    {
        cin >> l;
        cin >> r;
        qandr.push_back(pair<long long,long long>(l, r));
    }
    
    for (int i = 0; i < s.size(); i++)
    {
        a.push_back((int)s[i]);
    }
    
    for (int i = 0; i < q; i++)
    {
        long long count = 0;
        for (long long p = qandr[i].first-1; p <= qandr[i].second-1; i++)
        {
            if (!(p+1==a.size()&&p==a.size())&&p+1< qandr[i].second - 1)
            {
                if (!(a[p] == a[p + 1] || a[p] + 1 == a[p + 1] || a[p] + 2 == a[p + 1]))
                    count++;
            }
            
        }
        cout << count;
    }
}
