Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    start_a = s[l-1:r].find('A')
    end_a = s[l-1:r].rfind('A')
    start_c = s[l-1:r].find("C")
    end_c = s[l-1:r].rfind("C")
    if start_a == end_a or start_a +1 == start_c:
        start = start_a
    elif start_a +1 < start_c:
        start = start_c +1
       
    if start_c == end_c or end_a +1 == end_c:
        end = end_c
    elif end_a +1 < end_c:
        end = end_a
        
    if start == -1 or end== -1 or end <= start:
        print(0)
        continue
    cand = s[l-1:r][start:end+1]
    print(cand.count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    start_a = s[l-1:r].find('A')
    end_a = s[l-1:r].rfind('A')
    start_c = s[l-1:r].find("C")
    end_c = s[l-1:r].rfind("C")
    if start_a == end_a or start_a +1 == start_c:
        start = start_a
    elif start_a +1 < start_c
        start = start_c +1
       
    if start_c == end_c or end_a +1 == end_c:
        end = end_c
    elif end_a +1 < end_c
        end = end_a
        
    if start == -1 or end== -1 or end <= start:
        print(0)
        continue
    cand = s[l-1:r][start:end+1]
    print(cand.count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    start = s[l-1:r].find('A')
    end = s[l-1:r].rfind("C")
    if start == -1 or end== -1 or end <= start:
        print(0)
        continue
    cand = s[l-1:r][start:end+1]
    print(cand.count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    start = s[l-1:r].find('A')
    end = s[l-1:r].rfind("C")
     if start == -1 or end== -1:
        print(0)
        pass
    cand = s[l-1:r][start:end+1]
    print(cand.count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    count = 0
    flg = 0
    for j in range(l-1, r, 2):
        if s[j] =="A":
            if s[j+1] == "C":
                count +=1
            elif s[j+1] == "A": 
                flg = 1
        elif s[j] =="T" or s[j] =="G":
            if s[j+1] == "A": 
                flg = 1
        elif s[j] == "C" and flg ==1:
                count +=1
                flg = 0
        else:
            pass
    print(count)

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    s1 = s[l-1:r].replace("AT", " ")
    s2 = s1.replace("AG", " ")
    s3 = s2.replace("TC", " ")
    s4 = s3.replace("TG", " ")
    print(s4.count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    print(s[l-1:r].count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()

t = [0] * (N)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

for j in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])