#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define i_am_speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define se second
#define fi first
#define INF 1e9+7
#define pb push_back
#define pf push_front
typedef pair <long long, long long> pll;

const long long DIM=2e5+7;

long long n, a[DIM], p, v, s, vn, l, r;
string s1;

int main()
{
    i_am_speed
    cin >> s1;
    s++;
    a[0]=1;
    for(int i=0; i<s1.size(); i++){
        if(s1[i-1]>s1[i]) s++;
        a[i]=s;
    }
    cin >> n;
    for(int i=1; i<=n; i++){
        cin >> l >> r;
        cout << a[r-1]-a[l-1]+1 << endl;
    }


    return 0;
}
