#include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
	int n, q;
	cin >> n >> q;
	string s;
	cin >> s;
	vector<int>ac(n, 0), acsum(n + 1,0);
	for (int i = 0; i < n-1; i++) {
		if (s.substr(i, 2) == "AC") {
			ac[i]++;
			i++;
		}
	}
	rep(i, n)acsum[i + 1] = acsum[i] + ac[i];
	int l, r;
	rep(i, q) {
		cin >> l >> r;
		cout << acsum[r-1] - acsum[l-1] << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

using graph=vector<vector<int>>;

void add_directed_edge(graph& G,int u,int v){
	G[u].emplace_back(v);
}

vector<int> topological_order(const graph& D){
	int n=D.size();
	vector<int> deg(n);
	rep(u,n) for(int v:D[u]) deg[v]++;

	vector<int> res;
	queue<int> Q;
	rep(u,n) if(deg[u]==0) Q.emplace(u);
	while(!Q.empty()){
		int u=Q.front(); Q.pop();
		res.emplace_back(u);
		for(int v:D[u]) if(--deg[v]==0) Q.emplace(v);
	}
	return res;
}

int main(){
	int n,m; scanf("%d%d",&n,&m);
	m+=n-1;
	graph G(n);
	rep(i,m){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		add_directed_edge(G,u,v);
	}

	vector<int> dp(n);
	for(int u:topological_order(G)){
		for(int v:G[u]) dp[v]=max(dp[v],dp[u]+1);
	}

	vector<int> ans(n,-1);
	rep(u,n) for(int v:G[u]) if(dp[v]==dp[u]+1) ans[v]=u;
	rep(u,n) printf("%d\n",ans[u]+1);

	return 0;
}
