#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second
#define int long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
using namespace std;
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18;
constexpr ll mod = 998244353;

void solve(){
	string s;
	cin >> s;
	int n = s.size(), q;
	cin >> q;
	ordered_set <int> st;
	
	for (int i = 1; i < n; ++i){
		if (s[i] < s[i-1]) st.insert(i+1);
	}
	
	while (q--){
		int l, r;
		cin >> l >> r;
		if (l == r){
			cout << "1\n";
			continue;
		}
		int a = st.order_of_key(l+1);
		int b = st.order_of_key(r+1);
		cout << b - a + 1 << '\n';
	}
}
 
signed main(){
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    solve();
}