#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	map<string, bool> memo;
	string s;
	int n, m;
	cin >> n;
	rep(i, n){
		cin >> s;
		memo[s] = 1;
	}
	cin >> m;
	int cnt = 0;
	rep(i, m){
		cin >> s;
		if(memo[s])cnt++;
	}
	FIN(cnt);

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,Q;string S;cin>>N>>Q>>S;
  vector<int>V(N);
  int U=0;
  for(int X=0;X<N;X++){
    if(X!=0&&S[X]=='C'&&S[X-1]=='A'){
      U++;
    }
    V[X]=U;
  }
  for(int X=0;X<Q;X++){
    int A,B;cin>>A>>B;
    if(A!=1&&V[A-2]!=V[A-1]){
      cout<<V[B-1]-V[A-1]<<endl;
    }
    else{
      cout<<V[B-1]-V[A-1]<<endl;
    }
  }
}
