
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    string s;
    cin >> s;
    vector <ll> start = {};
    vector <ll> end = {};
    ll len = s.length();
    ll s1 = 100;
    ll k = 0;
    for (ll i = 0; i < len; i++)
    {
        ll n = 0;
        if (s[i] == 'a') {n = 1;}
        if (s[i] == 'b') {n = 2;}
        if (s[i] == 'c') {n = 3;}
        if (s1 > n) {k ++;}
        start.push_back(k);
        s1 = n;
    }
    k = 0;
    for (ll i = 0; i < len - 1; i++)
    {
        if (start[i] != start[i + 1]) {k++;}
        end.push_back(k);
    }
    end.push_back(k + 1);
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++)
    {
        ll l, r;
        cin >> l >> r;
        l-=2;
        if (l < 0) {l = 0;}
        r--;
        cout << (start[r] -  end[l]) << "\n";
    }
}
