#define _DEBUG 1
#define _GLIBCXX_DEBUG

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

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                     \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z \
       << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define dump3(x, y, z)
#define check(s)
#endif

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

const ll LINF = 2e18;
const int INF = 1e9;

void solve(ll N, ll Q, std::string S, std::vector<ll> l, std::vector<ll> r) {
  vector<ll> a(N, 0);
  FOR(i, 1, N) {
    a.at(i) = a.at(i - 1);
    if (S.at(i) == 'C' && S.at(i - 1) == 'A') a.at(i) += 1;
  }

  rep(i, Q) cout << a.at(r.at(i) - 1) - a.at(l.at(i) - 1) << endl;
}

int main() {
  ll N;
  scanf("%lld", &N);
  ll Q;
  scanf("%lld", &Q);
  std::string S;
  std::cin >> S;
  std::vector<ll> l(Q);
  std::vector<ll> r(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &l[i]);
    scanf("%lld", &r[i]);
  }
  solve(N, Q, S, std::move(l), std::move(r));
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.141592653589793
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rrep(i, n, k) for (int i = (n); i >= (k); i--);
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}
const int MOD = 2;
const int INF = 2e18;

signed main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N + 1);
    vector<int> count(N + 1, 0);
    int start = 0;
    {
        set<int> remain;
        for (int i = 1; i <= N; i++) remain.insert(i);
        for (int i = 0; i < N + M - 1; i++) {
            int A, B;
            cin >> A >> B;
            G[A].push_back(B);
            count[B]++;
            remain.erase(B);
        }
        start = *remain.begin();
    }
    vector<int> ans(N + 1, -1), dis(N + 1, -1);
    ans[start] = 0;
    dis[start] = 0;
    queue<int> explore;
    explore.push(start);
    while (!explore.empty()) {
        int now = explore.front();
        explore.pop();
        for (int next : G[now]) {
            count[next]--;
            if (dis[next] < dis[now] + 1) {
                dis[next] = dis[now] + 1;
                ans[next] = now;
            }
            if (count[next] == 0) explore.push(next);
        }
    }
    for (int i = 1; i <= N; i++) cout << ans[i] << endl;
}
