#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double
#define fi first
#define sc second
#define point pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define show(x) cerr << (#x) << " = " << x << '\n'
#define print(x) if (1) {cerr << (#x) << " = "; \
      for (auto it : x) cerr << it << ' '; cerr << '\n';}

using namespace std;
using namespace __gnu_pbds;

const int MAXN = 200064;
const int MAXT = 1e7 + 64;
const int ROOT = 1024;
const int MOD = 998244353;
const int LOG = 31;
const int INF = INT_MAX;
const ll MAX = LLONG_MAX;

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

      string s;
      cin >> s;

      int n = s.size();

      vector<int> pref(n);
      for (int i = 0; i + 1 < n; ++i)
            pref[i] = (i ? pref[i - 1] : 0) + (s[i] > s[i + 1]);
      if (n > 1) pref[n - 1] = pref[n - 2];

      //print(pref);

      int q;
      cin >> q;
      while (q--) {
            int l, r;
            cin >> l >> r, --l, --r;

            cout << pref[r] - (l ? pref[l - 1] : 0) - (r + 1 < n && s[r] > s[r + 1]) + 1 << '\n';
      }
}
