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

typedef int ll;
const ll INF = 20005;

ll num[INF], a[INF], t[4*INF];
ll n, m, ans, k1, k2;
string S;

ll Build(ll we, ll l, ll r){
    if(l > r) return 0;
    if(l == r) { t[we] = a[l]; return a[l]; }

    ll ans=0;
    ll tm = (l+r)/2;
    ans+=Build(2*we+1, l, tm);
    ans+=Build(2*we+2, tm+1, r);
    t[we] = ans;
    return ans;
}

ll Ask(ll we, ll l, ll r, ll sl, ll sr){
    if(l > sr || r < sl || r < l) return 0;
    if(sl <= l && r <= sr) return t[we];

    ll ans=0;
    ll tm = (l+r)/2;
    ans+=Ask(2*we+1, l, tm, sl, sr);
    ans+=Ask(2*we+2, tm+1, r, sl, sr);
    return ans;
}

int main()
{
    cin >> S;
    n = S.length();
    for(int i=0; i < n; i++){
        if(S[i] == 'a') num[i+1] = 1;
        else if(S[i] == 'b') num[i+1] = 2;
        else if(S[i] == 'c') num[i+1] = 3;
    }

    a[1] = 1;
    for(int i=1; i <= n; i++){
        if(num[i] < num[i-1]) a[i] = 1;
    }
    /*for(int i=1; i <= n; i++){
        cout << num[i];
    }
    cout << endl;
    cout << endl;
    for(int i=1; i <= n; i++){
        cout << a[i];
    }
    cout << endl;
    cout << endl;*/

    Build(0, 1, n);

    cin >> m;
    for(int i=1; i <= m; i++){
        cin >> k1 >> k2;
        ans = Ask(0, 1, n, k1, k2);
        if(a[k1] == 0) ans++;
        cout << ans << endl;
    }

    return 0;
}
