It's a walking tour day in SIS.Winter, so $t$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.

Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P":   "A" corresponds to an angry student  "P" corresponds to a patient student 

Such string describes the row from the last to the first student.

Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $i$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $i+1$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them.

[Image]

Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry.

Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group.


-----Input-----

The first line contains a single integer $t$ — the number of groups of students ($1 \le t \le 100$). The following $2t$ lines contain descriptions of groups of students.

The description of the group starts with an integer $k_i$ ($1 \le k_i \le 100$) — the number of students in the group, followed by a string $s_i$, consisting of $k_i$ letters "A" and "P", which describes the $i$-th group of students.


-----Output-----

For every group output single integer — the last moment a student becomes angry.


-----Examples-----
Input
1
4
PPAP

Output
1

Input
3
12
APPAPPPAPPPP
3
AAP
3
PPA

Output
4
1
0



-----Note-----

In the first test, after $1$ minute the state of students becomes PPAA. After that, no new angry students will appear.

In the second tets, state of students in the first group is:   after $1$ minute — AAPAAPPAAPPP  after $2$ minutes — AAAAAAPAAAPP  after $3$ minutes — AAAAAAAAAAAP  after $4$ minutes all $12$ students are angry 

In the second group after $1$ minute, all students are angry.

Difficulty: EASY
Tags: Implementation

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
for inp in range(int(input())):
	n = int(input())
	s = input() + 'A'
	s = list(s)
	s.reverse()
	m = 0
	for i in range(s.count('A')):
		ind = s.index('A')
		s = s[ind + 1:]
		if m < ind:
			m = ind
	print(m)


def get_new_str(s, n):
	ns = ''
	for i in range(n):
		if i > 0 and s[i] == 'A' and (s[i - 1] == 'A'):
			continue
		else:
			ns = ns + s[i]
	return ns
import sys
t = int(sys.stdin.readline().strip())
for _ in range(t):
	n = int(sys.stdin.readline().strip())
	s = sys.stdin.readline().strip()
	new_str = get_new_str(s, n)
	chunks = new_str.split('A')[1:]
	if chunks:
		len_chunks = [len(i) for i in chunks]
		print(max(len_chunks))
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	(s, g, c, m) = (input(), False, 0, 0)
	for el in s:
		if el == 'A' and (not g):
			g = True
		if el == 'P' and g:
			c += 1
		if el == 'A' and g:
			m = max(c, m)
			c = 0
	print(max(m, c))


t = int(input())
for i in range(t):
	n = int(input())
	students = input()
	first_angry = -1
	for j in range(n):
		if students[j] == 'A':
			first_angry = j
			break
	if first_angry == -1:
		print(0)
	else:
		pointer = 0
		arr_of_lens = [0 for _ in range(n)]
		for j in range(first_angry + 1, n):
			if students[j] == 'P' or students[j] == 'p':
				arr_of_lens[pointer] += 1
			else:
				pointer += 1
		print(max(arr_of_lens))


tc = int(input())
while tc > 0:
	tc -= 1
	n = int(input())
	s = input()
	cnt = 0
	mx = 0
	i = 0
	while i < n:
		if s[i] == 'A':
			cnt = 0
			while i < n and s[i] == 'A':
				i += 1
			while i < n and s[i] == 'P':
				cnt += 1
				i += 1
			if cnt > mx:
				mx = cnt
		else:
			i += 1
	print(mx)


for _ in range(int(input())):
	n = input()
	arr = input()
	if 'A' not in arr:
		print(0)
	else:
		arr = arr[arr.find('A'):]
		new = arr.split('A')
		print(max([len(i) for i in new]))


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	b = []
	k = 0
	t = 0
	for i in range(len(a)):
		if a[i] == 'A':
			t += 1
		if t > 0:
			if a[i] == 'P':
				k += 1
			else:
				b.append(k)
				k = 0
	b.append(k)
	print(max(b))


tests = int(input())
for _ in range(tests):
	cn = int(input())
	s_arr = input()
	prev_anv = -1
	max_dist = 0
	for (i, indiv) in enumerate(s_arr):
		if indiv == 'A':
			if prev_anv != -1:
				max_dist = max(max_dist, i - prev_anv - 1)
			prev_anv = max(prev_anv, i)
	if prev_anv != -1:
		max_dist = max(max_dist, cn - prev_anv - 1)
	print(max_dist)


t = int(input())
while t:
	t = t - 1
	k = int(input())
	s = input()
	a = []
	for i in range(len(s)):
		if s[i] == 'A':
			a.append(i)
	maxi = 0
	a.append(k)
	for i in range(len(a) - 1):
		maxi = max(maxi, a[i + 1] - a[i])
	print(max(0, maxi - 1))


for _ in range(int(input())):
	N = input()
	A = input()
	try:
		curr_angry = A.index('A')
	except:
		print(0)
		continue
	max_time = 0
	last_one = len(A)
	while True:
		try:
			next_angry = A.index('A', curr_angry + 1)
		except Exception as e:
			max_time = max(max_time, last_one - curr_angry - 1)
			break
		else:
			max_time = max(max_time, next_angry - curr_angry - 1)
			curr_angry = next_angry
	print(max_time)


from sys import stdin as si, stdout as so

def ch(s1, r1):
	for k in range(len(s1)):
		if s1[k] != r1[k]:
			return False
	return True

def main():
	for i in range(int(si.readline())):
		n = int(si.readline())
		s = list(si.readline().strip('\n'))
		t = 0
		con = True
		while con:
			r = s[:]
			for j in range(n - 1, 0, -1):
				if s[j - 1] == 'A' and s[j] == 'P':
					s[j] = 'A'
			if not ch(s, r):
				t += 1
			else:
				con = False
		so.write(str(t) + '\n')
	so.flush()
main()


t = int(input())
for g in range(t):
	n = int(input())
	s = input()
	j = c = 0
	while s.count('AP') != 0:
		l = ''
		j = 0
		c += 1
		i = 0
		while i <= n - 1:
			if s[i:i + 2] == 'AP':
				l += 'AA'
				j = i + 2
				i += 2
			else:
				l += s[i]
				j += 1
				i += 1
		s = l + ''
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	maxx = 0
	c = 0
	i = 0
	while i < n:
		if s[i] == 'P':
			i += 1
			continue
		else:
			i += 1
			break
	while i < n:
		if s[i] == 'A':
			c = 0
		else:
			c += 1
			if maxx < c:
				maxx = c
		i += 1
	print(maxx)


t = int(input())
for _ in range(t):
	n = int(input())
	str = input()
	k = 0
	while 'AP' in str:
		str = str.replace('AP', 'AA')
		k += 1
	print(k)


def mi():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	i = 0
	ans = 0
	while i < n:
		if a[i] == 'A':
			t = 0
			i += 1
			cnt = 0
			while i < n and a[i] == 'P':
				i += 1
				cnt += 1
			ans = max(ans, cnt)
		else:
			i += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	z = 0
	m = 0
	l = 0
	for i in s:
		if i == 'A' and z == 0:
			z = 1
		if z == 1:
			if i == 'P':
				l = l + 1
			else:
				m = max(l, m)
				l = 0
	if s[-1] == 'P':
		m = max(l, m)
	print(m)


t = int(input())
while t > 0:
	length = int(input())
	str_input = input()
	max_count = 0
	start = 0
	while start < len(str_input):
		if str_input[start] == 'A':
			start += 1
			count = 0
			while start < len(str_input) and str_input[start] != 'A':
				count += 1
				start += 1
			max_count = max(count, max_count)
		else:
			start += 1
	print(max_count)
	t = t - 1


t = int(input())
for tt in range(t):
	l = int(input())
	s = input()
	x = s.find('A')
	if x == -1:
		print(0)
	else:
		ans = 0
		while x < l:
			z = 0
			while x < l and s[x] == 'P':
				z += 1
				x += 1
			ans = max(ans, z)
			x += 1
		print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	i = 0
	count = 0
	while i < n:
		if s[i] == 'A':
			i += 1
			count = 0
			while i < n and s[i] == 'P':
				i += 1
				count += 1
			ans = max(ans, count)
		else:
			i += 1
	print(ans)


n = int(input())
for j in range(n):
	x = int(input())
	ch = input()
	i = 0
	while i < len(ch) and ch[i] == 'P':
		i += 1
	ch2 = ch[i:len(ch)]
	L = [i for i in ch2.split('A')]
	maxx = -1
	for i in L:
		maxx = max(maxx, len(i))
	print(maxx)


t = int(input())
for i in range(t):
	n = int(input())
	lst = input()
	(k, cnt, mx) = (0, 0, 0)
	if n == 1 and lst[0] == 'A' or (n == 1 and lst[0] == 'P') or lst.count('P') == len(lst):
		print(0)
	else:
		while lst[k] != 'A' and k < len(lst) - 1:
			cnt += 1
			k += 1
		for i in range(cnt, len(lst)):
			if lst[i] == 'A':
				cnt = 0
			else:
				cnt += 1
				mx = max(mx, cnt)
		print(mx)


for i in range(int(input())):
	k = int(input())
	a = input()
	b = []
	c = 0
	for i in range(k):
		if a[i] == 'A':
			b.append(i)
	for i in b:
		d = 0
		for j in range(i + 1, k):
			if a[j] == 'P':
				d += 1
			else:
				break
		c = max(c, d)
	print(c)


import math
T = int(input())
for l in range(T):
	n = int(input())
	s = input()
	j = -math.inf
	k = 0
	d = 0
	for i in range(n):
		if s[i] == 'A':
			j = i
		if s[i] == 'P':
			k = i
		if d < k - j and j >= 0:
			d = k - j
	if d > n:
		d = 0
	print(d)


for _ in range(int(input())):
	n = int(input())
	s = input()
	f = 0
	while s.count('AP') > 0:
		s = s.replace('AP', 'AA')
		f += 1
	print(f)


for _ in range(int(input())):
	n = int(input())
	s = input()
	maxi = 0
	cont = 0
	c = 0
	for i in range(n):
		if s[i] == 'A':
			c = 1
			cont = 0
		else:
			cont += 1
		if c:
			maxi = max(cont, maxi)
	print(maxi)


for _ in range(int(input())):
	n = int(input())
	s = input()
	curr_sum = 0
	prev_A = False
	tot_sum = 0
	for i in range(n):
		if s[i] == 'A':
			prev_A = True
			curr_sum = 0
		elif s[i] == 'P' and prev_A:
			curr_sum += 1
			if curr_sum > tot_sum:
				tot_sum += 1
	print(tot_sum)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	t = [0 for _ in range(k)]
	pos = -1
	for (i, x) in enumerate(s):
		if x == 'A':
			pos = i
		if pos != -1:
			t[i] = i - pos
	print(max(t))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	lo = list(s)
	f = 0
	x = 1
	while x == 1:
		x = 0
		for j in range(k - 1):
			if s[j] == 'A' and s[j + 1] == 'P':
				lo[j + 1] = 'A'
				x = 1
		if x == 1:
			f = f + 1
		s = ''.join(lo)
	print(f)


for u in range(int(input())):
	n = int(input())
	s = input()
	if s.count('A') == 0:
		print(0)
	else:
		c = s.index('A')
		m = 0
		for i in range(n):
			if s[i] == 'A':
				d = i - c
				m = max(d, m)
				c = i
		print(max(m - 1, n - c - 1))


n = int(input())
T = []
for k in range(n):
	nb = int(input())
	ch = list(input())
	for t in range(nb):
		nch = []
		nch.append(ch[0])
		end = 1
		for i in range(nb - 1):
			if ch[i] == 'A' and ch[i + 1] == 'P':
				nch.append('A')
				end = 0
			else:
				nch.append(ch[i + 1])
		if end == 1:
			break
		ch = nch
	T.append(t)
for t in T:
	print(t)


for i in range(int(input())):
	N = int(input())
	X = input()
	if 'A' not in X:
		print(0)
		continue
	X = X[X.index('A'):]
	Sum = 0
	Max = 0
	for j in range(len(X)):
		if X[j] == 'P':
			Sum += 1
		else:
			Max = max(Sum, Max)
			Sum = 0
	Max = max(Max, Sum)
	print(Max)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	raj = []
	minn = -1
	chk = 0
	while True:
		if chk == n:
			break
		if s[chk] == 'P':
			chk += 1
		else:
			break
	i = chk
	while i < n - 1:
		if s[i] == 'A' and s[i + 1] == 'A':
			i += 1
			continue
		i += 1
		j = i
		while True:
			if j == n:
				break
			if s[j] == 'P':
				j += 1
			elif s[j] == 'A':
				break
		if j - i > minn:
			minn = j - i
		i = j
	if minn < 0:
		print(0)
	else:
		print(minn)


T = int(input())
for i in range(T):
	input()
	K = list(input().split('A'))
	M = 0
	for j in range(1, len(K)):
		if len(K[j]) > M:
			M = len(K[j])
	print(M)


test = int(input())
for m in range(test):
	n = int(input())
	string = input()
	count = 0
	maxi = -1
	char = -1
	for i in range(0, len(string)):
		if string[i] == 'P' and char == 1:
			count = count + 1
		elif string[i] == 'A':
			char = 1
			maxi = max(maxi, count)
			count = 0
	print(max(maxi, count))


t = int(input())
for _ in range(t):
	input()
	students = input()
	patients = students.split('A')
	if students[0] != 'A':
		patients = patients[1:]
	res = 0
	for p in patients:
		res = max(res, len(p))
	print(res)


for i in range(int(input())):
	n = int(input())
	string = input()
	Rstring = string[::-1]
	arr = [0]
	count = 0
	for i in range(n):
		if Rstring[i] == 'P':
			count += 1
		if Rstring[i] == 'A':
			arr.append(count)
			count = 0
	print(max(arr))


g = int(input())
while g:
	n = int(input())
	students = str(input())
	m = 0
	while students.count('AP') > 0:
		students = students.replace('AP', 'AA')
		m += 1
	print(m)
	g -= 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	max_ = 0
	current = 0
	a_found = False
	for i in input():
		if i == 'A':
			a_found = True
			if max_ < current:
				max_ = current
			current = 0
		elif a_found:
			current += 1
	print(max(max_, current))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	count = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		count += 1
	print(count)


for i in range(int(input())):
	k = int(input())
	stud = input()
	s = 0
	if stud[0] == 'P':
		s = 1
	stud = stud.split('A')
	if s:
		del stud[0]
	ans = 0
	for elem in stud:
		ans = max(ans, len(elem))
	print(ans)


import sys
input = sys.stdin.readline
t = int(input().strip())
while t > 0:
	k = int(input().strip())
	s = input().strip()
	tmp = 0
	ans = 0
	for i in range(0, k):
		if s[i] is 'A':
			j = i + 1
			while j < k and s[j] is 'P':
				tmp += 1
				j += 1
			ans = max(tmp, ans)
			tmp = 0
	print(ans)
	t -= 1


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	n = sInt()
	s = input()
	cnt = [0]
	i = 0
	while i < n:
		c = 0
		if i != n - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				i += 1
				while i < n and s[i] == 'P':
					c += 1
					i += 1
				cnt.append(c)
			else:
				i += 1
		else:
			i += 1
	print(max(cnt))


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = 0
	id = 0
	for i in range(len(s)):
		if s[i] == 'A':
			a = 1
			id = i
			break
	if a == 1:
		p = 0
		ans = 0
		s = s[id + 1:]
		for i in range(len(s)):
			if s[i] == 'P':
				p += 1
			else:
				p = 0
			ans = max(ans, p)
		print(ans)
	else:
		print(0)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	s = input().strip()
	ans = 0
	a = []
	for i in range(n):
		if s[i] == 'A':
			a.append(i)
	a.append(n)
	for i in range(len(a) - 1):
		ans = max(ans, a[i + 1] - a[i] - 1)
	print(ans)
for _ in range(I()):
	solve()


import re

def change_group(group, total_minutes):
	new_group = re.sub('AP', 'AA', group)
	if new_group != group:
		total_minutes += 1
		return change_group(new_group, total_minutes)
	return [new_group, total_minutes]
number_of_groups = int(input())
groups = []
for i in range(number_of_groups * 2):
	input_string = input()
	if i % 2 != 0:
		groups.append(input_string)
for group in groups:
	print(change_group(group, 0)[1])


def test(n):
	a = list(input())
	i = 0
	flag = True
	while flag:
		flag = False
		j = 0
		while j < n - 1:
			if a[j] == 'A' and a[j + 1] == 'P':
				a[j + 1] = 'A'
				j += 1
				flag = True
			j += 1
		if flag:
			i += 1
	print(i)
for i in range(int(input())):
	test(int(input()))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	count = 0
	ans = []
	while True:
		flag = 1
		ans = []
		for i in range(len(s) - 1):
			if s[i] == 'A':
				if s[i + 1] == 'P':
					flag = 0
					ans.append(i + 1)
		for i in ans:
			s[i] = 'A'
		if not flag:
			count += 1
		else:
			break
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input().split('A')
	if len(s) == 1:
		s = 0
	else:
		s = max([len(i) for i in s[1:]])
	print(s)


no_of_tests = int(input())
for i in range(no_of_tests):
	no_of_students = int(input())
	students = input().lstrip('P')
	last_moment = 0
	moment = 0
	for student_state in students:
		if student_state == 'P':
			moment += 1
		else:
			last_moment = moment if moment > last_moment else last_moment
			moment = 0
	last_moment = moment if moment > last_moment else last_moment
	print(last_moment)


for _ in range(int(input())):
	l = int(input())
	s = input()
	ans = 0
	i = 0
	pa = []
	for i in range(l):
		if s[i] == 'A':
			pa.append(i)
	pa.append(l)
	for i in range(len(pa) - 1):
		j = i + 1
		ans = max(ans, pa[j] - pa[i] - 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lst = list(input())
	i = 0
	high = 0
	count = 0
	while i < n and lst[i] != 'A':
		i += 1
	while i < n:
		if lst[i] == 'P':
			count += 1
		else:
			if count > high:
				high = count
			count = 0
		i += 1
	if count > high:
		high = count
	print(high)


def inp(dtype=str, strip=True):
	s = input()
	res = [dtype(p) for p in s.split()]
	res = res[0] if len(res) == 1 and strip else res
	return res

def problem1():
	t = inp(int)
	for _ in range(t):
		n = inp(int)
		s = inp(str)
		i = 0
		while i < n and s[i] == 'P':
			i += 1
		res = 0
		while i < n:
			while i < n and s[i] == 'A':
				i += 1
			res1 = 0
			while i < n and s[i] == 'P':
				res1 += 1
				i += 1
			res = max(res, res1)
		print(res)

def problem2():
	pass

def problem3():
	pass

def problem4():
	pass

def problem5():
	pass
problem1()


t = int(input())
for cas in range(t):
	n = int(input())
	s = input()
	ans = 0
	flag = 0
	tmp = 0
	for i in range(n):
		if s[i] == 'A':
			if flag == 1:
				ans = max(ans, tmp)
			flag = 1
			tmp = 0
		else:
			tmp = tmp + 1
	if flag == 1:
		ans = max(ans, tmp)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	idx = []
	for i in range(n):
		if s[i] == 'A':
			idx.append(i)
	if not idx:
		print(0)
	elif len(idx) == 1:
		print(n - idx[0] - 1)
	else:
		mxl = 0
		for i in range(1, len(idx)):
			mxl = max(mxl, idx[i] - idx[i - 1] - 1)
		mxl = max(mxl, n - idx[-1] - 1)
		print(mxl)


def result(a):
	angry_idx = -1
	post_angry_patient_count = 0
	max_post_angry_patient_count = 0
	for i in range(len(a)):
		if a[i] == 'A':
			angry_idx = i
			post_angry_patient_count = 0
		elif angry_idx != -1:
			post_angry_patient_count += 1
		max_post_angry_patient_count = max(post_angry_patient_count, max_post_angry_patient_count)
	return max_post_angry_patient_count
t = int(input())
for _ in range(t):
	n = int(input())
	a = str(input())
	print(result(a))


t = int(input())
for x in range(t):
	n = int(input())
	s = list(input())
	f = 0
	c = 0
	for i in range(n):
		f = 0
		for j in range(n - 1, 0, -1):
			if s[j] == 'P' and s[j - 1] == 'A':
				s[j] = 'A'
				f = 1
		if f == 1:
			c = c + 1
	print(c)


groups = int(input())
lista = []
result = []
for x in range(groups):
	n = int(input())
	lista.append(input())
	count = 0
	countList = []
	isABefore = False
	for y in range(n):
		if lista[x][y] == 'A':
			isABefore = True
		if lista[x][y] != 'A' and isABefore:
			count += 1
		else:
			countList.append(count)
			count = 0
	countList.append(count)
	result.append(max(countList))
for x in result:
	print(x)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	k = inputnum()
	s = inputstring()
	start = False
	cur = 0
	mx = 0
	for i in range(k):
		if s[i] == 'A':
			start = True
			mx = max(cur, mx)
			cur = 0
		if s[i] == 'P' and start:
			cur += 1
	mx = max(cur, mx)
	print(mx)


for i in range(int(input())):
	n = int(input())
	l = list(input().split('A'))
	k = []
	for j in l:
		k.append(len(j))
	if len(k) > 1:
		print(max(k[1:]))
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(input())
	mx = 0
	for i in range(n):
		if l[i] == 'A':
			co = 0
			for ii in range(i + 1, n):
				if l[ii] == 'P':
					co += 1
				else:
					break
			if co > mx:
				mx = co
	print(mx)


def countMin(a):
	lst = []
	for i in range(len(a)):
		if a[i] == 'A':
			lst.append(i)
	if len(lst) == 0:
		return 0
	else:
		lst.append(len(a))
		res = 0
		for i in range(1, len(lst)):
			if lst[i] - lst[i - 1] - 1 > res:
				res = lst[i] - lst[i - 1] - 1
		return res
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	print(countMin(a))


t = int(input())
for i in range(t):
	n = int(input())
	l = list(input())
	count = 0
	z = 0
	if 'A' in l:
		ind = l.index('A')
		for i in l[ind:]:
			if i == 'P':
				count += 1
			else:
				count = 0
			z = max(z, count)
	print(z)


t = int(input())
for x in range(t):
	abc = int(input())
	str = input()
	r = str.find('A')
	num = 0
	sum = 0
	if r == -1:
		print(0)
	else:
		n = len(str)
		for i in range(r + 1, n):
			if str[i] == 'A':
				num = 0
			elif str[i] == 'P':
				num += 1
				if num > sum:
					sum = num
		print(sum)


import copy
n = int(input())
glst = []
for i in range(n):
	m = int(input())
	glst.append(list(input()))
res = []
for p in glst:
	l = copy.deepcopy(p)
	c = 0
	k = 0
	while True:
		k = k + 1
		x = [i for i in range(len(l)) if l[i] == 'A']
		if 'A' not in l:
			break
		if 'P' not in l:
			break
		q = ''.join(l).rindex('P')
		if q < l.index('A'):
			break
		if l[-1] == 'P':
			for i in x:
				if l[i + 1] == 'P':
					l[i + 1] = 'A'
					c = k
		if l[-1] == 'A':
			for i in x[0:len(x) - 1]:
				if l[i + 1] == 'P':
					l[i + 1] = 'A'
					c = k
	res.append(c)
for i in res:
	print(i)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	l = []
	l.append(0)
	for i in range(n):
		if s[i] == 'A':
			c = 0
			for j in range(i + 1, n):
				if s[j] == 'P':
					c += 1
				else:
					l.append(c)
					c = 0
			l.append(c)
	print(max(l))


for _ in range(int(input())):
	number = int(input())
	s = input()
	counter = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		counter += 1
	print(counter)


t = int(input())
for case in range(t):
	n = int(input())
	s = input()
	if 'A' not in s:
		answer = 0
	else:
		ind = s.index('A')
		lst = s[ind:].split('A')
		answer = len(max(lst, key=len))
	print(answer)


t = int(input())
groups = []
for z in range(t):
	groups.append((input(), input()))
answers = []
for group in groups:
	if 'A' not in group[1]:
		answers.append(0)
		continue
	a = 0
	n = 0
	for student in reversed(group[1]):
		if student == 'P':
			n += 1
		else:
			if n > a:
				a = n
			n = 0
	answers.append(a)
for answer in answers:
	print(answer)


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def getStr():
	return input().strip()
t = getInt()
for _ in range(t):
	n = getInt()
	s = getStr()
	index = s.find('A')
	if index == -1:
		print(0)
		continue
	m = 0
	k = 0
	for i in range(index + 1, n):
		if s[i] == 'A':
			m = max(m, k)
			k = 0
		else:
			k += 1
	m = max(m, k)
	print(m)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' not in s:
		print(0)
	else:
		ind = s.index('A')
		temp = 0
		con = 0
		for j in range(ind + 1, n):
			if s[j] == 'A':
				con = 0
			else:
				con += 1
			temp = max(temp, con)
		print(temp)


import sys
import math

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def minput():
	return map(int, sys.stdin.readline().strip().split())

def listinput():
	return list(map(int, sys.stdin.readline().strip().split()))
for _ in range(iinput()):
	n = iinput()
	s = list(map(str, input().lstrip('P')))
	s = ''.join(s)
	s = list(map(str, s.split('A')))
	maxx = 0
	for i in range(len(s)):
		if len(s[i]) > maxx:
			maxx = len(s[i])
	print(maxx)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	a += 'A'
	i = 0
	k = 0
	mx = 0
	while a[i] == 'P':
		i += 1
	for j in range(i, n + 1):
		if a[j] == 'P':
			k += 1
		else:
			if k > mx:
				mx = k
			k = 0
	print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	s = 'A' + input() + 'A'
	a = [i for (i, x) in enumerate(s) if x == 'A']
	try:
		print(max([a[i] - a[i - 1] for i in range(2, len(a))]) - 1)
	except ValueError:
		print(0)


def solve(arr, k):
	time = 0
	while True:
		if arr.count('A') == k or arr.count('P') == k:
			break
		time += 1
		indices = []
		for i in range(k - 1):
			if arr[i] == 'A' and arr[i + 1] == 'P':
				indices.append(i)
		for i in indices:
			arr[i + 1] = 'A'
		if not indices:
			time -= 1
			break
	print(time)

def main():
	t = int(input())
	for i in range(t):
		k = int(input())
		arr = list(input())
		solve(arr, k)
main()


t = int(input(''))
for i in range(t):
	n = int(input(''))
	d = input('')
	dem = 0
	a = []
	i = 0
	while d[i] == 'P':
		i += 1
		if i == n:
			break
	for j in range(i, len(d)):
		if d[j] == 'A':
			if dem > 0:
				a.append(dem)
			dem = 0
		else:
			dem += 1
	if dem > 0:
		a.append(dem)
	if len(a) == 0:
		print(0)
	else:
		print(max(a))


t = int(input())
for x in range(t):
	k = int(input())
	a = input()
	b = a.count('A')
	m = 0
	if b != 0:
		i = a.index('A')
		c = 0
		m = 0
		if i == k - 1:
			m = 0
		else:
			while i < k:
				if a[i] == 'P':
					c += 1
				else:
					m = max(m, c)
					c = 0
				i += 1
			m = max(m, c)
	print(m)


n = int(input())
for i in range(n):
	l = int(input())
	w = list(input())
	ma = []
	co = 0
	for i in range(l - 1):
		if w[i] == 'A' and w[i + 1] == 'P':
			co += 1
			w[i + 1] = 'A'
		else:
			ma.append(co)
			co = 0
	ma.append(co)
	print(max(ma))


def get_first(count, queue):
	for i in range(count):
		if queue[i] == 'A':
			return i
	return False

def get_time(count, queue):
	max_time = 0
	first_occured = get_first(count, queue)
	last_angry = first_occured
	if first_occured is not False:
		for i in range(first_occured + 1, count):
			if queue[i] is 'A':
				if i - last_angry > 1:
					max_time = max(max_time, i - last_angry - 1)
				last_angry = i
		max_time = max(max_time, count - last_angry - 1)
	return max_time
groups = int(input())
queues = []
for group in range(groups):
	input()
	queues.append(input())
for group in queues:
	print(get_time(len(group), group))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	if 'A' in s:
		s = s[s.find('A'):]
		n = max(s.split('A'))
		print(len(n))
	else:
		print(0)


for t in range(int(input())):
	n = int(input())
	s = input()
	c = 0
	ans = 0
	m = False
	for i in range(n):
		if s[i] == 'P' and m:
			c += 1
		elif s[i] == 'A' and (not m):
			m = True
		else:
			c = 0
		ans = max(c, ans)
	print(ans)


for i in range(int(input())):
	q = int(input())
	w = list(input())
	k = 0
	l = 0
	j = 0
	for i in w:
		if i == 'A':
			j = 1
		if j == 1 and i == 'P':
			k += 1
			l = max(k, l)
		elif j == 1 and i == 'A':
			l = max(k, l)
			k = 0
	print(l)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	j = 0
	while 1:
		q = [s[i] for i in range(len(s))]
		y = []
		for k in range(len(s)):
			if s[k] == 'A':
				y.append(k)
		for k in range(len(y)):
			if y[k] == len(s) - 1:
				pass
			else:
				s[y[k] + 1] = 'A'
		if q == s:
			break
		else:
			j = j + 1
	print(j)


for t in range(int(input())):
	n = int(input())
	s = list(input())
	indexP = -1
	indexA = -1
	ln = []
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 'P':
			if indexP == -1:
				indexP = i
				if indexA > 0 and indexP > 0:
					ln.append(indexP - indexA)
				indexA = -1
		if s[i] == 'A':
			if indexA == -1 and indexP > 0:
				indexA = i
				if indexA >= 0 and indexP >= 0:
					ln.append(indexP - indexA)
				indexP = -1
	if len(ln) != 0:
		print(max(ln))
	else:
		print('0')


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	count = []
	for j in range(len(s)):
		if s[j] == 'A':
			c = 0
			for k in range(j + 1, n):
				if s[k] == 'P':
					c += 1
				else:
					break
			count.append(c)
	if len(count) > 0:
		print(max(count))
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	ans = 0
	check = False
	cnt = 0
	for i in range(len(s)):
		if s[i] == 'A':
			check = True
			cnt = 0
		elif s[i] == 'P' and check:
			cnt += 1
			ans = max(ans, cnt)
	print(ans)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		s = input()
		if s == 'A' * n or s == 'P' * n:
			print(0)
		else:
			arr = []
			for i in range(n):
				if s[i] == 'A':
					arr.append(i)
			arr.append(n)
			mx = 0
			for i in range(1, len(arr)):
				mx = max(mx, arr[i] - arr[i - 1] - 1)
			print(mx)
main()


n = int(input())
for i in range(n):
	t = int(input())
	s = input().split('A')
	del s[0]
	if len(s) == 0:
		print(0)
	else:
		print(max([len(x) for x in s]))


def transformerEtudiant(k, s):
	nap = ''
	for i in range(k):
		if s[i] == 'A':
			nap = nap + 'A'
		elif i > 0 and s[i - 1] == 'A':
			nap = nap + 'A'
		else:
			nap = nap + 'P'
	return (nap == s, nap)
t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	c = 0
	b = False
	while not b:
		(b, s) = transformerEtudiant(k, s)
		if b == False:
			c += 1
	print(c)


def func():
	n = int(input())
	st = input()
	arr = []
	temp = []
	for i in range(n):
		if st[i] == 'A':
			arr.append(i)
	if len(arr) == 0:
		print(0)
	elif len(arr) == n:
		print(0)
	elif len(arr) == 1 and arr[0] == n - 1:
		print(0)
	else:
		arr.append(n)
		for i in range(1, len(arr)):
			temp.append(arr[i] - arr[i - 1] - 1)
		print(max(temp))
for i in range(int(input())):
	func()


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	ans = 0
	for i in range(n):
		if a[i] == 'A':
			count = 0
			for j in range(i + 1, n):
				if a[j] == 'P':
					count += 1
				else:
					break
			ans = max(ans, count)
		else:
			continue
	print(ans)


for _ in range(int(input())):
	x = int(input())
	y = input()
	l = [x]
	for i in range(x):
		if y[i] == 'A':
			l.append(i)
	l.sort()
	e = [0]
	for i in range(len(l) - 1):
		e.append(l[i + 1] - l[i] - 1)
	print(max(e))


T = int(input())
for i in range(T):
	n = int(input())
	seq = input()
	seq += 'A'
	ans = 0
	first_angry = seq.find('A')
	cnt = 0
	for j in range(first_angry, n + 1):
		if seq[j] == 'A':
			ans = max(ans, cnt)
			cnt = 0
		else:
			cnt += 1
	print(ans)


for i in range(int(input())):
	n = int(input())
	S = input()
	arr = [[S[0], 1]]
	for j in S[1:]:
		if j == arr[-1][0]:
			arr[-1][1] += 1
		else:
			arr.append([j, 1])
	ans = 0
	for i in range(len(arr) - 1):
		if arr[i][0] == 'A':
			ans = max(ans, arr[i + 1][1])
	print(ans)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	s = [x for x in s]
	store = []
	while True:
		count = 0
		for i in range(len(s) - 1, 0, -1):
			if s[i] == 'P' and s[i - 1] == 'A':
				s[i] = 'A'
				count += 1
		if count == 0:
			break
		else:
			store.append(count)
	print(len(store))


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = s.find('A')
	s = s[t:]
	c = s.count('A')
	if s[n - c:] == 'A' * c:
		print('0')
	else:
		a = s.split('A')
		a.sort(reverse=True)
		print(len(a[0]))


for _ in range(int(input())):
	n = int(input())
	s = str(input())

	def check(index):
		if index >= 0 and index < n:
			return s[index] == 'A'
		return False
	(output, pointer) = (0, 0)
	while pointer < n:
		if s[pointer] == 'P':
			(count, sub_pointer) = (1, pointer + 1)
			while sub_pointer < n:
				if s[sub_pointer] == 'P':
					count += 1
					sub_pointer += 1
				else:
					break
			if check(pointer - 1) == True:
				output = max(output, count)
			pointer = sub_pointer
		else:
			pointer += 1
	print(output)


for _ in range(int(input())):
	n = int(input())
	l = list(input())
	ll = []
	c = 0
	c1 = 0
	for i in range(len(l)):
		if l[i] == 'A':
			c = 1
		if c == 1 and l[i] == 'P':
			c1 += 1
		if c >= 1 and l[i] == 'A':
			c = 1
			ll.append(c1)
			c1 = 0
	ll.append(c1)
	print(max(ll))


t = int(input())
for k in range(t):
	n = int(input())
	a = input()
	m = []
	if 'A' in a:
		for i in range(n):
			if a[i] == 'A':
				pos1 = i + 1
				if 'A' in a[i + 1:n]:
					pos2 = a[i + 1:n].index('A') + 1 + i
				else:
					pos2 = n
				m.append(len(a[pos1:pos2]))
		print(max(m))
	else:
		print(0)


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	if s.count('A') == 0:
		print(0)
		continue
	ans = 0
	cnt = 0
	idx = s.find('A')
	for i in range(idx + 1, n):
		if s[i] == 'P':
			cnt += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
	ans = max(ans, cnt)
	print(ans)


for i in range(int(input())):
	s = int(input())
	arr = []
	line = input()
	for j in range(len(line)):
		if line[j] == 'A':
			arr.append(j)
	arr.append(len(line))
	arr.append(len(line))
	ans = -1
	for j in range(1, len(arr)):
		if arr[j] - arr[j - 1] >= ans:
			ans = arr[j] - arr[j - 1]
	print(max(0, ans - 1))


T = int(input())
for _ in range(T):
	n = int(input())
	a = input()
	a += 'A'
	ans = 0
	la = n
	i = 0
	while i < n:
		now = a.find('A', i)
		if now != -1:
			if now - la - 1 >= ans:
				ans = now - la - 1
		la = now
		i = now + 1
	print(ans)


t = int(input())
for tt in range(t):
	n = int(input())
	s = input()
	i = 0
	while i < n and s[i] == 'P':
		i += 1
	cnt_max = 0
	while i < n:
		while i < n and s[i] == 'A':
			i += 1
		cnt = 0
		while i < n and s[i] == 'P':
			i += 1
			cnt += 1
		cnt_max = max(cnt, cnt_max)
	print(cnt_max)


for _ in range(int(input())):
	_ = input()
	line = input().lstrip('P')
	ans = 0
	for sect in line.split('A'):
		ans = max(ans, len(sect))
	print(ans if ans < len(line) else 0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ind = -1
	for i in range(n):
		if s[i] == 'A':
			ind = i
			break
	mx = 0
	if ind == -1:
		print(0)
	else:
		cn = 0
		for i in range(ind + 1, n):
			if s[i] == 'P':
				cn += 1
			else:
				mx = max(mx, cn)
				cn = 0
		mx = max(mx, cn)
		print(mx)


t = int(input())
for _ in range(t):
	n = int(input())
	b = input()
	b = list(b)
	count = 0
	for i in range(n):
		if b[i] == 'A':
			temp = 0
			for j in range(i + 1, n):
				if b[j] == 'P':
					temp += 1
				else:
					break
			count = max(count, temp)
	print(count)


t = int(input())
for x in range(t):
	k = int(input())
	s = input()
	time = list()
	for y in range(k):
		time.append(0)
		if s[y] == 'A':
			for z in range(k):
				if z > y:
					if s[z] == 'P':
						time[y] = time[y] + 1
					else:
						break
	print(max(time))


t = int(input())
for k in range(0, t):
	n = int(input())
	s = [w for w in input()]
	count = -1
	for i in range(0, n):
		index = 9999999999
		flag = 0
		for j in range(0, n):
			if s[j] == 'A' and j + 1 < n and (index != j):
				if s[j + 1] == 'P':
					index = j + 1
					s[j + 1] = 'A'
					flag = 1
		count = count + 1
		if flag == 0:
			print(count)
			break


for q in range(int(input())):
	n = int(input())
	st = input()
	(maxm, count, flag) = (0, 0, 0)
	for i in range(n):
		if st[i] == 'A':
			flag = 1
			maxm = max(maxm, count)
			count = 0
		elif flag == 1:
			count += 1
		else:
			continue
	maxm = max(maxm, count)
	print(maxm)


n = int(input())
e = 0
q = 0
for i in range(n):
	r = int(input())
	j = input()
	g = list(j)
	e = 0
	q = 0
	for j in range(r):
		if g[j] == 'A':
			j = j + 1
			q = 0
			while j != r:
				if g[j] == 'P':
					q += 1
				if g[j] == 'A':
					break
				j += 1
		if e < q:
			e = q
	print(e)


t = eval(input())
while t != 0:
	t -= 1
	n = int(input())
	s = input()
	i = 0
	count = 0
	max_ = 0
	while i < n:
		if s[i] == 'A':
			i += 1
			while i < n and s[i] == 'P':
				count += 1
				i += 1
			if max_ < count:
				max_ = count
			count = 0
		else:
			i += 1
	print(max_)


for t in range(int(input())):
	k = int(input())
	s = input().strip()
	try:
		i = s.index('A')
	except:
		print(0)
		continue
	s = s[i:]
	l = 0
	count = 0
	for c in s:
		if c == 'A':
			count = 0
			continue
		else:
			count += 1
			if count > l:
				l = count
	print(l)


t = eval(input())
ts = []
for z in range(t):
	a = input()
	b = input()
	ts.append(b)
for sit in ts:
	change = True
	i = -1
	use = sit
	while change == True:
		next = ''
		last = ''
		change = False
		for c in use:
			if last == 'A' and c == 'P':
				change = True
				next += 'A'
			else:
				next += c
			last = c
		use = next
		i += 1
	print(i)


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = s.replace('A', '0')
	s = s.replace('P', '1')
	c = 0
	i = 1
	ma = 0
	while i < n:
		j = 0
		c = 0
		if s[i] == '1' and s[i - 1] == '0':
			for j in range(i, n):
				if s[j] == '0':
					break
				c += 1
			if j == n - 1:
				i = j
			else:
				i = j - 1
			ma = max(c, ma)
		i += 1
	print(ma)


for _ in range(int(input())):
	n_ = input()
	s = []
	s.extend(input())
	total = 0
	count = 1
	while count > 0:
		count = 0
		x = -1
		for i in range(int(n_) - 1):
			if s[i] == 'A' and s[i + 1] == 'P' and (i != x):
				count += 1
				s[i + 1] = 'A'
				x = i + 1
		if count > 0:
			total += 1
	print(total)


n = int(input())
result = []
for i in range(n):
	o = input()
	stri = input()
	maxp = 0
	for j in range(len(stri) - 1):
		loc_max_p = 0
		if stri[j] == 'A' and stri[j + 1] != 'A':
			for k in range(j + 1, len(stri)):
				if stri[k] == 'A':
					break
				else:
					loc_max_p += 1
			if loc_max_p > maxp:
				maxp = loc_max_p
	result.append(maxp)
for i in result:
	print(i)


results = []
for p in range(1, int(input()) + 1):
	lenstring = int(input())
	givenstring = input()
	condition = False
	count = 0
	pos = -1
	strarray = []
	i = -1
	while i >= len(givenstring) * -1 and givenstring != '':
		if givenstring[i] == 'A':
			count += 1
			strarray.append(givenstring[i:])
			givenstring = givenstring[:i]
			i = 0
		i += -1
	strarray2 = [c.replace('A', '') for c in strarray]
	strarray = list(filter(None, strarray2))
	if len(strarray) != 0:
		results.append(len(max(strarray)))
	else:
		results.append(0)
for x in results:
	print(x)


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	f = []
	ind = [x for x in range(len(s)) if s[x] == 'A']
	if ind == []:
		print(0)
	else:
		for i in range(1, len(ind)):
			f.append(ind[i] - ind[i - 1] - 1)
		if ind[-1] != n - 1:
			f.append(n - 1 - ind[-1])
		if f != []:
			print(max(f))
		else:
			print(0)


T = int(input())
ans_list = []
for t in range(T):
	length = int(input())
	s_arr = [x for x in input()]
	if length == 1 and s_arr[0] == 'A':
		ans_list.append(0)
	else:
		m = 0
		max_ctr = 0
		ctr = 0
		flag = 0
		while m < length:
			if s_arr[m] == 'A':
				break
			if s_arr[m] != 'A':
				m += 1
		if m >= length - 1:
			ans_list.append(0)
		else:
			for i in range(m, length):
				if s_arr[i] != 'A':
					ctr += 1
				else:
					if ctr > max_ctr:
						max_ctr = ctr
					ctr = 0
			if ctr > max_ctr:
				max_ctr = ctr
			ans_list.append(max_ctr)
for i in ans_list:
	print(i)


for _ in range(int(input())):
	k = int(input())
	s = input()
	max_len = 0
	count = None
	for i in range(len(s)):
		if s[i] == 'A':
			if count != None:
				max_len = max(max_len, count)
			count = 0
		elif count != None:
			count += 1
	if count == None:
		print(0)
		continue
	max_len = max(max_len, count)
	print(max_len)


t = int(input())
for i in range(t):
	n = int(input())
	schoolboys = input()
	tmp = 0
	length = 0
	if schoolboys.find('A') == -1:
		print(0)
		continue
	for j in range(schoolboys.find('A'), len(schoolboys)):
		if schoolboys[j] == 'P':
			tmp += 1
		elif tmp > 0:
			if tmp > length:
				length = tmp
			tmp = 0
	if tmp > length:
		length = tmp
	print(length)


import os
import sys
import bisect
from math import gcd, ceil
from heapq import heapify, heappop, heappush
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		a = []
		m = 0
		if 'A' not in s or 'P' not in s:
			print(0)
			continue
		for i in range(n):
			if s[i] == 'A':
				a.append(i)
		for i in range(1, len(a)):
			m = max(m, a[i] - a[i - 1] - 1)
		m = max(m, len(s) - 1 - a[-1])
		print(m)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = lambda s: self.buffer.write(s.encode()) if self.writable else None

	def read(self):
		if self.buffer.tell():
			return self.buffer.read().decode('ascii')
		return os.read(self._fd, os.fstat(self._fd).st_size).decode('ascii')

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline().decode('ascii')

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

def print(*args, sep=' ', end='\n', file=sys.stdout, flush=False):
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(end)
	if flush:
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10000)
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
sys.setrecursionlimit(int(pow(10, 6)))
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(l()[0]):
	n = l()[0]
	s = sl()[0]
	s = s.lstrip('P')
	mx = 0
	i = 0
	while i < len(s):
		k = 1
		while i + k < len(s) and s[i + k] == 'P':
			k += 1
		mx = max(mx, k - 1)
		i += k
	print(mx)


import sys
Input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()
	(p, k, l) = (0, 0, 0)
	for i in range(n):
		if s[i] == 'A':
			p = 0
			l = 1
		elif l == 1:
			p += 1
			if k < p:
				k = p
	print(l) if l == 0 else print(k)


def angry(m, s):
	c = 0
	d = 0
	t = False
	for i in range(m):
		if s[i] == 'A':
			t = True
			d = max(d, c)
			c = 0
		elif t:
			c += 1
		if t:
			d = max(d, c)
	return d
n = int(input())
for i in range(n):
	m = int(input())
	s = input()
	print(angry(m, s))


m = int(input())
l = []
for i in range(m):
	t = int(input())
	a = input()
	if 'A' in a:
		a = a[a.index('A'):]
		l.append(len(max(a.split('A'), key=len)))
	else:
		l.append(0)
for i in l:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	m = 0
	flag = 0
	a = []
	for j in range(n):
		if s[j] == 'A':
			m = 0
			for k in range(j + 1, n):
				if s[k] == 'P':
					m += 1
					continue
				else:
					break
			a.append(m)
	if len(a) > 0:
		print(max(a))
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	max_count = 0
	x = 0
	if s[0] == 'A':
		x = 0
	else:
		while x < n and s[x] == 'P':
			x += 1
	for i in range(x, n):
		if s[i] == 'P':
			count += 1
		else:
			if count > max_count:
				max_count = count
			count = 0
	if max_count > count:
		print(max_count)
	else:
		print(count)


exec(int(input()) * "input();print(max(0,0,*map(len,input().split('A')[1:])));")


n = int(input())
for i in range(n):
	input()
	s = list(map(len, input().split('A')[1:]))
	m = 0
	if len(s) > 0:
		m = max(s)
	print(m)


q = int(input())
for _ in range(q):
	input()
	g = [c for c in input()]
	l = len(g)
	count = 0
	while True:
		has_new = False
		for i in range(l - 2, -1, -1):
			if g[i] == 'A' and g[i + 1] == 'P':
				has_new = True
				g[i + 1] = 'A'
		if not has_new:
			print(count)
			break
		count += 1


import re
t = int(input())

def score(match):
	(a, b) = match.span()
	return b - a - 1

def ans(str):
	return max((score(match) for match in re.finditer('AP+', str)), default=0)
for i in range(t):
	input()
	s = input()
	print(ans(s))


for i in range(int(input())):
	l = int(input())
	s = list(input())
	c = 0
	cb = 0
	log = 0
	for k in s:
		if log < 1:
			if k == 'A':
				log = 1
		if log == 1:
			if k == 'A':
				if cb < c:
					cb = c
				c = 0
			else:
				c += 1
				if cb < c:
					cb = c
	print(cb)


n = int(input())
while n > 0:
	l = int(input())
	s = str(input())
	k = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		k += 1
	print(k)
	n -= 1


T = int(input())
for i in range(T):
	n = int(input())
	s = input()
	if 'A' in s:
		f = s.index('A')
		s = s[f:].split('A')
		print(len(max(s, key=len)))
	else:
		print(0)


n = int(input())
for casos in range(n):
	a = int(input())
	b = input()
	li = []
	c = 0
	for i in b:
		li.append(i)
	for i in range(a):
		j = 0
		sw = 0
		while j < a - 1:
			if li[j] == 'A' and li[j + 1] == 'P':
				li[j + 1] = 'A'
				j = j + 2
				if sw == 0:
					c = c + 1
					sw = 1
			else:
				j = j + 1
	print(c)


q = int(input())
for j in range(q):
	maxlen = 0
	l = int(input())
	s = list(input())
	i = 0
	while i < l - 1:
		temp = ''
		if (s[i] != 'A') * (s[i + 1] != 'A'):
			temp = s[i + 1] + s[i]
			s[i] = temp
			del s[i + 1]
			l -= 1
		else:
			i += 1
	for i in range(0, l - 1):
		if (s[i] == 'A') * (s[i + 1] != 'A'):
			if len(s[i + 1]) > maxlen:
				maxlen = len(s[i + 1])
	print(maxlen)


t = int(input())
for i in range(t):
	x = int(input())
	s = str(input())
	y = 0
	z = 'AP' in s
	while z == True:
		s = s.replace('AP', 'AA')
		y = y + 1
		z = 'AP' in s
	print(y)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s = list(input())
	m = 0
	lastA = None
	for i in range(len(s)):
		if s[i] == 'A' and lastA == None:
			lastA = i
		elif s[i] == 'A' and lastA != None:
			m = max(m, i - lastA - 1)
			lastA = i
		if i == len(s) - 1 and lastA != None:
			m = max(m, i - lastA)
	print(m)


for i in range(0, int(input())):
	k = int(input())
	s = input()
	s = list(s)
	t = 0
	for j in range(0, len(s) - 1):
		te = 0
		if s[j] == 'A':
			for k in range(j + 1, len(s)):
				if s[k] == 'A':
					break
				else:
					te += 1
		if te >= t:
			t = te
	print(t)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	max_val = 0
	acc_val = 0
	p_flag = False
	for c in s:
		if not p_flag and c == 'A':
			p_flag = True
		if p_flag and c == 'A':
			max_val = max(acc_val, max_val)
			acc_val = 0
		if p_flag and c == 'P':
			acc_val += 1
	max_val = max(acc_val, max_val)
	print(max_val)


t = int(input())
new_str = ''
for j in range(t):
	n = int(input())
	s = input()
	count = 0
	while 1:
		flag = False
		for i in range(n - 2, -1, -1):
			if s[i] == 'A' and s[i + 1] == 'P':
				flag = True
				new_str = s[:i + 1] + 'A' + s[i + 2:]
				s = new_str
		if flag == False:
			break
		count += 1
	print(count)


t = int(input())
for _ in range(t):
	k = int(input())
	s = list(input())
	l = 0
	while 'AP' in ''.join(s):
		i = 0
		while i < k - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				i += 2
			else:
				i += 1
		l += 1
	print(l)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	l = 0
	st = 0
	for i in range(n):
		if s[i] == 'P' and st == 0:
			continue
		elif s[i] == 'A':
			l = 0
			st = 1
		else:
			l += 1
			m = max(m, l)
	print(m)


import sys, itertools
T = int(sys.stdin.readline().strip())
for _ in range(T):
	n = int(sys.stdin.readline().strip())
	s = sys.stdin.readline().strip()
	angry = False
	longest = 0
	for (k, g) in itertools.groupby(s):
		if k == 'A':
			angry = True
		elif angry:
			longest = max(longest, len(list(g)))
	print(longest)


t = int(input())
for _ in range(t):
	k = int(input())
	line = input()
	first_A = 0
	for l in line:
		if l == 'A':
			break
		first_A += 1
	start = -1
	maks = 0
	for i in range(first_A, k):
		if line[i] == 'P' and start == -1:
			start = i
		if line[i] == 'A':
			start = -1
		if start != -1:
			if i - start + 1 > maks:
				maks = i - start + 1
	print(maks)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	s = list(s)
	if len(s) == 1:
		print(0)
	else:
		c = 0
		i = 0
		while i < len(s):
			if s[i] == 'P':
				c = c + 1
			else:
				break
			i = i + 1
		s[:c] = []
		if len(s) <= 1:
			print(0)
		else:
			ss = []
			per = 0
			for i in range(len(s)):
				if s[i] == 'A':
					ss.append(per)
					per = 0
				elif s[i] == 'P':
					per = per + 1
			ss.append(per)
			print(max(ss))


for _ in range(int(input())):
	n = int(input())
	s = input()
	lol = list(s.split('A'))
	lol.pop(0)
	curr = 0
	for i in lol:
		curr = max(len(i), curr)
	print(curr)


t = int(input())
for i in range(t):
	n = int(input())
	students = input()
	count = 0
	maxCount = 0
	prev = 'P'
	i = 0
	while i < len(students) and students[i] != 'A':
		i += 1
	for j in range(i, len(students)):
		if students[j] == 'P':
			count += 1
		else:
			count = 0
		maxCount = max(maxCount, count)
	print(maxCount)


t = int(input())
for q in range(t):
	k = int(input())
	s = input()
	if s.find('A') == -1:
		print(0)
	else:
		j = s.index('A')
		s = s[j:]
		p = t = 0
		for i in range(k - j):
			if s[i] == 'A':
				if p > t:
					t = p
				p = 0
			else:
				p += 1
			if p > t:
				t = p
		print(t)


for t in range(int(input())):
	l = int(input())
	s = input()
	maxi = 0
	cur = 0
	for i in range(l - 1, -1, -1):
		if s[i] == 'P':
			cur += 1
		else:
			maxi = max(maxi, cur)
			cur = 0
	print(maxi)


import calendar
t = int(input())
for _ in range(t):
	n = int(input())
	string = input()
	for i in range(1, 10000000):
		if 'AP' not in string:
			print(i - 1)
			break
		else:
			new = list(string)
			j = 0
			while j < n - 1:
				if new[j] == 'A' and new[j + 1] == 'P':
					new[j + 1] = 'A'
					j += 2
				else:
					j += 1
			string = ''.join(new)


import sys
from itertools import groupby
for i in range(int(input())):
	n = int(input())
	s = input()
	fl = False
	res = 0
	for (i, j) in groupby(s):
		if i == 'A':
			fl = True
		elif fl:
			res = max(len(list(j)), res)
	print(res)


import re

def main():
	f = re.compile('AP+').findall
	for _ in range(int(input())):
		input()
		print(max(map(len, f(input())), default=1) - 1)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l = input()
	c = 0
	for i in range(len(l) - 1):
		if l[i] == 'A':
			c = 1
			break
	if c == 0:
		print('0')
	else:
		idx = 0
		for i in range(len(l)):
			if l[i] == 'A':
				idx = i
				break
		a = 0
		ma = 0
		for i in range(idx, len(l) - 1):
			if l[i] == 'P' and l[i] == l[i + 1]:
				a += 1
			else:
				if l[i] == 'P':
					a += 1
				if ma < a:
					ma = a
				a = 0
		if l[len(l) - 1] == 'P':
			a += 1
		if ma < a:
			ma = a
			a = 0
		print(ma)


for _ in range(int(input())):
	length = int(input())
	s = input()
	max_pos = temp_pos = 0
	for i in range(length - 1, -1, -1):
		if s[i] == 'A':
			max_pos = max(max_pos, temp_pos)
			temp_pos = 0
		else:
			temp_pos += 1
	print(max_pos)


t = int(input())
for test in range(t):
	n = int(input())
	S = input()
	ind = 0
	while ind < n and S[ind] == 'P':
		ind += 1
	ANS = 0
	count = 0
	for i in range(ind, n):
		if S[i] == 'P':
			count += 1
		else:
			ANS = max(ANS, count)
			count = 0
	ANS = max(ANS, count)
	print(ANS)


def chert(s):
	new = [s[0]]
	for i in range(1, len(s)):
		if s[i - 1] == 'A':
			new.append('A')
		else:
			new.append(s[i])
	return list(new)
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	ans = 0
	prev = s[:]
	while True:
		s = chert(s)
		if s == prev:
			break
		ans += 1
		prev = s
	print(ans)


t = int(input())
e = []
for i in range(t):
	k = int(input())
	u = input()
	u = list(u)
	x = 1
	s = 0
	d = 0
	while x != k:
		if u[x] == 'P' and u[x] != u[x - 1]:
			d = 1
			u[x] = 'A'
			if x + 1 != k:
				x += 1
		x += 1
		if x == k and d == 1:
			x = 1
			d = 0
			s += 1
	e.append(s)
for i in e:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	pre = -1
	ans = 0
	for i in range(0, n):
		if pre > -1 and s[i:i + 1] == 'A':
			ans = max(ans, i - pre - 1)
		if s[i:i + 1] == 'A':
			pre = i
	if pre > -1:
		ans = max(ans, n - pre - 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	c = 0
	for i in range(n):
		rslt = 0
		if a[i] == 'A' and i + 1 < n:
			for j in range(i + 1, n):
				if a[j] == 'P':
					rslt += 1
				else:
					break
			i = j
		if rslt > c:
			c = rslt
	print(c)


for i in range(int(input())):
	n = int(input())
	s = list(input())
	count = 0
	f = 1
	while f == 1:
		i = 0
		while i < n - 1:
			if s[i] == 'A' and s[i + 1] != 'A':
				s[i + 1] = 'A'
				i += 2
				f = 0
			else:
				i += 1
		if f == 0:
			count += 1
			f = 1
		else:
			f = 0
	print(count)


for _ in range(int(input())):
	N = int(input())
	st = input()
	j = 0
	while j < len(st) and st[j] != 'A':
		j += 1
	ma_x = 0
	while j < len(st):
		max2 = 0
		j += 1
		while j < len(st) and st[j] != 'A':
			j += 1
			max2 += 1
		ma_x = max(ma_x, max2)
	print(ma_x)


for i in range(int(input())):
	n = int(input())
	s = input()
	ans = -1000000000.0
	cur = 0
	fl = False
	for j in range(n):
		if s[j] == 'A':
			fl = True
			ans = max(ans, cur)
			cur = 0
		elif fl == True:
			cur += 1
	print(max(cur, ans))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	x = 0
	flag = True
	move = False
	if len(s) == 1:
		print(0)
	else:
		while flag:
			flag = False
			for i in range(0, n - 1):
				if not move:
					if s[i] == 'A' and s[i + 1] == 'P':
						s[i + 1] = 'A'
						flag = True
						move = True
				else:
					move = False
			if not flag:
				break
			x += 1
		print(x)


import re
testcases = int(input())
while testcases > 0:
	testcases -= 1
	strlen = int(input())
	data = input()
	if data[:len(data) - 1].find('A') == -1:
		print(0)
		continue
	data = data[data.find('A'):]
	counter = 0
	while 'P' in data:
		lst = [x for x in re.split('A', data)]
		for (j, item) in enumerate(lst):
			if item != '':
				idx = item.find('P')
				if idx != -1:
					lst[j] = 'A' + item[idx + 1:]
		data = ''.join(lst)
		counter += 1
	print(counter)


t = int(input())
for i in range(t):
	n = int(input())
	s1 = str(input()).upper()
	tup = tuple(s1)
	ls = list(tup)
	(cnt, temp) = (0, 0)
	for i in range(n):
		if ls[i] == 'A':
			for j in range(i + 1, n):
				if ls[j] == 'P':
					cnt += 1
				elif ls[j] == 'A':
					break
			if cnt > temp:
				temp = cnt
			cnt = 0
	print(temp)


try:
	for _ in range(int(input())):
		n = int(input())
		l = [str(i) for i in input()]
		m = []
		for i in range(n):
			if l[i] == 'A':
				m.append(i)
		m.append(n)
		for i in range(len(m) - 1):
			m[i] = m[i + 1] - m[i]
		m[len(m) - 1] = 0
		if max(m) != 0:
			print(max(m) - 1)
		else:
			print(0)
except:
	pass


for _ in range(int(input())):
	input()
	s = input()
	print(max(map(len, s.split('A')[1:]), default=0))


t = int(input())
while t > 0:
	k = int(input())
	s = input()
	count = -1
	max_count = 0
	for c in s:
		if c == 'P':
			if count != -1:
				count += 1
		else:
			max_count = max(max_count, count)
			count = 0
	max_count = max(max_count, count)
	print(max_count)
	t -= 1


import re
for t in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	while True:
		z = re.search('AP', s)
		if z == None:
			break
		else:
			s = re.sub('AP', 'AA', s)
			t += 1
	print(t)


t = int(input())
for _ in range(t):
	m = 0
	n = int(input())
	x = input()
	i = 0
	while i < n:
		if x[i] == 'A':
			j = i + 1
			while j < n:
				if x[j] == 'P':
					j += 1
				else:
					break
			if j - i > m:
				m = j - i - 1
			i = j
		else:
			i += 1
	print(m)


def func(x):
	t = 0
	l = [0]
	for i in x[::-1]:
		if i == 'P':
			t += 1
		else:
			l.append(t)
			t = 0
	return max(l)
y = int(input())
for i in range(y):
	n = int(input())
	x = input()
	print(func(x))


n = int(input())
for _ in range(n):
	ki = int(input())
	k = list(input())
	streak = 0
	longest = 0
	isStreak = False
	for i in range(ki):
		if isStreak:
			if k[i] == 'P':
				streak += 1
			else:
				longest = max(streak, longest)
				streak = 0
		if k[i] == 'A':
			isStreak = True
	print(max(streak, longest))


t = int(input())
while t > 0:
	l = int(input())
	si = input()
	if 'A' in si:
		s = list(si.split('A'))[1:]
		s = [len(x) for x in s]
		print(max(s))
	else:
		print(0)
	t -= 1


n_groups = int(input())
solutions = []
for k in range(n_groups):
	group_size = int(input())
	group = input()
	maks = 0
	for i in range(len(group)):
		pom_maks = 0
		if group[i] == 'A':
			for j in range(i + 1, len(group)):
				if group[j] == 'P':
					pom_maks += 1
				else:
					break
		if pom_maks > maks:
			maks = pom_maks
	solutions.append(maks)
for num in solutions:
	print(num)


n = int(input())
acum2 = 0
for j in range(n):
	n2 = int(input())
	s = str(input())
	acum = 0
	count = 0
	for k in range(len(s)):
		if acum2 > acum:
			acum = acum2
		acum2 = 0
		if s[k] == 'A':
			for g in range(k + 1, len(s)):
				if s[g] == 'P':
					acum2 += 1
				if s[g] == 'A':
					break
	print(acum)


for t in range(int(input())):
	n = int(input())
	s = input()
	(ans, ii, f) = (0, 0, 0)
	for i in s:
		if i == 'A':
			f = 1
		if f:
			if i == 'P':
				ii += 1
				ans = max(ans, ii)
			else:
				ii = 0
		else:
			ii = 0
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = input()
	a = s.lstrip('P').split('A')
	print(len(max(a, key=len)))


t = int(input())
for _ in range(t):
	n = int(input())
	string = list(input())
	index_of_A = []
	for i in range(n):
		if string[i] == 'A':
			index_of_A.append(i)
	count = []
	empty = []
	index_of_A.append(n)
	for i in range(len(index_of_A) - 1):
		count.append(index_of_A[i + 1] - index_of_A[i] - 1)
	if count == empty:
		print(0)
	else:
		print(max(count))


def func(s, n):
	start = s.find('A')
	if start == -1:
		return 0
	elif start == n - 1:
		return 0
	else:
		nex = start + 1
		run = True
		max = 0
		while run and nex < n:
			if s[nex] == 'P':
				if nex - start > max:
					max = nex - start
			else:
				start = nex
			nex += 1
		return max
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(func(s, n))


for _ in range(int(input())):
	input()
	ns = input()
	s = ns.lstrip('P')
	time = 0
	count = 0
	for i in range(len(s)):
		if s[i] == 'A':
			count = 0
		else:
			count += 1
			if count > time:
				time = count
	print(time)


a = int(input())
num = []
my_list_of_strs = []
ans = []
for f in range(1, a + 1):
	num.append(input())
	my_list_of_strs.append(input())
for i in range(0, a):
	x = 0
	temp = my_list_of_strs[i].count('AP')
	while temp != 0:
		my_list_of_strs[i] = my_list_of_strs[i].replace('AP', 'AA')
		x += 1
		temp = my_list_of_strs[i].count('AP')
	ans.append(x)
for q in range(0, a):
	print(ans[q])


num_of_cases = int(input())
for _ in range(num_of_cases):
	length = int(input())
	arr = [char for char in input()]
	A = 0
	P = 0
	long = 0
	arr_long = []
	count = True
	for char in arr:
		if char == 'A':
			count = True
			A = A + 1
		elif A > 0:
			count = False
		if not count:
			long = long + 1
		else:
			arr_long.append(long)
			long = 0
	arr_long.append(long)
	print(max(arr_long))


def angry():
	for _ in range(t):
		n = int(input())
		stus = input()
		res = 0
		temp = None
		for i in range(n):
			if stus[i] == 'A':
				if temp:
					res = max(res, temp)
				temp = 0
			else:
				if temp != None:
					temp += 1
				if i == n - 1:
					if temp == None:
						temp = 0
					res = max(res, temp)
		yield res
t = int(input())
ans = angry()
print(*ans, sep='\n')


for i in range(int(input())):
	input()
	maxi = 0
	index = -1
	for (indice, j) in enumerate(input() + 'A'):
		if j == 'A':
			if index != -1:
				maxi = max(maxi, indice - index - 1)
			index = indice
	print(maxi)


for _ in ' ' * int(input()):
	input()
	c = 0
	k = input()[::-1]
	for _ in ' ' * k.count('A'):
		c = max(k.find('A'), c)
		k = k[k.index('A') + 1:]
	print(c)


query = int(input())
for _ in range(query):
	n = int(input())
	s = input()
	g = 0
	As = ''
	a = []
	for i in range(n - 1):
		if s[i] == 'A':
			As += s[i:]
			break
	p = len(As)
	for j in range(p):
		if j == p - 1:
			if As[j] == 'A':
				a.append(g)
				g = 0
			else:
				g += 1
				a.append(g)
		elif As[j] == 'P':
			g += 1
		else:
			a.append(g)
			g = 0
	if a:
		print(max(a))
	else:
		print(0)


for i in range(int(input())):
	n = input()
	s = str(input())
	c = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		c += 1
	print(c)


def solve():
	n = eval(input())
	s = list(input())
	ans = 0
	while True:
		ok = 0
		i = 1
		while i < n:
			if s[i] == 'P' and s[i - 1] == 'A':
				s[i] = 'A'
				ok = 1
				i += 2
			else:
				i += 1
		if ok == 0:
			break
		ans += ok
	print(ans)
t = eval(input())
while t:
	solve()
	t -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	ans = 0
	k = int(input())
	s = input().rstrip()
	p = -1
	for i in range(k):
		if s[i] == 'A':
			ans = max(ans, p)
			p = 0
		elif p != -1:
			p += 1
	ans = max(ans, p)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	s = list(s)
	r = 0
	a = []
	a.append(0)
	for i in range(n - 1, -1, -1):
		if s[i] == 'P':
			r += 1
		elif s[i] == 'A':
			a.append(r)
			r = 0
	print(max(a))


t = int(input())
for tt in range(0, t):
	k = int(input())
	s = input()
	ss = [i for i in s]
	kk = s.find('A')
	if kk == -1 or kk == k - 1:
		print(0)
	else:
		ans = 0
		ss = ss[kk:]
		i = 1
		while 'P' in ss:
			if i < len(ss) and ss[i - 1] == 'A' and (ss[i] == 'P'):
				ss[i] = 'A'
				i += 2
			else:
				i += 1
			if 'P' not in ss:
				ans += 1
				break
			if i >= len(ss):
				ans += 1
				i = 1
		print(ans)


for i in range(int(input())):
	input()
	t = input()
	r = list(map(len, t.split('A')))
	if not t[0] == 'A':
		r = r[1:]
	try:
		print(max(r))
	except ValueError:
		print(0)


for i in range(int(input())):
	k = int(input())
	s = input().lstrip('P').split('A')
	print(len(max(s)))


for _ in range(int(input())):
	n = int(input())
	l = input()
	ind = -1
	for (i, x) in enumerate(l):
		if x == 'A':
			ind = i
			break
	l = l[ind:]
	if l == 'P':
		print(0)
		continue
	l = list(l.split('A'))
	print(max(map(len, l)))


def split(a):
	return [char for char in a]
testcases = int(input())
while testcases != 0:
	n = int(input())
	students = input()
	count = 0
	ans = 0
	students_split = split(students)
	for i in range(n - 1, -1, -1):
		if students_split[i] == 'P':
			count += 1
		else:
			ans = max(ans, count)
			count = 0
	print(ans)
	testcases -= 1


t = int(input())
for z in range(t):
	n = int(input())
	s = input()
	a = s.split('A')
	if s[0] == 'P':
		a = a[1:]
	ans = 0
	for st in a:
		ans = max(ans, len(st))
	print(ans)


t = int(input())
for _ in range(0, t):
	n = int(input())
	s = input()
	s = s + 'A'
	c = 0
	f = 1
	for i in range(0, n + 1):
		if s[i] == 'A' and f:
			f = 0
			g = i
		elif s[i] == 'A':
			c = max(i - g, c)
			g = i
	print(max(0, c - 1))


for i in range(int(input())):
	k = int(input())
	s = input() + 'A'
	m = 0
	f = False
	t = 0
	for j in s:
		if j == 'A':
			f = True
		if f:
			if j == 'A':
				if t > m:
					m = t
				t = 0
			else:
				t += 1
	print(m)


for _ in range(int(input())):
	k = int(input())
	s = input()
	pre = -1
	ans = 0
	for i in range(k):
		if s[i] == 'A':
			if pre == -1:
				pre = i
			else:
				ans = max(ans, i - pre - 1)
				pre = i
	if pre != -1:
		ans = max(ans, k - 1 - pre)
	else:
		ans = 0
	print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def main():
	n = inp()
	for i in range(n):
		arr_length = inp()
		string = insr()
		arr = string.split('A')
		if string[0] == 'P':
			arr = arr[1:]
		max_size = 0
		for elem in arr:
			if len(elem) > max_size:
				max_size = len(elem)
		print(max_size)
main()


def solve():
	n = int(input())
	s = input()
	mx = 0
	cnt = 0
	was = 0
	for i in range(n):
		if s[i] == 'A':
			mx = max(mx, cnt)
			cnt = 0
			was = 1
		elif was and s[i] == 'P':
			cnt += 1
	if was:
		mx = max(mx, cnt)
	print(mx)
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	k = int(input())
	students = input()
	pos = []
	for i in range(k):
		if students[i] == 'A':
			pos.append(i)
	pos.append(k)
	ans = 0
	for i in range(1, len(pos)):
		ans = max(pos[i] - pos[i - 1] - 1, ans)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	S = input()
	(ans, cnt) = (0, 0)
	before_a = False
	for s in S:
		if s == 'P':
			if before_a:
				cnt += 1
			else:
				pass
		else:
			before_a = True
			ans = max(ans, cnt)
			cnt = 0
	else:
		ans = max(ans, cnt)
	print(ans)


t = int(input())
for i in range(t):
	input()
	s = input()
	m = 0
	p = 0
	for j in s[::-1]:
		if j == 'P':
			p += 1
		else:
			m = max(m, p)
			p = 0
	print(m)


def main():
	n = int(input())
	line = input()
	if not 'A' in line:
		print(0)
		return
	line = line[line.index('A'):]
	tm = 0
	mx = 0
	for i in line:
		if i == 'A':
			tm = 0
		if i == 'P':
			tm += 1
			mx = max(mx, tm)
	print(mx)
t = int(input())
for i in range(t):
	main()


test = int(input())
for x in range(0, test):
	rand = int(input())
	str = input()
	count = 0
	maxi = 0
	try:
		i = str.index('A')
	except:
		pass
	str2 = str.lstrip('P')
	for i in str2:
		if i == 'P':
			count = count + 1
			if count > maxi:
				maxi = count
		else:
			count = 0
	print(maxi)


n = int(input())
for i in range(n):
	size = int(input())
	s = input()
	cnt = 0
	temp = s.replace('AP', 'AA')
	while temp != s:
		cnt += 1
		s = temp
		temp = s.replace('AP', 'AA')
	print(cnt)


t = int(input())
for _ in range(t):
	k = int(input())
	S = input()
	ans = 0
	c = -1
	for s in S:
		if s == 'A':
			ans = max(ans, c)
			c = 0
		elif c != -1:
			c += 1
	print(max(ans, c))


t = int(input())
for j in range(t):
	n = int(input())
	stri = input()
	(ct, i) = (0, 0)
	while 'AP' in stri:
		stri = stri.replace('AP', 'AA')
		ct += 1
	print(ct)


def ThrowSnowball(a, n):
	global L
	b = [k for k in a]
	hasThrown = False
	for m in range(n - 1):
		if a[m] == 1 and a[m + 1] == 0:
			b[m + 1] = 1
			hasThrown = True
	L[1] = [i for i in b]
	if not hasThrown:
		return False
	return True
t = int(input())
L = [0] * 2
ans = []
for i in range(t):
	L[0] = int(input())
	L[1] = []
	s = input()
	for c in s:
		if c == 'P':
			b = 0
		else:
			b = 1
		L[1].append(b)
	sec = 0
	while ThrowSnowball(L[1], L[0]):
		sec += 1
	ans.append(sec)
for i in ans:
	print(i)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	a = 0
	l = []
	for j in range(len(s)):
		if s[j] == 'P':
			a += 1
		elif s[j] == 'A':
			l.append(a)
			a = 0
	l.append(a)
	if s[0] == 'P':
		del l[0]
	if len(l) > 0:
		print(max(l))
	else:
		print(0)


for i in range(int(input())):
	a = int(input())
	b = input()
	c = list(b)
	while c.count('A') != 0 and c[0] != 'A':
		c.remove(c[0])
	d = []
	if c.count('P') == 0 or c.count('A') == 0:
		print(0)
	else:
		for j in range(len(c)):
			if c[j] == 'P':
				k = j
				while k < len(c) and c[k] == 'P':
					k += 1
				d.append(k - j)
		print(max(d))


q = lambda : map(int, input().split())
qi = lambda : int(input())
qs = lambda : input().split()
for _ in [0] * qi():
	input()
	a = input()
	if len(a) == 1 or a.count('P') == 0 or a.count('A') == 0:
		print(0)
	else:
		print(len(max(a[a.index('A'):].split('A'))))


x = int(input())
for x in range(0, x):
	n = int(input())
	y = list(input())
	p = len(y)
	l = 0
	chk = False
	ans = 0
	for a in y:
		if a == 'A' and (not chk):
			chk = True
			l = 0
		elif a == 'A' and chk:
			ans = max(ans, l)
			l = 0
		else:
			l += 1
	if chk:
		ans = max(ans, l)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	line = input()
	sline = line.split('A')
	sline.append('')
	if line[0] == 'A':
		print(len(max(sline, key=lambda x: len(x))))
	else:
		print(len(max(sline[1:], key=lambda x: len(x))))


t = int(input())
while t > 0:
	n = int(input())
	x = 0
	ch = list(input())
	ok = False
	count = 0
	while ok == False:
		ok = True
		x = 0
		i = 0
		while i < n - 1:
			if ch[i] == 'A' and ch[i + 1] == 'P':
				ch[i + 1] = 'A'
				i = i + 2
				x = x + 1
			else:
				i = i + 1
		if x > 0:
			ok = False
			count = count + 1
	print(count)
	t -= 1


t = int(input())
for k in range(t):
	n = int(input())
	a = str(input())
	f = 0
	s = []
	for i in range(n):
		s.append(a[i])
	h = [0 for i in range(n)]
	for i in range(n):
		h[i] = s[i]
	while f <= n:
		z = 0
		for i in range(n - 1):
			if s[i] == 'A' and s[i + 1] == 'P':
				h[i + 1] = 'A'
				z += 1
		for i in range(n):
			s[i] = h[i]
		if z == 0:
			break
		f += 1
	print(f)


t = int(input())
for _ in range(t):
	n = int(input())
	line = input()
	mini = 0
	count = 0
	flag = 0
	for i in line:
		if flag == 0 and i == 'P':
			continue
		elif i == 'A':
			flag = 1
			mini = max(count, mini)
			count = 0
		if flag == 1 and i == 'P':
			count += 1
	print(max(mini, count))


t = int(input())
arr = []
for _ in range(t):
	arr.append(int(input()))
	arr.append(input().strip())
for i in range(t):
	(s, l) = (arr[2 * i + 1], arr[2 * i])
	(last_min, sm, flag) = (0, 0, 0)
	for idx in range(l):
		if s[idx] == 'A':
			flag = 1
			last_min = max(last_min, sm)
			sm = 0
		elif flag:
			sm += 1
	print(max(last_min, sm))


for _ in range(int(input())):
	n = input()
	s = [str(i) for i in input()]
	if 'A' not in s:
		print(0)
	else:
		while s[0] != 'A':
			del s[0]
		ans = 0
		c = 0
		for i in range(1, len(s)):
			if s[i] == 'P' and (s[i - 1] == 'P' or s[i - 1] == 'A'):
				c += 1
			else:
				c = 0
			if ans < c:
				ans = c
		print(ans)


for t in range(int(input())):
	n = int(input())
	s = input()
	s = s.lstrip('P')
	s = s.split('A')
	mx = 0
	for x in s:
		mx = max(mx, len(x))
	print(mx)


T = int(input())
for z in range(T):
	n = int(input())
	v = str(input()).split('A')
	v[0] = ''
	print(max(list(map(lambda a: len(a), v))))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	ans = 0
	while s.count('AP') != 0:
		ans += 1
		s = s.replace('AP', 'AA')
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	max = 0
	for i in range(len(s)):
		if s[i] == 'A':
			c = 0
			while i + 1 < len(s):
				if s[i + 1] == 'P':
					i += 1
					c += 1
					if i + 1 == len(s):
						if max < c:
							max = c
							break
				else:
					i += 1
					if max < c:
						max = c
					break
	print(max)


t = int(input())
for i in range(t):
	input()
	s = input()
	if s[0] == 'P':
		start = 1
	else:
		start = 0
	s = list(s.split('A'))
	s = s[start:]
	res = 0
	for x in s:
		if len(x) > res:
			res = len(x)
	print(res)


t = int(input())
for _ in range(t):
	(k, s) = (int(input()), input())
	(res, last) = (0, None)
	for i in range(k):
		if s[i] == 'A':
			last = i
		elif last is not None:
			res = max(res, i - last)
	print(res)


for i in range(int(input())):
	x = int(input())
	s = input()
	a = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		a = a + 1
	print(a)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	time = 0
	nc = 0
	if s == 'P' or s == 'A':
		print(0)
		continue
	for i in range(n - 1, -1, -1):
		if s[i] == 'P':
			nc += 1
		else:
			time = max(time, nc)
			nc = 0
	print(time)


import sys
t = int(input())
while t > 0:
	n = int(input())
	s = input()
	ans = 0
	ok = False
	cnt = 0
	for i in range(n):
		if ok and s[i] == 'P':
			cnt += 1
			ans = max(ans, cnt)
		elif s[i] == 'A':
			cnt = 0
			ok = True
	print(ans)
	t -= 1


import random

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
for _ in range(int(input())):
	n = int(input())
	s = input()
	a = []
	ind = -1
	for i in range(n):
		if s[i] == 'A':
			ind = i
			break
	ans = 0
	if ind == -1:
		print(0)
	else:
		for i in range(ind, n):
			if s[i] == 'A':
				if len(a):
					ans = max(ans, i - a[len(a) - 1] - 1)
				a.append(i)
		if len(a):
			ans = max(ans, n - 1 - a[len(a) - 1])
		print(ans)


def count_P(s):
	ls = s.split('A')
	maxLen = 0
	for i in ls:
		if len(i) > maxLen:
			maxLen = len(i)
	return maxLen
for ts in range(int(input())):
	l = int(input())
	s = input()
	if len(s) == 1:
		print(0)
	elif s[0] == 'A':
		print(count_P(s))
	else:
		ind = s.find('A')
		if ind == -1:
			print(0)
		else:
			print(count_P(s[ind:]))


q = int(input())
for i in range(q):
	n = int(input())
	s = input()
	for j in range(n - 1, -1, -1):
		if 'A' + 'P' * j in s:
			print(j)
			break
	if 'A' not in s:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	if s[-1] != 'A':
		s = s + 'A'
	w = s.split('A')
	if s[0] != 'A':
		max = len(w[1])
		for j in range(2, len(w)):
			if len(w[j]) > max:
				max = len(w[j])
	if s[0] == 'A':
		max = len(w[0])
		for j in range(1, len(w)):
			if len(w[j]) > max:
				max = len(w[j])
	print(max)


t = int(input().strip())
for _ in range(t):
	k = int(input().strip())
	g = input()
	(ai, m) = (-1, 0)
	for (i, s) in enumerate(g):
		if s == 'A':
			if ai >= 0:
				m = max(m, i - ai - 1)
			ai = i
	else:
		if ai >= 0:
			m = max(m, k - ai - 1)
	print(m)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = []
	count = status = 0
	for i in range(n):
		if a[i] == 'A':
			status = 1
			b.append(count)
			count = 0
		elif status == 1 and a[i] == 'P':
			count += 1
		if i == n - 1:
			b.append(count)
	print(max(b))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	a = n - 1
	list = []
	q = s.count('A')
	for j in range(n):
		if s[j] == 'A':
			a = j
			list.append(j)
	list.append(n)
	if q != 0 and q != n:
		list1 = []
		for j in range(len(list) - 1):
			list1.append(list[j + 1] - list[j])
		x = max(list1)
	if q == 0 or q == n:
		print(0)
	else:
		print(x - 1)


for i in range(int(input())):
	n = int(input())
	s = input()
	a = 0
	h = -1
	for i in range(0, n):
		if s[i] == 'A':
			h = i
			break
	if h != -1:
		i = h
		while i < n:
			if s[i] == 'P':
				c = 0
				while i < n and s[i] == 'P':
					i += 1
					c += 1
					a = max(a, c)
			else:
				i += 1
	print(a)


t = int(input())
while t:
	k = int(input())
	s = input()
	cout = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		cout += 1
	print(cout)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	count = [-1] * n
	for (i, a) in enumerate(s):
		if a == 'A':
			count[i] = 0
		elif i > 0 and count[i - 1] >= 0:
			count[i] = count[i - 1] + 1
	print(max(0, max(count)))


for _ in range(int(input())):
	n = int(input())
	a = list(input().split('A'))
	a.pop(0)
	if n != 1 and len(a) > 0:
		print(len(max(a)))
	else:
		print('0')


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	ans = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	t = 0
	flag = False
	for i in range(n):
		if s[i] == 'A':
			if m < t and flag == True:
				m = t
			t = 0
			if not flag:
				flag = True
		elif flag:
			t += 1
	print(max(m, t))


for _ in range(int(input())):
	k = int(input())
	s = input().strip()
	s1 = s.find('A')
	if s1 == -1:
		print(0)
		continue
	s = s[s1:]
	w = s.split('A')
	ll = [len(i) for i in w]
	ans = max(ll)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(input())
	x = 0
	max = 0
	for i in range(n):
		flag = 0
		curr = 0
		if l[i] == 'A':
			for j in range(i + 1, n):
				if l[j] == 'P':
					curr += 1
				else:
					break
				if curr > max:
					max = curr
		curr = 0
	print(max)


def program():
	n = int(input())
	s = input()
	count = 0
	l = []
	if 'A' not in s:
		print('0')
		return 0
	for i in range(n):
		if s[n - i - 1] == 'P':
			count += 1
		elif s[n - i - 1] == 'A':
			l.append(count)
			count = 0
	print(max(l))
t = int(input())
for i in range(t):
	program()


for _ in range(int(input())):
	n = int(input())
	s = input()
	ss = list(s)
	num = -1
	max = 0
	for i in range(n):
		if ss[i] == 'A':
			if num > max:
				max = num
			num = 0
		elif ss[i] == 'P' and num > -1:
			num += 1
	if num > max:
		max = num
	print(max)


for _ in ' ' * int(input()):
	n = int(input())
	a = input()
	mx = 0
	ind = 0
	f = 0
	if 'A' not in a:
		print(0)
	else:
		for i in range(n):
			if a[i] == 'A':
				if f == 0:
					mx = 0
					f = 1
				else:
					mx = max(mx, i - ind - 1)
				ind = i
		print(max(n - ind - 1, mx))


t = int(input())
for q in range(t):
	n = int(input())
	s = input()
	maxs = 0
	l = 0
	flag = 0
	for i in range(n):
		if s[i] == 'A':
			flag = 1
		if s[i] == 'P' and flag == 1:
			l += 1
		if (s[i] == 'A' or i == n - 1) and flag == 1:
			if l > maxs:
				maxs = l
			l = 0
	print(maxs)


t = int(input())
for i in range(t):
	n = int(input())
	l1 = input().split('A')
	l1 = l1[1:]
	ans = 0
	for j in l1:
		ans = max(ans, len(j))
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = input()
	a += 'A'
	s = 0
	flag = 0
	c = 0
	for i in a:
		if flag == 0 and i == 'P':
			continue
		if flag == 0 and i == 'A':
			flag = 1
			continue
		if i == 'A':
			s = max(s, c)
			c = 0
		else:
			c += 1
	print(s)


t = int(input())
for i in range(t):
	l = int(input())
	x = input()
	y = []
	pc = 0
	count = 0
	for j in range(len(x)):
		if x[j] == 'A':
			count += 1
			y.append(j)
	if count == 0:
		print(0)
	else:
		for j in range(len(y) - 1):
			d = y[j + 1] - y[j]
			if d > pc:
				pc = d
		la = len(x) - y[len(y) - 1]
		if la > pc:
			pc = la
		print(pc - 1)


t = int(input())
for _ in range(t):
	numstu = int(input())
	stu = input()
	maxc = 0
	cnt = 0
	for i in range(len(stu)):
		if stu[i:i + 1] == 'A' and i != len(stu) - 1:
			j = i + 1
			while stu[j:j + 1] == 'P' and j != len(stu):
				cnt += 1
				j = j + 1
			if cnt > maxc:
				maxc = cnt
			cnt = 0
	print(maxc)


n = int(input())
for i in range(n):
	q = int(input())
	s = input()
	count = 0
	while True:
		s1 = s
		s2 = s
		for j in range(len(s)):
			if j != len(s) - 1 and s[j] == 'A':
				s2 = s2[0:j + 1] + 'A' + s2[j + 2:len(s)]
		s = s2
		if s1 == s:
			print(count)
			break
		else:
			count += 1


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	cur = 0
	mx = 0
	f = False
	for i in range(0, n):
		if f == True and s[i] == 'P':
			cur += 1
			mx = max(cur, mx)
		if s[i] == 'A':
			cur = 0
			f = True
	print(mx)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	a = list(s)
	count = 0
	if 'A' in s:
		idx = s.index('A')
	else:
		idx = len(s)
	while s.count('P') > idx:
		for i in range(k):
			if i + 1 < k and s[i] == 'A' and (s[i + 1] == 'P'):
				a[i + 1] = 'A'
		count += 1
		s = ''.join(a)
	print(count)


num = int(input())
arr = []
for i in range(num):
	input()
	arr.append(input())

def largest(arr):
	ind = 0
	for i in range(len(arr)):
		if arr[i] == 'A':
			ind = i
			break
	if i == len(arr) - 1:
		return 0
	curr = 0
	maxseq = 0
	for i in range(ind, len(arr)):
		if arr[i] == 'P':
			curr += 1
			if i == len(arr) - 1:
				if curr > maxseq:
					maxseq = curr
		else:
			if curr > maxseq:
				maxseq = curr
			curr = 0
	return maxseq
for i in range(len(arr)):
	print(largest(arr[i]))


t = int(input())
ans = []
for i in range(t):
	input()
	x = input()
	s = 0
	while 'AP' in x:
		x = x.replace('AP', 'AA')
		s += 1
	ans.append(s)
[print(i) for i in ans]


def nex(s):
	l = len(s)
	t = s.copy()
	for i in range(l):
		if i == 0:
			t[0] = s[0]
		elif s[i - 1] == 'A':
			t[i] = 'A'
	return t
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	c = 0
	while nex(s) != s:
		s = nex(s)
		c += 1
	print(c)


for _ in range(int(input())):
	i1 = int(input())
	ar = list(input())
	flag = False
	count = 0
	ar1 = [0]
	for i in ar:
		if i == 'A' and (not flag):
			flag = True
		if i == 'P' and flag:
			count += 1
		elif i == 'A' and flag:
			ar1.append(count)
			count = 0
		ar1.append(count)
	print(max(ar1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	a = a + 'A'
	ar = []
	for i in range(n + 1):
		if a[i] == 'A':
			ar.append(i)
	d = 0
	for i in range(1, len(ar)):
		d = max(d, ar[i] - ar[i - 1] - 1)
	print(d)


n = input()
lens = []
string = []
kq = []
for i in range(int(n)):
	lens.append(input())
	string.append(input())
	kq.append(0)
for i in range(int(n)):
	(temp, flag) = (0, 0)
	for k in range(int(lens[i])):
		if string[i][k] == 'A':
			if temp > kq[i] and flag > 0:
				kq[i] = temp
			flag += 1
			temp = 0
		else:
			temp += 1
	if temp > kq[i] and flag >= 1:
		kq[i] = temp
for i in range(int(n)):
	print(kq[i])


t = int(input())
while t:
	t = t - 1
	k = int(input())
	chars = input()
	maxp = 0
	p = 0
	flag = 0
	for i in range(k):
		if flag == 0:
			if chars[i] == 'A':
				flag = 1
			continue
		if chars[i] == 'A':
			maxp = max(p, maxp)
			p = 0
		else:
			p = p + 1
	maxp = max(p, maxp)
	print(maxp)


from re import findall
t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	if s.count('AP'):
		s = findall('AP+', s[s.index('A'):])
		if s == []:
			print(0)
		else:
			print(max(s, key=lambda x: x.count('P')).count('P'))
	else:
		print(0)


for i in [0] * int(input()):
	n = int(input())
	auff = False
	(k, max_k) = (0, 0)
	for j in input():
		if j == 'A':
			auff = True
			max_k = max(max_k, k)
			k = 0
		elif auff == True:
			k += 1
	print(max(max_k, k))


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	count = 0
	f = 0
	if n == 1:
		print(count)
		continue
	c = 0
	for j in range(n - 1, -1, -1):
		if a[j] == 'P':
			c += 1
		elif a[j] == 'A' and c > count:
			count = c
			c = 0
		else:
			c = 0
	print(count)


import copy
for _ in range(int(input())):
	am = int(input())
	pers = input()
	pers = [char for char in pers]
	its = 0
	while True:
		fl = False
		newPers = copy.copy(pers)
		for i in range(am - 1):
			if pers[i] == 'A' and pers[i + 1] == 'P':
				newPers[i + 1] = 'A'
				fl = True
		if fl:
			its += 1
			pers = newPers
		else:
			print(its)
			break


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	(count, maxi, A) = (0, 0, [])
	f = 0
	for i in range(n):
		if s[i] == 'A':
			f = 1
			count = 0
		if f == 1 and s[i] == 'P':
			count += 1
			if maxi < count:
				maxi = count
	print(maxi)
	t -= 1


def Input():
	tem = input().split()
	ans = []
	for it in tem:
		ans.append(int(it))
	return ans
T = Input()[0]
for t in range(T):
	n = Input()[0]
	s = input()
	i = 0
	while i < n and s[i] == 'P':
		i += 1
	if i == n:
		print(0)
		continue
	MAX = 0
	tem = 0
	while i < n:
		if s[i] == 'A':
			MAX = max(MAX, tem)
			tem = 0
		else:
			tem += 1
		i += 1
	MAX = max(MAX, tem)
	print(MAX)


t = int(input())
for j in range(t):
	k = int(input())
	s = input()
	ls = [i for i in range(k) if s[i] == 'A']
	if len(ls) > 1:
		ls1 = [abs(ls[i] - ls[i - 1]) for i in range(1, len(ls))]
		maxDif = max(ls1)
		print(max([maxDif - 1, k - ls[-1] - 1]))
	elif len(ls) == 1:
		print(k - ls[0] - 1)
	else:
		print(0)


t = int(input())
for i in range(t):
	k = int(input())
	list1 = list(input())
	carray = []
	count = 0
	carray.append(count)
	for j in range(k - 1):
		if list1[j] == 'A':
			s = j + 1
			while s <= k - 1:
				if list1[s] == 'A':
					break
				else:
					count += 1
					s += 1
			carray.append(count)
			count = 0
	print(max(carray))


t = int(input())
while t:
	count = 0
	n = int(input())
	s = input()
	k = -1
	ans = 0
	for i in range(n):
		if s[i] == 'P' and k > -1:
			ans = max(ans, i - k)
		elif s[i] == 'A':
			k = i
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = list(s)
	if 'A' in s:
		k = s.index('A')
	else:
		k = n
	cnt = 0
	while s.count('A') != n - k:
		i = 0
		while i < n - 1:
			if s[i] == 'A':
				if s[i + 1] == 'P':
					s[i + 1] = 'A'
					i += 2
				else:
					i += 1
			else:
				i += 1
		cnt += 1
	print(cnt)


t = int(input())
for i in range(t):
	l = int(input())
	s = input()
	ans = 0
	i1 = s.find('A')
	if i1 >= 0:
		i2 = s.find('A', i1)
		while i2 >= 0:
			if i2 - i1 - 1 > ans:
				ans = i2 - i1 - 1
			(i1, i2) = (i2, s.find('A', i2 + 1))
		if l - i1 - 1 > ans:
			ans = l - i1 - 1
	print(ans)


n = int(input())
while n:
	j = input()
	a = input()
	(ans, res, temp) = (0, 0, 0)
	for i in a:
		if i == 'A' and (not temp):
			temp = 1
		elif i == 'A' and temp:
			ans = max(ans, res)
			res = 0
		elif i != 'A' and temp:
			res += 1
	ans = max(ans, res)
	print(ans)
	n -= 1


t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	i = 0
	ma = 0
	while i < n and s[i] == 'P':
		i += 1
	while i < n:
		while i < n and s[i] == 'A':
			i += 1
		if i < n:
			j = i
			while j < n and s[j] == 'P':
				j += 1
			ma = max(ma, j - i)
			i = j
	print(ma)


t = int(input())
for case in range(t):
	n_s = int(input())
	string = input()
	ai = -1
	c = 0
	for i in range(len(string)):
		if string[i] == 'A':
			if ai == -1:
				c = 0
				ai = i
			else:
				c = max(c, i - ai - 1)
				ai = i
	if ai == -1:
		print(0)
	else:
		c = max(c, len(string) - 1 - ai)
		print(c)


t = int(input())
while t:
	t = t - 1
	k = int(input())
	s = input()
	print(max(0, 0, *map(len, s.split('A')[1:])))


for _ in range(int(input())):
	n = int(input())
	s = input()
	mins = 0
	temp = 'AP'
	for i in range(len(s)):
		if temp in s:
			mins += 1
			s = s.replace(temp, 'AA')
	print(mins)


def main():
	N = int(input())
	for _ in range(N):
		L = int(input())
		S = str(input())
		res = 0
		for i in range(L - 1):
			if S[i] == 'A':
				tmp = 0
				i += 1
				while i < L and S[i] == 'P':
					tmp += 1
					i += 1
				res = max(res, tmp)
		print(res)
main()


t = int(input())
for case in range(t):
	L = int(input())
	s = input()
	Aind = L
	Pind = 0
	most = 0
	for i in range(L):
		if s[i] == 'A':
			Aind = i
		if s[i] == 'P':
			Pind = i
		most = max(most, Pind - Aind)
	print(most)


for i in range(int(input())):
	l = int(input())
	str = list(input())
	dist = 0
	distMax = 0
	index = 0
	for j in range(l):
		if str[j] == 'A':
			index = 1
			dist = 0
		elif index and str[j] == 'P':
			dist += 1
		if dist > distMax:
			distMax = dist
	print(distMax)


n = int(input())
for i in range(n):
	temp = int(input())
	a = input()
	ans = 0
	c = -9999999999999
	for i in a:
		if i == 'A':
			ans = max(ans, c)
			c = 0
		else:
			c += 1
	print(max(ans, c))


import math
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	ct = 0
	mydic = {}
	for j in range(len(s) - 1):
		if s[j] == 'A':
			k = j + 1
			while s[k] == 'P':
				k += 1
				if k > len(s) - 1:
					break
			mydic[j] = k - j - 1
	ans = 0
	for j in mydic:
		ans = max(ans, mydic[j])
	print(ans)


for _ in range(int(input())):
	x = int(input())
	s = input()
	som = 0
	cnt = 0
	i = 1
	while i < len(s):
		if s[i] == 'P' and s[i - 1] == 'A':
			while i < len(s) and s[i] != 'A':
				cnt = cnt + 1
				i = i + 1
		elif s[i] == 'A' and s[i - 1] == 'A':
			cnt = 0
			i = i + 1
		elif s[i] == 'P' and s[i - 1] == 'P':
			cnt = 0
			i = i + 1
		elif s[i] == 'A' and s[i - 1] == 'P':
			cnt = 0
			i = i + 1
		if cnt > som:
			som = cnt
	print(som)


T = int(input())
for _ in range(T):
	n = int(input())
	line = input()
	(max_time, last) = (0, n)
	for (i, c) in enumerate(line):
		if c == 'A':
			last = i
		else:
			max_time = max(max_time, i - last)
	print(max_time)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()[:k]
	p = s.split('A')
	if len(p) <= 1:
		print(0)
		continue
	print(max(map(len, p[1:])))


for i in range(int(input())):
	a = int(input())
	b = input()
	l = []
	for j in range(a):
		if b[j] == 'A':
			l.append(j + 1)
	l.append(a)
	m = []
	m.append(0)
	for j in range(1, len(l)):
		if j == len(l) - 1:
			m.append(l[j] - l[j - 1])
		else:
			m.append(l[j] - l[j - 1] - 1)
	print(max(m))


t = int(input())
for _ in range(t):
	l = int(input())
	s = str(input())
	if s.count('A') == 0 or s.count('P') == 0:
		print(0)
		continue
	big = c = 0
	pre = 0
	for i in range(-1, -l - 1, -1):
		if s[i] == 'A':
			c = i - pre + 1
			pre = i
			if c < big:
				big = c
	print(-big)


for t in range(int(input())):
	(k, s, v) = (int(input()), input() + 'A', 0)
	i = s.find('AP')
	while i != -1:
		j = s.find('A', i + 1)
		v = max(v, j - i - 1)
		i = s.find('AP', j)
	print(v)


import re

def solve(S):
	pattern = '(A{1}P+)'
	subs = re.findall(pattern, S)
	mx = 0
	for p in subs:
		mx = max(mx, len(p) - 1)
	return mx
T = int(input())
res = []
for _ in range(T):
	_ = input()
	S = input().strip()
	res.append(solve(S))
for r in res:
	print(r)


t = int(input())
for _ in range(t):
	k = int(input())
	s = list(input())
	time = 0
	change = True
	while change:
		change = False
		time_end = time
		for i in range(k - 2, -1, -1):
			if s[i] == 'A' and s[i + 1] == 'P':
				if time_end == time:
					time += 1
				change = True
				s[i + 1] = 'A'
	print(time)


for i in range(int(input())):
	k = int(input())
	s = input()
	num = 0
	firsta = -1
	for c in range(len(s)):
		if s[c] == 'A':
			firsta = c
			break
	else:
		print(0)
	if firsta != -1:
		for c in range(firsta + 1, len(s)):
			if s[c] == 'A':
				now = c - firsta - 1
				num = max(now, num)
				firsta = c
		num = max(num, len(s) - firsta - 1)
		print(num)


from sys import stdin, stdout
import gc
gc.disable()
import array

def check(index):
	c = 0
	for i in range(index, n):
		if string[i] == 'P':
			c += 1
		else:
			break
	return c
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	string = stdin.readline().strip()
	count = 0
	for i in range(n - 1, -1, -1):
		if string[i] == 'A':
			count = max(count, check(i + 1))
	print(count)


def getTests():
	qtTests = int(input())
	tests = []
	for test in range(qtTests):
		testSize = int(input())
		tests.append(input())
	return tests

def printAnswers(answers):
	for answer in answers:
		print(answer)

def fmax(val1, val2):
	return val1 if val1 > val2 else val2

def getAnswers(tests):
	answers = []
	angryStudentChr = 'A'
	for test in tests:
		maxValue = 0
		testSize = len(test)
		previousAngry = -1
		index = 0
		for testChr in reversed(test):
			if testChr == angryStudentChr:
				if previousAngry == -1:
					maxValue = index
				else:
					maxValue = fmax(maxValue, index - previousAngry)
				previousAngry = index + 1
			index += 1
		answers.append(maxValue)
	return answers
tests = getTests()
answers = getAnswers(tests)
printAnswers(answers)


t = int(input())
for i in range(t):
	k = int(input())
	s = input().split('A')[1:]
	s.sort()
	s.reverse()
	try:
		print(len(s[0]))
	except:
		print(0)


for _ in range(int(input())):
	n = int(input())
	k = input()
	if 'A' in k:
		h = k.split('A')
		l = [len(h[i]) for i in range(1, len(h))]
		print(max(l))
	else:
		print(0)


t1 = int(input())
for _ in range(t1):
	n = int(input())
	p = 0
	o = 0
	s = str(input())
	b = []
	for i in s:
		if i == 'A':
			o = 1
		if o == 1:
			if i == 'A':
				b.append(p)
				p = 0
			else:
				p += 1
	b.append(p)
	print(max(b))


def main():
	for _ in range(int(input())):
		n = int(input())
		ap = 0
		c = 0
		s = input()
		res = 0
		for w in s:
			if w is 'A':
				c = 1
				res = max(res, ap)
				ap = 0
			elif w is 'P' and c == 1:
				ap += 1
		print(max(res, ap))
main()


for i in range(int(input())):
	n = int(input())
	s = input()
	(c, ans) = (0, 0)
	cnt = 0
	for i in range(n):
		if s[i] == 'A':
			c = 0
			cnt = 1
		elif s[i] == 'P' and cnt == 1:
			c += 1
		ans = max(ans, c)
	print(ans)


import re
for _ in range(int(input())):
	n = int(input())
	s = input()
	(f, c, m) = (0, 0, 0)
	for i in re.finditer('AP+', s):
		c = i.end() - i.start() - 1
		if c > m:
			m = c
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	maxDistance = 0
	current = None
	for i in range(n):
		if s[i] == 'A':
			current = 0
		elif current != None:
			current += 1
			maxDistance = max(current, maxDistance)
	print(maxDistance)


def nextInt():
	return int(input())

def nextString():
	return input()

def nextIntArray(n):
	lst = []
	for i in range(n):
		lst.append(int(input()))
	return lst

def nextStringArray(n):
	lst = []
	for i in range(n):
		lst.append(input())
	return lst
tt = nextInt()
for _ in range(tt):
	N = nextInt()
	s = nextString()
	s = s.split('A')
	try:
		s = len(max(s[1:], key=len))
	except:
		s = 0
	print(s)


for i in range(int(input())):
	k = int(input())
	s = input()
	v = []
	t = 0
	h = 0
	for i in range(k):
		if s[i] == 'A':
			v.append(t)
			t = 0
			h = 1
		elif h:
			t += 1
	v.append(t)
	print(max(v))


t = int(input())
for test in range(t):
	n = int(input())
	s = input()
	count = 0
	arr = []
	for i in range(n):
		if s[i] == 'A':
			arr.append(i + 1)
	arr.append(n + 1)
	if len(arr) == 1:
		print(0)
	else:
		res = 0
		for i in range(1, len(arr)):
			res = max(res, arr[i] - arr[i - 1] - 1)
		print(res)


t = int(input())
r = []
while t > 0:
	l = int(input())
	s = input()
	A = 0
	c = 0
	m = 0
	for i in s:
		if i == 'A':
			A = 1
			c = 0
		elif A == 1 and i == 'P':
			c += 1
		if c > m:
			m = c
	r.append(m)
	t -= 1
for i in r:
	print(i)


import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip() + 'A'
	ans = 0
	cnt = 0
	initialized = False
	for ch in s:
		if ch == 'A':
			ans = max(ans, cnt)
			cnt = 0
			initialized = True
		elif initialized:
			cnt += 1
	print(ans)


t = int(input())
for i in range(t):
	maxi = 0
	aux = 0
	n = int(input())
	s = input()
	for i in range(n):
		if s[i] == 'A':
			aux = 0
			i += 1
			if i < n:
				while s[i] == 'P':
					aux += 1
					i += 1
					if i == n:
						break
			maxi = max(maxi, aux)
	print(maxi)


num_t = int(input())
for t in range(num_t):
	_ = input()
	s = input().strip()
	i = 0
	while i < len(s) and s[i] == 'P':
		i += 1
	max_r = 0
	r = 0
	while i < len(s):
		if s[i] == 'P':
			r += 1
		else:
			max_r = max(r, max_r)
			r = 0
		i += 1
	max_r = max(r, max_r)
	print(max_r)


t = int(input())
for i in range(0, t):
	x = int(input())
	line = input()
	temp = line[0]
	line = line.split('A')
	if temp == 'P':
		line = line[1:]
	total = 0
	for ii in range(0, len(line)):
		temp = len(line[ii])
		if temp > total:
			total = temp
	print(total)


for t in range(int(input())):
	n = int(input())
	s = input()
	cnt = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		cnt += 1
	print(cnt)


from array import array
from sys import stdin
import bisect
from itertools import *

def scan_gen():
	for line in stdin:
		yield from iter(line.split())
scan = scan_gen()

def nint():
	return int(next(scan))

def nintk(k):
	return tuple((nint() for _ in range(k)))

def nfloat():
	return float(next(scan))

def intar_init(size):
	return array('i', [0]) * size

def intar(size=None):
	if size == None:
		size = nint()
	arr = intar_init(size)
	for x in range(size):
		arr[x] = nint()
	return arr

def solve():
	t = nint()
	for _ in range(t):
		k = nint()
		S = next(scan)
		res = 0
		cur = 0
		S += 'A'
		while len(S) > 0 and S[0] == 'P':
			S = S[1:]
		for i in range(len(S)):
			if S[i] == 'P':
				cur += 1
				res = max(res, cur)
			else:
				cur = 0
		print(res)
solve()


t = int(input())
for y in range(t):
	n = int(input())
	a = input()
	i = a.count('A')
	if i == 0:
		print(0)
	else:
		ct = 0
		i = a.index('A')
		i += 1
		while i < n:
			st = i
			while i < n and a[i] != 'A':
				i += 1
			ct = max(ct, i - st)
			i += 1
		print(ct)


t = int(input())
for i in range(t):
	res = 0
	max_res = 0
	k = int(input())
	s = list(input())
	while True:
		if len(s) > 0:
			if s[0] == 'P':
				s.pop(0)
			else:
				res = 0
				while True:
					if len(s) > 1:
						if s[1] == 'P':
							s.pop(1)
							res += 1
						else:
							break
					else:
						break
				s.pop(0)
				if res > max_res:
					max_res = res
		else:
			break
	print(max_res)


n = int(input())
for i in range(n):
	s = int(input())
	a = input()
	c = 0
	max = 0
	if 'A' in a:
		h = a.index('A')
		for i in range(h + 1, s):
			if a[i] == 'A':
				c = 0
			else:
				c = c + 1
				if c > max:
					max = c
	print(max)


times = int(input())
arr = []
for i in range(times):
	k = int(input())
	arr.append(input())

def last_min(order):
	count = 0
	cp = list(order).copy()
	on = True
	while on:
		new = cp.copy()
		for i in range(len(cp)):
			if cp[i] == 'A':
				if i + 1 < len(cp):
					if cp[i + 1] == 'P':
						new[i + 1] = 'A'
		if new != cp:
			count += 1
			cp = new
		else:
			on = False
	print(count)
for i in arr:
	last_min(i)


for t in range(int(input())):
	k = int(input())
	s = input()
	if 'A' not in s:
		print(0)
		continue
	for i in range(k):
		if s[i] == 'A':
			s = s[i:]
			break
	ml = 0
	for i in range(1, len(s) + 1):
		if 'P' * i in s:
			ml = i
	print(ml)


t = int(input())
for q in range(t):
	n = int(input())
	s = input()
	c = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		c += 1
	print(c)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class union_find:

	def __init__(self, n):
		self.n = n
		self.rank = [0] * n
		self.parent = [int(j) for j in range(n)]

	def union(self, i, j):
		i = self.find(i)
		j = self.find(j)
		if self.rank[i] == self.rank[j]:
			self.parent[i] = j
			self.rank[j] += 1
		elif self.rank[i] > self.rank[j]:
			self.parent[j] = i
		else:
			self.parent[i] = j

	def find(self, i):
		temp = i
		if self.parent[temp] != temp:
			self.parent[temp] = self.find(self.parent[temp])
		return self.parent[temp]
from math import log2, ceil

def main():
	for t in range(int(input())):
		n = int(input())
		s = input()
		tmp = -1e+18
		ans = 0
		for i in range(n):
			if s[i] == 'P':
				tmp += 1
			else:
				ans = max(tmp, ans)
				tmp = 0
		ans = max(ans, tmp)
		print(ans)
main()


for ele in range(int(input())):
	t = int(input())
	n = input().lstrip('P').split('A')
	print(len(max(n)))


for i in range(int(input())):
	n = int(input())
	k = input()
	p = 0
	for j in range(n):
		if 'AP' in k:
			y = k.replace('AP', 'AA')
			k = y
			p = p + 1
		else:
			print(p)
			break


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	ans = []
	for _ in range(n):
		k = I()
		s = list(S())
		turn = 0
		while True:
			f = True
			for i in range(k - 1)[::-1]:
				if s[i] == 'A' and s[i + 1] == 'P':
					s[i + 1] = 'A'
					f = False
			if f:
				break
			turn += 1
		ans.append(turn)
	for x in ans:
		print(x)
main()


t = int(input())
i = 0
while i < t:
	n = int(input())
	s = input()
	res = 0
	count = 0
	r = False
	for x in s:
		if x == 'P' and r == False:
			pass
		elif x == 'P' and r == True:
			count = count + 1
		elif x == 'A':
			r = True
			res = max(res, count)
			count = 0
	res = max(res, count)
	print(res)
	i = i + 1


tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(input())
	new_arr = ['P'] * n
	count = 0
	while True:
		for i in range(n - 1):
			if arr[i] == 'A':
				new_arr[i] = 'A'
				new_arr[i + 1] = 'A'
		count += 1
		if arr[-1] == 'A':
			new_arr[-1] = 'A'
		if arr == new_arr:
			break
		arr = new_arr
		new_arr = ['P'] * n
	print(count - 1)


n = int(input())
for i in range(n):
	p = int(input())
	s = input()
	ans = 0
	tmpans = 0
	flag = False
	for j in range(len(s)):
		if j < len(s) - 1:
			if s[j] == 'A':
				if ans < tmpans:
					ans = tmpans
				tmpans = 0
				flag = True
			elif flag:
				tmpans += 1
		elif s[j] == 'P':
			if flag:
				tmpans += 1
			if ans < tmpans:
				ans = tmpans
			tmpans = 0
		else:
			if ans < tmpans:
				ans = tmpans
			tmpans = 0
	print(ans)


for _ in '.' * int(input()):
	x = int(input())
	s = input()
	if 'A' not in s:
		print(0)
	else:
		(S, M) = (0, 0)
		s = s[s.index('A'):]
		for i in range(len(s)):
			if s[i] == 'P':
				S += 1
			else:
				M = max(S, M)
				S = 0
		M = max(S, M)
		print(M)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input().strip())
	ans = 0
	marked = []
	for i in range(n):
		if s[i] == 'A':
			marked.append(i)
	s.append('A')
	flag = True
	while flag:
		ans += 1
		flag = False
		for i in marked:
			if s[min(i + ans, n)] == 'P':
				s[min(i + ans, n)] = 'A'
				flag = True
	print(ans - 1)


for _ in range(int(input())):
	(y, x) = (int(input()), input())
	diff = 0
	if x.count('A') != 0:
		c = x.index('A')
		for i in range(y):
			if x[i] == 'A':
				diff = max(diff, i - c)
				c = i
		print(max(diff - 1, y - c - 1))
	else:
		print(0)


for i in range(int(input())):
	n = int(input())
	s = input()
	if 'A' in s:
		x = s.index('A')
		ans = 0
		p = 0
		for j in s[x + 1:]:
			if j == 'P':
				p = p + 1
			else:
				if p > ans:
					ans = p
				p = 0
		if p > ans:
			ans = p
		print(ans)
	else:
		print(0)


from collections import deque
import itertools
import operator
import queue
import time
import math
import sys
import os
t = int(input())
for k in range(t):
	n = int(input())
	s = str(input())
	cnt = 0
	ind = -1
	for i in range(n):
		if s[i] == 'A':
			ind = i
			break
	if ind == n:
		print(0)
		continue
	else:
		c = 0
		mx = 0
		for i in range(ind, n):
			if s[i] == 'P':
				c += 1
			else:
				mx = max(mx, c)
				c = 0
		mx = max(mx, c)
		if ind != -1:
			print(mx)
		else:
			print(0)


n = int(input())
for i in range(n):
	k = int(input())
	line = input() + 'A'
	(foundA, lastA, maxMarche) = (False, 0, -9999)
	for i in range(len(line)):
		if line[i] == 'A':
			if not foundA:
				foundA = True
				lastA = i
			else:
				maxMarche = max(maxMarche, i - lastA - 1)
				lastA = i
	print(max(0, maxMarche))


t = int(input())
for _ in range(t):
	k = int(input())
	s = str(input())
	s = s.lstrip('P')
	sp = s.split('A')
	print(max(map(len, s.split('A'))))


test_cases = int(input())
for _ in range(test_cases):
	student_no = int(input())
	split_value = input().split('A')
	if len(split_value) < 2:
		print(0)
	else:
		print(len(max(split_value[1:])))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	mx_dist = 0
	flag = 0
	for j in range(n):
		if a[j] == 'A':
			flag = 1
			break
	if flag:
		for i in range(j, n):
			if a[i] == 'A':
				pos_a = i
			else:
				mx_dist = max(mx_dist, abs(i - pos_a))
	if not flag:
		print(0)
	else:
		print(mx_dist)


import sys
import math
import itertools
import functools
import collections
import operator

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
t = ii()
for _ in range(t):
	n = int(input())
	s = input()
	max_ = 0
	if 'A' in s:
		pos = s.index('A')
		for j in range(100):
			if s[pos:].find('P' * j) != -1:
				max_ = max(max_, j)
	print(max_)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' not in s:
		print(0)
	else:
		c = 0
		l1 = []
		for j in range(n):
			if s[j] == 'A':
				c = j
				break
		l = s[j:].split('A')
		for i in range(len(l)):
			l1.append(len(l[i]))
		print(max(l1))


t = int(input())
for i in range(t):
	m = 0
	c = 0
	n = int(input())
	s = input()
	if 'A' in s:
		for j in range(s.index('A'), n):
			if s[j] == 'A':
				c = 0
			else:
				c += 1
				if c > m:
					m = c
	print(m)


v = int(input())
for i in range(v):
	f = int(input())
	s = 'A' + str(input())
	l = list(s.split('A'))[2:]
	if len(l) == 0:
		print('0')
	else:
		print(len(max(l, key=len)))


a = int(input())
mas = []
for i in range(a):
	b = int(input())
	st = input()
	mn = []
	for e in range(len(st)):
		if st[e] == 'A':
			mn.append(e)
	mn.append(len(st))
	r = 0
	for x in range(len(mn) - 1):
		if mn[x + 1] - mn[x] - 1 > r:
			r = mn[x + 1] - mn[x] - 1
	mas.append(r)
for i in range(len(mas)):
	print(mas[i])


n = int(input())
a = []
for i in range(n):
	k = int(input())
	s = input() + 'yo'
	m = 0
	x = 0
	if 'A' in s:
		z = s.index('A') + 1
		for i in range(z, len(s)):
			if s[i] == 'P':
				x += 1
			else:
				m = max(m, x)
				x = 0
	a += [m]
for i in range(len(a)):
	print(a[i])


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	t = 0
	while True:
		k = s.replace('AP', 'AA')
		if s != k:
			s = k
			t += 1
		else:
			break
	print(t)


import sys

def check(students, k):
	for i in range(k - 1):
		if students[i] == 'A' and students[i + 1] == 'P':
			return True
	return False

def solution(students, k):
	res = 0
	flag = 0
	students = list(students)
	while check(students, k):
		i = 0
		while i < k - 1:
			if students[i] == 'A' and students[i + 1] == 'P':
				flag = 1
				students[i + 1] = 'A'
				i += 2
			else:
				i += 1
		if flag:
			res += 1
		flag = 0
	return res
t = int(input())
for _ in range(t):
	k = int(input())
	students = input()
	print(solution(students, k))


t = int(input())
for i in range(t):
	n = int(input())
	k = input().split('A')
	if len(k) == 1:
		print(0)
	else:
		print(len(sorted(k[1:])[-1]))


t = int(input())
for i in range(t):
	length = int(input())
	s = input()
	s = [i for i in s]
	time = -1

	def ind_A(s):
		list_ind = []
		for i in range(len(s)):
			if s[i] == 'A':
				list_ind.append(i)
		return list_ind
	prev_s = ''
	while prev_s != ''.join(s):
		prev_s = ''.join(s)
		for i in ind_A(s):
			if i < length - 1:
				s[i + 1] = 'A'
		time += 1
	print(time)


ttt = int(input())
for cas in range(ttt):
	n = int(input())
	s = input()
	ma = 0
	last = n
	for i in range(n):
		if s[i] == 'A':
			ma = max(ma, i - last - 1)
			last = i
	print(max(ma, n - last - 1))


q = int(input())
for _ in range(q):
	input()
	s = input()
	i = 0
	while i < len(s) and s[i] == 'P':
		i += 1
	ans = 0
	cnt = 0
	for j in range(i, len(s)):
		if s[j] == 'A':
			ans = max(ans, cnt)
			cnt = 0
		else:
			cnt += 1
	print(max(ans, cnt))


t = int(input())
for i in range(t):
	s = int(input())
	a = input()
	e = 0
	z = -1
	while z != 0:
		b = a[0]
		z = 0
		for i in range(1, len(a)):
			if a[i] == 'P' and a[i - 1] == 'A':
				b += 'A'
				z += 2
			else:
				b += a[i]
		a = b
		b = ''
		e += 1
	print(e - 1)


t = int(input())
m = []
n = []
i = 0
k = 0
x = 0
for f in range(0, t):
	m = int(input())
	n = input()
	i = 0
	x = 0
	k = 0
	for z in range(0, m):
		if n[z] == 'P' and x == 1:
			i += 1
		elif n[z] == 'A':
			x = 1
			i = 0
		if i > k:
			k = i
	print(k)


for _ in range(int(input())):
	n = int(input())
	a = input()
	(open, cnt, ans) = (0, 0, 0)
	for i in a:
		if i == 'A':
			open = 1
		if open:
			if i == 'P':
				cnt += 1
			else:
				ans = max(ans, cnt)
				cnt = 0
	ans = max(ans, cnt)
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	dis = 0
	prev = -1
	s = s + 'A'
	for i in range(0, len(s), 1):
		if prev == -1 and s[i] == 'A':
			prev = i
		elif s[i] == 'A':
			dis = max(dis, i - prev - 1)
			prev = i
	print(dis)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ans = 0
	for i in range(len(s)):
		if s[i] == 'P':
			continue
		ok = 0
		for j in range(i + 1, len(s)):
			if s[j] == 'A':
				ans = max(ans, j - 1 - i)
				ok = 1
				break
		if ok == 0:
			ans = max(ans, len(s) - 1 - i)
	print(ans)


t = int(input())
while t:
	k = int(input())
	s = input()
	i = 0
	while i < k and s[i] == 'P':
		i += 1
	max_len = 0
	while i < k:
		curr_len = 0
		while i < k and s[i] == 'P':
			curr_len += 1
			i += 1
		max_len = max(curr_len, max_len)
		i += 1
	print(max_len)
	t -= 1


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	count = 0
	ok = False
	maxi = 0
	angry = False
	for i in range(n):
		if s[i] == 'P' and angry == True:
			if ok:
				count += 1
			else:
				ok = True
				count = 1
		else:
			maxi = max(count, maxi)
			ok = False
			if s[i] == 'A':
				angry = True
	maxi = max(count, maxi)
	print(maxi)


t = int(input())
while t > 0:
	k = int(input())
	s = input()
	ans = 0
	for i in range(len(s)):
		maxAns = 0
		if s[i] == 'A':
			for j in range(i + 1, len(s)):
				if s[j] == 'P':
					maxAns += 1
				else:
					break
		ans = max(ans, maxAns)
	print(ans)
	t -= 1


t = int(input())
while t > 0:
	n = int(input())
	k = input().split('A')
	if len(k) == 1:
		print(0)
	else:
		print(len(sorted(k[1:])[-1]))
	t -= 1


n = int(input())
ans = []
for i in range(n):
	studients_num = int(input())
	line = input()
	(max_t, cur_t) = (0, 0)
	angry_in = False
	for i in line:
		if i == 'A':
			max_t = max(cur_t, max_t)
			cur_t = 0
			angry_in = True
		elif angry_in:
			cur_t += 1
	ans.append(max(max_t, cur_t))
for i in ans:
	print(i)


import math
import sys

def listIntInput():
	return list(map(int, input().split(' ')))

def listStrInput():
	return input().split(' ')

def main():
	answer = ''
	for test in range(int(input())):
		n = int(input())
		group = input()
		longest = 0
		length = 0
		angry_found = False
		for stud in group:
			if not angry_found:
				if stud == 'P':
					pass
				else:
					angry_found = True
			elif stud == 'P':
				length += 1
				longest = max(longest, length)
			else:
				length = 0
		answer += str(longest) + '\n'
		pass
	print(answer)
main()


for _ in range(int(input())):
	n = int(input())
	a = input().split('A')
	c = 0
	for i in range(1, len(a)):
		c = max(c, len(a[i]))
	print(c)


n = int(input())
for i in range(n):
	_ = input()
	s = input()
	res = 0
	temp = 0
	has_a = False
	for c in s:
		if c == 'P' and has_a:
			temp += 1
			if temp > res:
				res = temp
		if c == 'A':
			has_a = True
			temp = 0
	print(res)


groups = int(input())
for i in range(groups):
	kolich = int(input())
	peop = input()
	time = 0
	skotch = 0
	h = 0
	for j in peop:
		if j == 'A':
			h = 1
			if time < skotch:
				time = skotch
			skotch = 0
		if j == 'P' and h == 1:
			skotch += 1
	if time < skotch:
		print(skotch)
	else:
		print(time)


t = int(input())
for _ in range(t):
	n = int(input())
	c = input()
	mp = 0
	i = 0
	while i < n:
		if c[i] == 'A':
			p = 0
			i += 1
			while i < n and c[i] == 'P':
				p += 1
				i += 1
			if p > mp:
				mp = p
		else:
			i += 1
	print(mp)


amount_groups = int(input())
for group in range(amount_groups):
	amount_pupils = int(input())
	line = list(input())
	cur_streak = 0
	max_streak = 0
	first_a = False
	for pupil in line:
		if pupil == 'A':
			cur_streak = 0
			first_a = True
		elif first_a:
			cur_streak += 1
			max_streak = max(max_streak, cur_streak)
	print(max_streak)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	p = 0
	coun = 0
	counmax = 0
	c = 0
	while p == 0 and c < k:
		if s[c] == 'A':
			p = 1
		c += 1
	for i in range(c, k):
		if s[i] == 'P':
			coun += 1
		else:
			if coun > counmax:
				counmax = coun
			coun = 0
	if coun > counmax:
		counmax = coun
	print(counmax)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' in s:
		q = set(s[s.index('A') + 1:].split('A'))
		m = 0
		for i in q:
			if len(i) > m:
				m = len(i)
		print(m)
	else:
		print(0)


k = []
for _ in range(int(input())):
	n = int(input())
	c = 0
	s = input('')
	while 'AP' in s:
		c += 1
		s = s.replace('AP', 'AA')
	k.append(c)
for i in k:
	print(i)


def solve(n, stds):
	an_index = -1
	p_cnt = 0
	max_p_cnt = 0
	for i in range(n):
		if stds[i] == 'A':
			an_index = i
			p_cnt = 0
		elif an_index != -1:
			p_cnt += 1
		max_p_cnt = max(p_cnt, max_p_cnt)
	return max_p_cnt
t = int(input())
for _ in range(t):
	n = int(input())
	stds = input()
	print(solve(n, stds))


test = int(input())
for i in range(test):
	counter = 0
	max_n = 0
	isA = False
	le = input()
	x = input()
	for ch in x:
		if ch == 'A':
			isA = True
			if counter > max_n:
				max_n = counter
			counter = 0
		elif isA:
			counter += 1
	if counter != 0:
		if counter > max_n:
			max_n = counter
	print(max_n)


for i in range(int(input())):
	k = int(input())
	s = input()
	cnt = 0
	max1 = 0
	k = 0
	for i in s:
		if i == 'P':
			if k == 1:
				cnt += 1
		else:
			k = 1
			if max1 < cnt:
				max1 = cnt
			cnt = 0
	if max1 < cnt:
		max1 = cnt
	print(max1)


num = input()
for i in range(int(num)):
	_ = input()
	listOfElems = input()
	indexPosList = [i for i in range(len(listOfElems)) if listOfElems[i] == 'A']
	max_diff = 0
	for (i, ele) in enumerate(indexPosList):
		if i != len(indexPosList) - 1:
			diff = indexPosList[i + 1] - 1 - ele
		else:
			diff = len(listOfElems) - 1 - ele
		if diff >= max_diff:
			max_diff = diff
	print(max_diff)


from sys import stdin
from collections import defaultdict as dd
t = int(stdin.readline().rstrip())
while t > 0:
	n = int(stdin.readline().rstrip())
	s = stdin.readline().rstrip()
	c = 0
	mc = 0
	an = 0
	for i in range(n):
		if s[i] == 'A':
			an = 1
			c = 0
		elif an == 1:
			c += 1
		mc = max(mc, c)
	print(mc)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	state = list(input())
	if state[-1] == 'A':
		state.pop()
	if state.count('A') == 0 or state.count('P') == 0:
		print(0)
		continue
	ptr = 0
	while state[ptr] == 'P':
		ptr += 1
	state = state[ptr:]
	max_ser = 0
	cur_ser = 0
	for i in range(len(state)):
		if state[i] == 'P':
			cur_ser += 1
		else:
			max_ser = max(max_ser, cur_ser)
			cur_ser = 0
	print(max(max_ser, cur_ser))


t = int(input())
for i in range(t):
	k = int(input())
	st = input()
	answer = 0
	i = k - 1
	while i >= 0:
		if st[i] == 'P':
			i = i - 1
			count = 1
			while i >= 0 and st[i] == 'P':
				count += 1
				i = i - 1
			if i >= 0:
				if count > answer:
					answer = count
		else:
			i = i - 1
	print(answer)


for _ in range(int(input())):
	l = int(input())
	sl = list(input().split('A'))
	count_list = []
	for i in sl:
		count_list.append(len(i))
	if count_list[0] != 0:
		count_list[0] = 0
	print(max(count_list))


t = int(input())
for i in range(0, t):
	n = int(input())
	s = input()
	f = n
	for j in range(0, n):
		if s[j] == 'A':
			f = j
			break
	if f < n - 1:
		nd = 0
		max1 = 0
		for j in range(f + 1, n):
			if s[j] == 'A':
				if max1 < nd:
					max1 = nd
				nd = 0
			elif j == n - 1 and s[j] == 'P':
				if max1 <= nd:
					max1 = nd + 1
			else:
				nd += 1
		print(max1)
	else:
		print(0)


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	s = input()
	maxi = 0
	temp = 0
	start = False
	for i in s:
		if i == 'A':
			start = True
			temp = 0
		if start and i == 'P':
			temp += 1
			if temp > maxi:
				maxi = temp
	print(maxi)


def solve():
	n = int(input())
	s = input()
	(cnt, ans) = (0, 0)
	for x in s[::-1]:
		if x == 'P':
			cnt += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
	print(ans)
t = int(input())
for _ in range(t):
	solve()


n = int(input())
for x in range(n):
	no = int(input())
	ang = input()
	ans = 0
	temp = 0
	now = 0
	x = 0
	for i in range(0, no):
		if x == 1:
			if ang[i] == 'P':
				now += 1
			else:
				x = 0
				ans = max(ans, now)
				now = 0
		if ang[i] == 'A':
			x = 1
	ans = max(ans, now)
	print(ans)


t = int(input())
while t:
	k = int(input())
	s = input()
	ls = [0]
	count = 0
	flag = False
	for i in range(k):
		if s[i] == 'A':
			flag = True
			ls.append(count)
			count = 0
		elif flag:
			count += 1
			if i == k - 1:
				ls.append(count)
	print(max(ls))
	t -= 1


def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		a = s.split('A')
		s = [len(e) for e in a[1:]]
		try:
			print(max(s))
		except:
			print(0)
main()


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	a = input()
	k = 0
	while 'AP' in a:
		a = a.replace('AP', 'AA')
		k += 1
	print(k)


test = int(input())
for _ in range(test):
	n = int(input())
	s = input()
	time = 0
	counter = 0
	if s == 'P' or s == 'A':
		print(0)
		continue
	for i in range(n - 1, -1, -1):
		if s[i] == 'P':
			counter += 1
		else:
			time = max(time, counter)
			counter = 0
	print(time)


def ab():
	c = int(input())
	b = list(input())
	r = []
	t = 0
	g = 0
	for i in range(0, len(b)):
		if b[i] == 'A':
			g = 1
			r.append(t)
			t = 0
		elif g == 1:
			t = t + 1
	r.append(t)
	print(max(r))
for i in range(0, int(input())):
	ab()


n = int(input())
for i in range(n):
	m = int(input())
	l = list(input())
	p = []
	c = 0
	r = len(l)
	for i in range(r):
		if l[i] == 'P':
			c += 1
			if i == r - 1:
				p.append(c)
		if l[i] == 'A':
			p.append(c)
			c = 0
	if len(p) != 1:
		if p[0] == 0:
			print(max(p))
		else:
			p.pop(0)
			print(max(p))
	else:
		print(0)


t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	a = 'A'
	p = 'P'
	flag = 0
	l = 0
	maxs = 0
	for i in range(n):
		if s[i] == a:
			flag = 1
		if s[i] == p and flag == 1:
			l += 1
		if s[i] == a or i == n - 1:
			if l > maxs:
				maxs = l
			l = 0
	print(maxs)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ind = []
	for i in range(n):
		if s[i] == 'A':
			ind.append(i)
	if len(ind) == 0:
		print(0)
	else:
		for j in range(len(ind) - 1):
			ind[j] = ind[j + 1] - ind[j] - 1
		ind[len(ind) - 1] = n - ind[len(ind) - 1] - 1
		print(max(ind))


no_of_test_cases = int(input())

def cal_time(input_str, len):
	current_count = 0
	max = 0
	start = 0
	end = len - 1
	found_a = False
	while start <= end:
		if input_str[start] == 'A':
			found_a = True
			if current_count > max:
				max = current_count
			current_count = 0
		elif found_a == True:
			current_count = current_count + 1
		start += 1
	if current_count > max:
		max = current_count
	return max
for x in range(1, no_of_test_cases + 1):
	length = int(input())
	input_str = input()
	print(cal_time(input_str, length))


testcases = int(input())
for _ in range(testcases):
	n = int(input())
	l = list(input())
	result = 0
	for i in range(n - 1):
		ans = 0
		if l[i] == 'A':
			for j in range(i + 1, n):
				if l[j] == 'P':
					ans += 1
				else:
					if result < ans:
						result = ans
					break
			if result < ans:
				result = ans
	print(result)


import re

def main():
	f = re.compile('AP+').findall
	for _ in range(int(input())):
		input()
		print(max(map(len, input().lstrip('P').replace('A', ' ').split()), default=0))
main()


for _ in range(int(input())):
	maxi = 0
	k = int(input())
	st = input()
	c = 0
	while c < k:
		if st[c] == 'A':
			c += 1
			m = 0
			while c < k and st[c] == 'P':
				m += 1
				c += 1
			maxi = max(maxi, m)
		else:
			c += 1
	print(maxi)


t = int(input())
for i in range(t):
	k = int(input())
	a = input()
	maxlen = 0
	cl = -1
	for i in a:
		if i == 'A':
			cl = 0
		elif cl != -1:
			cl += 1
			maxlen = max(cl, maxlen)
	print(max(maxlen, 0))


for m in range(int(input())):
	k = int(input())
	group = list(map(str, input()))
	time = 0
	prev_index = len(group)
	for i in range(k - 1, -1, -1):
		if group[i] == 'A':
			time = max(time, prev_index - 1 - i)
			prev_index = i
	print(time)


t = int(input())
for i in range(t):
	n = int(input()) + 1
	s = input() + 'A'
	max_dist = 0
	cur = 0
	start = max(0, s.find('A'))
	for i in range(n):
		if s[i] == 'A':
			cur = 0
		elif i > start:
			cur += 1
		max_dist = max(max_dist, cur)
	print(max_dist)


ee = int(input())
while ee > 0:
	ee = ee - 1
	n = int(input())
	s = input()
	cnt = int(0)
	flag = int(-1)
	ans = int(0)
	for i in range(0, n):
		if s[i] == 'A':
			flag = 0
			cnt = 0
		if s[i] == 'P':
			if flag == 0:
				cnt = cnt + 1
		if cnt > ans:
			ans = cnt
	print(ans)


for _ in range(int(input())):
	k = int(input())
	s = list(input())
	t = 0
	while True:
		f = 0
		i = 0
		while i < len(s) - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				if f == 0:
					t += 1
					f = 1
				i += 1
			i += 1
		if f == 0:
			break
	print(t)


for _ in range(int(input())):
	k = int(input())
	data = input()
	max_distance = 0
	count = 0
	start = False
	for i in data:
		if i == 'A':
			count = 0
			start = True
		elif start:
			count += 1
			max_distance = max(max_distance, count)
	print(max_distance)


t = int(int(input()))
while t:
	t -= 1
	k = int(input())
	s = input().strip()
	ind = 0
	count = 0
	for i in range(len(s)):
		if s[i] == 'A':
			ind = i
			count += 1
			break
	ans = 0
	tmp = 0
	if count == 1:
		for i in range(ind, len(s)):
			if s[i] == 'A':
				tmp = 0
			else:
				tmp += 1
			ans = max(ans, tmp)
		print(ans)
	else:
		print(0)


def solution(s):
	N = 0
	s = s.lstrip('P')
	while True:
		s = s.rstrip('A')
		slist = list(s)
		if not slist:
			return N
		tlist = [slist[0]]
		for i in range(len(slist) - 1):
			if slist[i] == 'A':
				tlist.append('A')
			else:
				tlist.append(slist[i + 1])
		s = ''.join(tlist)
		s = s.lstrip('A')
		if s:
			s = 'A' + s
		N += 1
	return N
for _ in range(int(input())):
	_ = input()
	students = input()
	print(solution(students))


for u in range(int(input())):
	input()
	s = input().lower().lstrip('p')
	print(max(map(len, s.split('a'))))


n = int(input())
for _ in range(n):
	k = input()
	s = input().strip()
	if 'A' in s:
		s = s[s.index('A'):]
		s = s.split('A')
		print(max([len(x) for x in s]))
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	cnt = []
	temp = 0
	flag = 0
	for i in range(n):
		if a[i] == 'P' and flag == 0:
			continue
		if a[i] == 'A':
			flag = 1
			cnt.append(temp)
			temp = 0
		else:
			temp += 1
	else:
		cnt.append(temp)
	print(max(cnt))


n = int(input())
for i in range(n):
	maxa = 0
	x = int(input())
	sir = input()
	ok = 1
	cnt = 0
	for caracter in sir:
		if caracter == 'A':
			cnt = 0
			ok = 0
		elif ok == 0:
			cnt += 1
		if ok == 0:
			maxa = max(maxa, cnt)
	print(maxa)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	f = False
	m = 0
	count = 0
	for i in s:
		if i == 'A':
			m = max(m, count)
			f = True
			count = 0
		elif f:
			count += 1
	m = max(m, count)
	print(m)


for i in range(int(input())):
	ts = int(input())
	s = input()
	i = 0
	n = 0
	while n != 1:
		cp = 0
		l = ''
		l = l + s[0]
		for j in range(1, ts):
			if s[j - 1] == 'A' and s[j] == 'P':
				cp = 1
				l = l + 'A'
			else:
				l = l + s[j]
		s = l
		if cp != 1:
			n = 1
		i += 1
	print(i - 1)


for i in range(int(input())):
	n = int(input())
	hihi = input()
	arr = hihi.split('A')
	if len(arr) == 1:
		print(0)
		continue
	else:
		arr = arr[1:]
	print(len(max(arr)))


N = int(input())
for _ in range(N):
	numStudents = int(input())
	s = input()
	cnt = None
	ans = 0
	for c in s:
		if cnt is not None and c == 'P':
			cnt += 1
			ans = max(cnt, ans)
		elif c == 'A':
			cnt = 0
	print(ans)


test = int(input())
while test != 0:
	n = int(input())
	s = str(input())
	start_index = -1
	for i in range(n):
		if s[i] == 'A':
			start_index = i
			break
	cnt = 0
	how_many = 0
	if start_index != -1:
		for i in range(start_index + 1, n):
			if s[i] == 'P':
				cnt += 1
			else:
				cnt = 0
			if how_many < cnt:
				how_many = cnt
		print(how_many)
	else:
		print('0')
	test -= 1


def solve(n, students):
	angry_idx = -1
	post_angry_patient_count = 0
	max_post_angry_patient_count = 0
	for i in range(0, n):
		if students[i] == 'A':
			angry_idx = i
			post_angry_patient_count = 0
		elif angry_idx != -1:
			post_angry_patient_count += 1
		max_post_angry_patient_count = max(post_angry_patient_count, max_post_angry_patient_count)
	return max_post_angry_patient_count
t = int(input())
for _ in range(0, t):
	n = int(input())
	students = input()
	print(solve(n, students))


def main():
	for i in range(int(input())):
		k = int(input())
		s = input()
		max = 0
		count = 0
		check = False
		for student in s:
			if student == 'A':
				if count > max:
					max = count
				count = 0
				check = True
			elif check == True:
				count += 1
		if s[-1] == 'P' and count > max:
			max = count
		print(max)
main()


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	t = s.split('A')
	le = []
	if len(s) < 2:
		print(0)
	else:
		for j in t:
			le.append(len(j))
		if le[0] == 0:
			print(max(le))
		elif len(le) > 1:
			print(max(le[1:]))
		else:
			print(0)


t = int(input())
while t:
	n = int(input())
	s = input()
	s = list(s)
	news = s[:]
	i = 0
	count = 0
	for i in range(len(s)):
		j = 0
		while j < len(s) - 1:
			if s[j] == 'A' and s[j + 1] == 'A':
				j += 1
			elif s[j] == 'A' and s[j + 1] == 'P':
				news[j + 1] = 'A'
				j += 2
			else:
				j = j + 1
		if news[:] != s[:]:
			s = news[:]
			count += 1
	print(count)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	flag = 0
	k = []
	for i in s:
		k.append(i)
	tin = 0
	while flag == 0:
		i = 1
		number = 0
		while i <= len(k) - 1:
			if k[i] == 'P' and k[i - 1] == 'A':
				k[i] = 'A'
				i += 2
				number += 1
			else:
				i += 1
		if number > 0:
			tin += 1
		else:
			flag = 1
	print(tin)


testCase = int(input())
listTemp = []
while testCase:
	n = int(input())
	inputStr = input()
	m = 0
	while inputStr.count('AP') > 0:
		inputStr = inputStr.replace('AP', 'AA')
		m += 1
	listTemp.append(m)
	testCase -= 1
for x in listTemp:
	print(x)


q = int(input())
for i in range(q):
	n = int(input())
	a = list(input()) + ['A']
	k = 0
	ans = 0
	f = 0
	for i in range(len(a)):
		if a[i] == 'A':
			f = 1
			ans = max(k, ans)
			k = 0
		elif f == 1:
			k += 1
	print(ans)


t = int(input())
a = []
for i in range(t):
	x = int(input())
	c = input()
	mx = 0
	for f in range(x - 1):
		mx1 = 0
		if c[f] == 'A' and c[f + 1] == 'P':
			mx1 = mx1 + 1
			for j in range(f + 2, len(c)):
				if c[j] == 'P':
					mx1 = mx1 + 1
				else:
					break
		mx = max(mx1, mx)
	a.append(mx)
for i in range(len(a)):
	print(a[i])


for T in range(int(input())):
	input()
	ans = 0
	isFirst = True
	for i in list(input().split('A')):
		if isFirst:
			isFirst = False
			continue
		ans = max(ans, len(i))
	print(ans)


import re
for _ in range(int(input())):
	lens = int(input())
	apap = input()
	candidate = re.findall('AP*', apap)
	maxs = 0
	for i in candidate:
		curr = i.count('P')
		maxs = max(maxs, curr)
	print(maxs)


def case():
	n = int(input())
	s = input()
	flag = False
	count = 0
	mcount = 0
	for i in s:
		if i == 'A':
			flag = True
		if not flag:
			continue
		if i == 'P':
			count += 1
		else:
			mcount = max(count, mcount)
			count = 0
	mcount = max(count, mcount)
	print(mcount)
for _ in range(int(input())):
	case()


import math
from collections import *
import sys
from functools import reduce

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
DIRECTIONS = [[0, 1], [0, -1], [1, 0], [1, -1]]
NEIGHBOURS = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0]
OrdUnicode_a = ord('a')
OrdUnicode_A = ord('A')
CAPS_ALPHABETS = {chr(i + OrdUnicode_A): i for i in range(26)}
SMOL_ALPHABETS = {chr(i + OrdUnicode_a): i for i in range(26)}
UGLYMOD = int(1000000000.0) + 7
SEXYMOD = 998244353
MAXN = int(100000.0) + 1
import itertools
for _testcases_ in range(int(input())):
	n = int(input())
	s = input()
	try:
		ind = s.index('A')
	except:
		ind = n - 1
	if ind == n - 1:
		print('0')
	else:
		ans = 0
		d = itertools.groupby(s[ind:])
		for (k, v) in d:
			if k == 'P':
				v = len(list(v))
				ans = max(ans, v)
		print(ans)


for i in range(int(input())):
	t = int(input())
	a = input()
	(m, i) = (0, 0)
	while i < t and a[i] == 'P':
		i += 1
	while i < t:
		count = 0
		i += 1
		while i < t and a[i] == 'P':
			count += 1
			i += 1
		m = max(m, count)
	print(m)


t = int(input())
while t > 0:
	k = int(input())
	s = input()
	lst = list(s)
	count = 0
	while 1:
		flag = 0
		i = 0
		while i < k - 1:
			if lst[i] == 'A' and lst[i + 1] == 'P':
				lst[i + 1] = 'A'
				i += 2
				flag = 1
			else:
				i += 1
		if flag == 0:
			break
		count += 1
	print(count)
	t -= 1


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	num = 0
	ans = 0
	first = s.find('A')
	if first == -1:
		first = k
	for i in range(first, k):
		if s[i] == 'P':
			num += 1
		else:
			ans = max(ans, num)
			num = 0
	print(max(ans, num))


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	s = list(s)
	c = 0
	l = []
	for i in range(n):
		if s[i] == 'A':
			l.append(i)
	li = []
	z = []
	x = 0
	if len(l) != z:
		l.append(n)
	for i in range(len(l) - 1):
		li.append(l[i + 1] - l[i] - 1)
	if li == z:
		print(0)
	else:
		print(max(li))


from math import *
from bisect import bisect_left as bl
from bisect import bisect_right as br
from collections import Counter as ctr
from collections import deque as dq
from array import array
from re import search
li = lambda : list(map(int, input().split()))
arr = lambda a: array('i', a)
bi = lambda n: bin(n).replace('0b', '')
yn = lambda f: print('NYOE S'[f::2])
sbstr = lambda a, s: search('.*'.join(a), s)

def solve():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		if 'P' in s and 'A' in s:
			s = s.split('A')
			s.pop(0)
			print(max((len(x) for x in s)))
		else:
			print('0')
solve()


from math import ceil
from math import factorial
from collections import Counter
from operator import itemgetter
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
t = ii()
for _ in range(t):
	n = ii()
	s = input()
	x = [0] * n
	c = 0
	flag = 0
	for i in range(n):
		if s[i] == 'A':
			c = 0
			flag = 1
		elif flag == 1:
			c += 1
			x[i] = c
	print(max(x))


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	count = 0
	new_list = []
	old_list = []
	for i in range(n):
		j = 0
		old_list = s.copy()
		while j < n - 1:
			if s[j] == 'A' and s[j + 1] == 'P':
				s[j + 1] = 'A'
				j += 1
			j += 1
		new_list = s.copy()
		if new_list == old_list:
			break
		else:
			count += 1
	print(count)


t = int(input())
for i in range(t):
	n = 0
	ans = 0
	k = int(input())
	s = list(input())[::-1]
	for c in s:
		if c == 'P':
			n += 1
		else:
			if ans < n:
				ans = n
			n = 0
	print(ans)


for i in range(int(input())):
	k = int(input())
	s = input()
	longest = 0
	curr = 0
	for i in range(k - 1, -1, -1):
		if s[i] == 'P':
			curr += 1
		else:
			longest = max(longest, curr)
			curr = 0
	print(longest)


for i in range(int(input())):
	input()
	counter = 0
	x = []
	s = str(input())
	if len(set(s)) == 1:
		print(0)
	else:
		for i in range(len(s)):
			if s[i] == 'A':
				s = s[i:]
				break
		for j in s:
			if j == 'A':
				x.append(counter)
				counter = 0
			else:
				counter += 1
		x.append(counter)
		print(max(x))


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	ans = 0
	m = None
	for i in range(len(s)):
		if s[i] == 'P' and m != None:
			ans = max(ans, i - m)
		elif s[i] == 'A':
			m = i
	print(ans)
	t -= 1


t = int(input())
for v in range(t):
	n = int(input())
	s = input()
	ans = 0
	i = 0
	while i < n:
		if s[i] == 'A':
			j = i + 1
			count = 0
			while j < n and s[j] == 'P':
				count += 1
				j += 1
			ans = max(count, ans)
			i = j
		else:
			i += 1
	print(ans)


n = eval(input())
i = 0
j = 0
a = c = 0
l = []
while i < n:
	x = eval(input())
	s = input()
	l = list(s)
	while j < len(s) - 1:
		if l[j] == 'A':
			if l[j + 1] == 'P':
				l[j + 1] = 'A'
				c += 1
			else:
				if c > a:
					a = c
				c = 0
		j += 1
	if c > a:
		a = c
	print(a)
	a = c = j = 0
	i += 1


t = int(input())
for inh in range(0, t):
	n = int(input())
	s = input()
	j = False
	ans = 0
	m = 0
	for i in range(0, n):
		if j:
			if s[i] == 'A':
				m = 0
			else:
				m += 1
		elif s[i] == 'A':
			j = True
		if m > ans:
			ans = m
	print(ans)


import math
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	s = input()
	f = False
	b = []
	c = 0
	for i in range(n):
		if f == True and s[i] == 'P':
			c += 1
		elif s[i] == 'A' and f == True:
			b.append(c)
			c = 0
		if s[i] == 'A':
			f = True
	if c != 0:
		b.append(c)
	if len(b) == 0:
		print(0)
	else:
		print(max(b))


for _ in range(int(input())):
	n = int(input())
	string = input()
	index = None
	count = 0
	for i in range(n):
		if string[i] == 'A':
			index = i
			break
	if index is None or index == n - 1:
		print(0)
	else:
		temp = 0
		for i in range(index, n):
			if string[i] == 'P':
				temp += 1
			else:
				count = max(temp, count)
				temp = 0
		count = max(temp, count)
		print(count)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	for j in range(len(s)):
		if s[j] == 'A':
			break
	tcount = count = 0
	for k in range(j + 1, len(s)):
		if s[k] == 'P':
			tcount = tcount + 1
			if tcount > count:
				count = tcount
		else:
			tcount = 0
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	arr = [0]
	count = 0
	for i in range(1, n):
		if s[i] == 'P' and s[i - 1] == 'A':
			for j in range(i, n):
				if s[j] == 'P':
					count += 1
				else:
					arr += [count]
					count = 0
					break
	print(max(max(arr), count))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	ans = 0
	cur = -float('inf')
	for i in range(k):
		if s[i] == 'A':
			ans = max(ans, cur)
			cur = 0
		else:
			cur += 1
	ans = max(ans, cur)
	print(ans)


def string_after_minute(minute, a):
	d = 0
	b = list(a)
	for i in range(len(a) - 1):
		if a[i] == 'A' and a[i + 1] == 'P':
			b[i + 1] = 'A'
			d = 1
	if d == 1:
		minute = minute + 1
		string_after_minute(minute, b)
	else:
		print(minute)
a = []
b = []
groups = int(input(''))
for i in range(groups):
	a.append(int(input('')))
	b.append(input(''))
for i in range(len(a)):
	minute = 0
	string_after_minute(minute, list(b[i]))


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	start = 0
	for i in range(n):
		if s[i] == 'A':
			start = i
			break
	(flag, ans) = (1, 0)
	if start == n - 1:
		print('0')
	else:
		while flag:
			flag = 0
			repeat = ''
			for i in range(start + 1):
				repeat += s[i]
			for i in range(start + 1, n):
				if s[i - 1] == 'A' and s[i] == 'P':
					flag = 1
					repeat += 'A'
				else:
					repeat += s[i]
			ans += 1
			s = ''
			s += str(repeat)
		print(ans - 1)


def f():
	l = int(input())
	s = input()
	ans = 0
	i = 0
	while i < l:
		if s[i] == 'A':
			break
		i += 1
	hold = 0
	while i < l:
		if s[i] == 'A':
			ans = max(ans, hold)
			hold = 0
		else:
			hold += 1
		i += 1
	ans = max(ans, hold)
	return ans
t = int(input())
for i in range(t):
	print(f())


from pprint import pprint
import sys
q = int(input())
for _ in range(q):
	n = int(input())
	res = 0
	s = input()
	isa = False
	cur = 0
	for i in range(n):
		if s[i] == 'A':
			isa = True
			cur = 0
		if isa and s[i] == 'P':
			cur += 1
		res = max(res, cur)
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	string = input()
	arr = []
	for i in range(n):
		if string[i] == 'A':
			arr.append(i + 1)
	ans = 0
	count = 0
	for i in range(len(arr) - 1):
		if arr[i] + count < arr[i + 1] - 1:
			ans += arr[i + 1] - 1 - (count + arr[i])
			count += arr[i + 1] - 1 - (count + arr[i])
	else:
		if len(arr) > 0:
			if arr[-1] + count < n:
				ans += n - (count + arr[-1])
	print(ans)


n = int(input())
for i in range(n):
	m = int(input())
	k = input()
	if 'A' not in k:
		print(0)
	else:
		ind = k.index('A')
		k = k[ind + 1:]
		k = k.split('A')
		print(len(max(k)))


import re
n = int(input())
for i in range(n):
	k = int(input())
	stu = input()
	if 'A' in stu:
		numP = re.split('A+', stu)
		numP.sort(reverse=True)
		if 'A' in stu[0:stu.index(numP[0])]:
			print(len(numP[0]))
		else:
			print(len(numP[1]))
	else:
		print(0)


t = int(input())
for i in range(t):
	k = int(input())
	str = input()
	change = False
	time = 0
	maxi = 0
	for j in range(k):
		if str[j] == 'A':
			change = True
			time = 0
		elif str[j] == 'P':
			if change:
				time += 1
				maxi = max(maxi, time)
	print(maxi)


t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	if 'A' in s:
		start = s.index('A')
		print(len(max(s[start:].split('A'))))
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	flag = 0
	ma = 0
	cur = 0
	for i in range(n):
		if a[i] == 'A':
			flag = 1
		if flag:
			if a[i] == 'P':
				cur += 1
			if a[i] == 'A':
				ma = max(ma, cur)
				cur = 0
	ma = max(ma, cur)
	print(ma)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	mx = 0
	if 'A' in s:
		x = s.index('A')
		s = s[x + 1:]
		for i in range(len(s), 0, -1):
			if 'P' * i in s and mx < i:
				mx = i
	print(mx)


t = input()
for x in range(int(t)):
	ans = 0
	k = input()
	students = input()
	for i in range(len(students) - 1):
		if students[i] == 'A':
			count = 0
			for j in range(i + 1, len(students)):
				if students[j] == 'A':
					break
				count += 1
			if count > ans:
				ans = count
	print(ans)


def solve():
	n = int(input())
	s = input()
	ans = 0
	k = -1
	for i in range(n):
		if s[i] == 'P' and k > -1:
			ans = max(ans, i - k)
		elif s[i] == 'A':
			k = i
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def answer():
	n = int(input())
	while n:
		b = int(input())
		i = 0
		k = input()
		a = [len(x) for x in k.split('A')]
		if len(a):
			if k[0] != 'A':
				a.remove(a[0])
			if len(a):
				print(max(a))
			else:
				print(0)
		else:
			print(0)
		n -= 1
answer()


def maxConsecutive1(input):
	print(max(map(len, input.split('A'))))
t = int(input())
for i in range(t):
	g = int(input())
	r = str(input())
	if 'A' in r:
		index = r.index('A')
		maxConsecutive1(r[index:])
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ss = [0]
	dif = [0]
	if 'A' in s:
		s = s[s.index('A'):]
		for i in range(1, len(s)):
			if s[i] == 'A':
				ss.append(i)
			dif.append(i - ss[-1])
		print(max(dif))
	else:
		print(0)


def solve(k, s):
	r = -1
	maxi = 0
	i = 0
	while i < k:
		if s[i] == 'A':
			r = 0
		if r >= 0:
			if s[i] == 'P':
				r += 1
				maxi = max(r, maxi)
			else:
				r = 0
		i += 1
	return max(0, maxi)

def main():
	for _ in range(int(input())):
		k = int(input())
		s = input()
		print(solve(k, s))
main()


t = int(input())
for k in range(t):
	n = int(input())
	l = input()
	res = [0]
	for i in range(1, len(l)):
		if l[i] == 'A':
			res.append(0)
			continue
		if l[i - 1] == 'A':
			res.append(1)
		elif res[-1] != 0:
			res.append(res[-1] + 1)
		else:
			res.append(0)
	print(max(res))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	counter = 0
	while True:
		ns = s[0]
		before = s[0]
		for c in s[1:]:
			if before == 'A':
				ns += 'A'
			else:
				ns += c
			before = c
		if ns == s:
			break
		s = ns
		counter += 1
	print(counter)


for _ in range(int(input())):
	n = int(input())
	print(max(map(len, input().split('A')[1:] + [''])))


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def main():
	t = inin()
	for _ in range(t):
		x = 0
		n = inin()
		s = sin()
		an = ans = 0
		for i in s:
			if i == 'A':
				ans = 0
				x = 1
			elif i == 'P' and x == 1:
				ans += 1
				an = max(an, ans)
		print(an)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


n = int(input())
for i in range(0, n):
	a = int(input())
	s = input()
	c = 0
	while True:
		j = 0
		c += 1
		t = 0
		while j < a - 1:
			if s[j] == 'A' and s[j + 1] == 'P':
				s = s[:j + 1] + 'A' + s[j + 2:]
				j += 1
				t += 1
			j += 1
		if t == 0:
			print(c - 1)
			break


n = int(input())
l = []
for i in range(n):
	input()
	l.append(input())
res = []
for i in l:
	co = []
	n = 0
	for j in range(len(i) - 1):
		c = 0
		if i[j] == 'A' and i[j + 1] != 'A':
			k = j + 1
			while i[k] != 'A':
				c += 1
				if k < len(i) - 1:
					k += 1
				else:
					break
			co.append(c)
		else:
			n += 1
		if n == len(i) - 1:
			co.append(0)
	if len(i) != 1:
		res.append(max(co))
	else:
		res.append(0)
for i in res:
	print(i)


for _ in range(int(input())):
	k = int(input())
	s = input()
	count = 0
	m = -1
	l = 0
	for i in range(k):
		if l == 0:
			if s[i] == 'A':
				l = 1
				count = 0
			elif s[i] == 'P':
				l = 0
			else:
				break
		elif s[i] == 'P':
			count += 1
		elif s[i] == 'A':
			l = 1
			count = 0
		else:
			break
		if m < count:
			m = count
	print(m)


for _ in [*open(0)][2::2]:
	(k, q) = (0, 0)
	for i in _[::-1]:
		if i == 'P':
			q += 1
		else:
			k = max(k, q)
			q = 0
	print(k)


def main():
	examples = input()
	for _ in range(int(examples)):
		num_students = input()
		line = input()
		possible_angry = set()
		first_angry = -1
		for index in range(int(num_students)):
			if first_angry >= 0:
				if line[index] == 'P':
					possible_angry.add(index)
			elif line[index] == 'A':
				first_angry = index
		time = 0
		while len(possible_angry) > 0:
			next_possible_angry = set()
			time += 1
			for a in possible_angry:
				if a - 1 in possible_angry:
					next_possible_angry.add(a)
			possible_angry = next_possible_angry
		print(time)
main()


t = int(input())
for _ in range(t):
	k = int(input())
	try:
		s = input().split('A')[1:]
		print(len(max(s)))
	except:
		print(0)


t = int(input())

def main():
	x = int(input())
	students = input() + 'A'
	cnt = 0
	ans = 0
	ls = []
	for (i, s) in enumerate(students):
		if s == 'A':
			ls.append(i)
	for i in range(1, len(ls)):
		ans = max(ans, ls[i] - ls[i - 1] - 1)
	print(ans)
for i in range(t):
	main()


import re
t = int(input())
lst = []
lstg = []
for i in range(t):
	count = 0
	input()
	s = input()
	x = (s, -1)
	while x[1] != 0:
		x = re.subn('AP', 'AA', s)
		s = x[0]
		count += 1
	lst.append(count - 1)
print(*lst, sep='\n')


def int_lst_input():
	return [int(val) for val in input().split(' ')]

def int_input():
	return int(input())

def print_lst(lst):
	print(' '.join([str(val) for val in lst]))

def solve():
	t = int_input()
	for i in range(t):
		k = int_input()
		s = list(input())
		a = 0
		while True:
			local_a = 0
			for j in range(1, len(s)):
				if s[j - 1] == 'A' and s[j] == 'P':
					local_a += 1
					s[j] = 'a'
			if local_a == 0:
				break
			a += 1
			for j in range(len(s)):
				if s[j] == 'a':
					s[j] = 'A'
		print(a)
solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	x = 0
	f = 0
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			x = 0
			f = 1
		elif s[i] == 'P' and f == 1:
			x += 1
		ans = max(ans, x)
	print(ans)


for i in range(int(input())):
	l = input()
	s = list(input().split('A'))
	a = list(map(len, s))
	a.pop(0)
	if not a:
		print('0')
	else:
		print(max(a))


T = int(input())
for _ in range(T):
	max_ = -1
	flag = 0
	count = 0
	x = int(input())
	s = input()
	for c in s:
		if c == 'A' and flag == 0:
			flag = 1
		if flag == 1 and c == 'P':
			count += 1
		else:
			max_ = max(count, max_)
			count = 0
	max_ = max(count, max_)
	print(max_)


t = int(input())
for _ in range(t):
	k = int(input())
	a = input()
	c = 0
	while 'AP' in a:
		a = a.replace('AP', 'AA')
		c = c + 1
	print(c)


def comp(list1, length):
	max1 = 0
	count = 0
	done = False
	for i in range(length):
		if list1[i] == 'A':
			done = True
			max1 = max(max1, count)
			count = 0
		elif done == True:
			count = count + 1
	max1 = max(count, max1)
	return max1
cases = int(input())
for i in range(cases):
	length = int(input())
	list1 = input()
	res = comp(list1, length)
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	max = 0
	for i in range(0, n):
		if s[i] == 'A':
			nd = 0
			for j in range(i + 1, n):
				if s[j] == 'A':
					break
				else:
					nd += 1
			if max < nd:
				max = nd
	print(max)


import sys
input = sys.stdin.readline
t = int(input().strip())
for tt in range(t):
	k = int(input())
	pre = ''
	f = False
	ans = 0
	cnt = 0
	for s in input().strip():
		if not f:
			if s == 'A':
				f = True
			continue
		if s == 'P':
			cnt += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
	else:
		ans = max(ans, cnt)
	print(ans)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	S = input()
	cnt = ans = 0
	for s in S[::-1]:
		if s == 'P':
			cnt += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
	print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	n = ip()
	s = ips()
	if s.count('P') == n:
		ans = 0
	else:
		for i in range(n):
			if s[i] == 'A':
				st = i
				break
		s = s[st + 1:]
		n = len(s)
		if n == 0:
			ans = 0
		else:
			ct = 0
			gap = []
			for i in range(n):
				if s[i] == 'P':
					ct += 1
				else:
					if ct != 0:
						gap.append(ct)
					ct = 0
			if ct != 0:
				gap.append(ct)
			if len(gap) == 0:
				ans = 0
			else:
				ans = max(gap)
	out(ans)


def solve():
	le = int(input())
	s = input()
	lis = []
	flag = 0
	cnt = 0
	for i in range(le):
		if s[i] == 'P' and flag:
			cnt = cnt + 1
		elif s[i] == 'A':
			if flag:
				lis.append(cnt)
			cnt = 0
			flag = 1
	lis.append(cnt)
	lis.sort()
	lis.reverse()
	print(lis[0])
	return
n = int(input())
for i in range(n):
	solve()


a = int(input())
for i in range(a):
	b = int(input())
	s = input() + 'A'
	ma = 0
	cur = 0
	for j in range(s.find('A'), len(s)):
		if s[j] == 'P':
			cur += 1
		else:
			ma = max(ma, cur)
			cur = 0
	ma = max(ma, cur)
	print(ma)


g = int(input())
for _ in range(g):
	n = int(input())
	s = input() + 'A'
	f = False
	apos = 0
	mpos = 0
	for i in range(len(s)):
		if s[i] == 'A' and (not f):
			f = True
			apos = i
		if s[i] == 'A':
			mpos = max(mpos, i - apos - 1)
			apos = i
	print(mpos)


T = int(input())
for z in range(T):
	n = int(input())
	v = str(input())
	ans = 0
	cur = 0
	v = v.lstrip('P')
	for val in v:
		if val == 'P':
			cur += 1
		else:
			ans = max(ans, cur)
			cur = 0
	ans = max(ans, cur)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input() + 'A'
	cur = a.index('A')
	ans = 0
	for i in range(1, n + 1):
		if a[i] == 'A':
			ans = i - cur if i - cur > ans else ans
			if cur == 0:
				ans -= 1
			cur = i + 1
	print(ans)


t = int(input())
while t:
	t += -1
	k = int(input())
	s = input()
	s += 'A'
	ans = 0
	l = []
	for i in range(k + 1):
		if s[i] == 'A':
			l.append(i)
	for i in range(1, len(l)):
		ans = max(ans, l[i] - l[i - 1])
	print(max(0, ans - 1))


el = int(input())
for x in range(el):
	el2 = int(input())
	ar = list(input())
	count = 0
	mC = 0
	ch = 0
	for x in range(len(ar)):
		if ar[x] == 'P' and ch == 0:
			continue
		elif ar[x] == 'P' and ch != 0:
			count = count + 1
			if count > mC:
				mC = count
		elif ar[x] == 'A':
			count = 0
			ch = 1
	print(mC)


def main():
	for _ in range(int(input())):
		input()
		print(len(max(input().lstrip('P').split('A'), default='')))
main()


n = int(input())
for i in range(n):
	r = int(input())
	e = input()
	t = 0
	while e.count('AP') > 0:
		e = e.replace('AP', 'AA')
		t += 1
	print(t)


import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	s = input()
	mx = -1
	i = 0
	co = 0
	while i < n:
		if s[i] == 'A':
			i += 1
			while i < n and s[i] == 'P':
				co += 1
				i += 1
			if co > mx:
				mx = co
			if i != n:
				co = 0
		else:
			i += 1
	if co > mx:
		mx = co
	print(mx)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	mx = 0
	f = -1
	for i in range(n):
		if s[i] == 'A':
			f = 0
		elif f >= 0:
			f += 1
		mx = max(f, mx)
	print(mx)


import sys, re, math
from decimal import *
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2, floor
from heapq import heappush, heappop, heapify, nlargest, nsmallest

def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))[::-1]
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
INF = float('inf')
mod = 10 ** 9 + 7
t = INT()
while t != 0:
	n = INT()
	k = input()
	mxCons = 0
	i = 0
	while i < len(k):
		ch = k[i]
		if ch == 'A':
			mx = 0
			j = i + 1
			for j in range(i + 1, len(k)):
				if k[j] == 'P':
					mx += 1
				else:
					break
				mxCons = max(mx, mxCons)
			i = j
		else:
			i += 1
	print(mxCons)
	t -= 1


def solve():
	k = int(input())
	s = input()
	s = s.split('A')
	s = s[1:]
	ans = 0
	for element in s:
		ans = max(ans, len(element))
	print(ans)
t = int(input())
for _ in range(t):
	solve()


tc = int(input())
i = 0
while i < tc:
	sn = int(input())
	row = input()
	newRow = ''
	prevRow = ''
	moments = 0
	while row != prevRow:
		newRow = row[0]
		j = 1
		while j < len(row):
			if row[j - 1] == 'A':
				newRow += 'A'
			else:
				newRow += row[j]
			j += 1
		moments += 1
		prevRow = row
		row = newRow
		newRow = ''
	print(moments - 1)
	i += 1


import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
	n = int(sys.stdin.readline().strip())
	s = sys.stdin.readline().strip()
	angry = False
	longest = 0
	patient_run = 0
	for c in s:
		if c == 'P':
			if angry:
				patient_run += 1
		else:
			angry = True
			longest = max(patient_run, longest)
			patient_run = 0
	longest = max(patient_run, longest)
	print(longest)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	(i, time, large) = (0, 0, 0)
	while i < n:
		cnt = 0
		if i < n - 1 and s[i] == 'A':
			i += 1
			while i < n and s[i] == 'P':
				cnt += 1
				i += 1
		if i < n and s[i] == 'P':
			i += 1
		if large < cnt:
			large = cnt
		if i == n - 1:
			break
	print(large)
	t -= 1


tests = int(input())
for te in range(tests):
	n = int(input())
	stringa = input()
	i = 0
	flag = 1
	ans = 0
	while i < n and flag == 1:
		if stringa[i] == 'A':
			if i == n - 1:
				flag = 0
				break
			j = i + 1
			while j < n and stringa[j] == 'P':
				j += 1
			ans = max(ans, j - i - 1)
			i = j
		else:
			i = i + 1
			continue
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	q = []
	c = 0
	d = {}
	for i in s:
		d[i] = 0
		if i == 'A':
			q.append(c)
			c = 0
		elif 'A' in d:
			c = c + 1
	q.append(c)
	print(max(q))


t = int(input())
for i in range(t):
	m = 0
	z = 0
	k = int(input())
	arr = list(input())
	for l in range(k):
		if arr[l] == 'A':
			break
	for j in range(l + 1, k):
		if arr[j] == 'P':
			m += 1
			if z < m:
				z = m
		else:
			m = 0
	print(z)


t = int(input())
for i in range(t):
	size = int(input())
	children = input()
	(max_sequence, last_angry) = (0, -1)
	curr_sequence = 0
	for j in range(len(children)):
		if children[j] == 'A':
			last_angry = j
			if curr_sequence > max_sequence:
				max_sequence = curr_sequence
			curr_sequence = 0
		elif last_angry != -1:
			curr_sequence += 1
	if curr_sequence > max_sequence:
		max_sequence = curr_sequence
	print(max_sequence)


for _ in range(int(input())):
	n = int(input())
	x = input()
	lsf = -1
	cr = 0
	for c in x:
		if c == 'A':
			lsf = max(cr, lsf)
			cr = 0
		elif lsf != -1:
			cr += 1
	print(max(cr, lsf))


for i in range(int(input())):
	n = int(input())
	s = input()
	flag = 0
	for j in range(n):
		if s[j] == 'A':
			l = n - j
			for k in range(j, n + 1):
				if 'P' * l in s[j:n]:
					print(l)
					flag = 1
					break
				l -= 1
			break
	if flag != 1:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	s1 = list(s)
	a = 0
	for i in range(len(s1)):
		k = 0
		for j in range(len(s1) - 1):
			if s[j] == 'A':
				if s[j + 1] == 'P':
					s1[j + 1] = 'A'
					k = 1
		if k == 0:
			break
		s = ''.join(s1)
		a += k
	print(a)


sum = 0
for i in range(int(input())):
	k = int(input())
	li = input()
	sum = 0
	for i in li:
		ki = li
		li = li.replace('AP', 'A')
		if ki != li:
			sum += 1
	print(sum)


b = int(input())
for n in range(b):
	c = int(input())
	a = input()
	m = 0
	for i in range(c - 1):
		if a[i] == 'A' and a[i + 1] == 'P':
			i += 1
			k = 0
			while i != c and a[i] == 'P':
				k += 1
				i += 1
			m = max(m, k)
	print(m)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	ans = 0
	for j in range(k):
		if s[j] == 'A':
			cnt = 0
			for l in range(j + 1, k):
				if s[l] == 'P':
					cnt += 1
				else:
					ans = max(ans, cnt)
					break
			else:
				ans = max(ans, cnt)
	print(ans)


n = int(input())
c = 0
l = []
for _ in range(n):
	m = int(input())
	s = list(input())
	max = 0
	if 'A' in s:
		d = s.index('A')
		for i in range(d, m):
			if s[i] == 'P':
				c += 1
				if c > max:
					max = c
			else:
				c = 0
		print(max)
	else:
		print('0')


lol = ''
x = int(input())
for i in range(x):
	y = int(input())
	contador = 0
	maxi = 0
	lol = input()
	h = len(lol)
	for j in range(len(lol)):
		if lol[j] == 'A':
			h = j
			break
	for j in range(h, len(lol)):
		if lol[j] == 'A':
			if contador > maxi:
				maxi = contador
			contador = 0
		else:
			contador += 1
	if contador > maxi:
		maxi = contador
	print(maxi)


for _ in range(int(input())):
	k = int(input())
	s = input()
	arr = []
	max_n = -1
	for i in range(k):
		if s[i] == 'A':
			arr.append(i)
	if arr == [] or arr == [k - 1]:
		print(0)
		continue
	arr.append(k)
	for i in range(len(arr) - 1):
		p = arr[i + 1] - arr[i]
		if p > max_n:
			max_n = p
	print(max_n - 1)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = list(inp())
	a.append('A')
	ans = 0
	cnt = 0
	f = False
	for i in range(0, n + 1):
		if a[i] == 'A':
			if f:
				ans = max(ans, cnt)
				cnt = 0
			f = True
		elif f:
			cnt += 1
		else:
			continue
	print(ans)
testcase(int(inp()))


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	t = []
	f = 0
	c = 0
	while a != t:
		t = a[:]
		c += 1
		for i in range(n - 1, 0, -1):
			if a[i - 1] == 'A':
				a[i] = 'A'
	print(c - 1)


import sys
n = int(sys.stdin.readline())
for _ in range(n):
	cs = [int(x) for x in sys.stdin.readline().split(' ')]
	s = sys.stdin.readline().rstrip()
	ss = 0
	counter = 0
	a = False
	for i in s:
		if i == 'A':
			a = True
			if counter > ss:
				ss = counter
			counter = 0
		elif i == 'P' and a:
			counter += 1
	if counter > ss:
		ss = counter
	print(ss)


t = int(input())
for i in range(t):
	l = int(input())
	s = input()
	if 'A' not in s:
		print('0')
	else:
		j = 0
		for j in range(0, l):
			x = s.find('A')
			if 'P' not in s[x:]:
				break
			s = s.replace('AP', 'AA')
		print(j)


t = int(input())
for lol in range(t):
	n = int(input())
	s = input()
	k = -1
	for i in range(len(s)):
		if s[i] == 'A':
			k = i
			break
	if k == -1:
		print(0)
	else:
		L = s[k:].split('A')
		ma = 0
		for i in L:
			ma = max(ma, len(i))
		print(ma)


T = int(input())

def countP(S):
	res = 0
	for x in S:
		if x == 'P':
			res += 1
		else:
			break
	return res

def solve(s):
	mx = 0
	for (i, x) in enumerate(s):
		if x == 'A':
			mx = max(mx, countP(s[i + 1:]))
	return mx
for _ in range(T):
	input()
	print(solve(input()))


for _ in range(int(input())):
	n = int(input())
	a = [i for i in input()]
	maxx = 0
	ans = []
	for i in range(n - 1, -1, -1):
		if a[i] == 'P':
			maxx += 1
		else:
			ans.append(maxx)
			maxx = 0
	if ans:
		print(max(ans))
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	(res, i) = (0, 0)
	while i < n:
		if s[i] == 'P':
			i += 1
			continue
		j = i + 1
		while j < n and s[j] == 'P':
			j += 1
		res = max(res, j - i - 1)
		i = j
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input().lstrip('P')
	s = s.split('A')
	m = []
	for i in s:
		a = len(i)
		m.append(a)
	print(max(m))


k = int(input())
out = []
while k > 0:
	n = int(input())
	s = str(input())
	s = list(s)
	m = 0
	while True:
		f = 0
		i = 0
		while i < len(s) - 1:
			if s[i] == 'A' and i != len(s) - 1 and (s[i + 1] == 'P'):
				s[i + 1] = 'A'
				i += 2
				f = 1
			else:
				i += 1
		if f == 1:
			m += 1
		else:
			out.append(m)
			break
	k -= 1
for x in out:
	print(x)


from itertools import groupby
for _ in range(int(input())):
	input()
	s = input().lstrip('P')
	max = 0
	for (k, v) in groupby(s):
		l = len(tuple(v))
		if l > max and k == 'P':
			max = l
	print(max)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if s.count('A') == 0 or s.count('P') == 0:
		print(0)
	else:
		s = s[s.index('A'):]
		s = list(s.split('A'))
		while s.count('') > 0:
			s.remove('')
		if s == []:
			print(0)
		else:
			Max = 0
			for i in range(len(s)):
				if len(s[i]) > Max:
					Max = len(s[i])
			print(Max)


import re
for i in range(int(input())):
	n = int(input())
	s = input()
	a = re.split('A+', s)[1:]
	if len(a) == 0:
		print(0)
	else:
		print(max(map(len, a)))


for _ in range(int(input())):
	n = int(input())
	s = input()
	i = 0
	mx = 0
	while i < n:
		if s[i] == 'A':
			i += 1
			c = 0
			while i < n and s[i] == 'P':
				c += 1
				i += 1
			mx = max(mx, c)
		else:
			i += 1
	print(mx)


for _ in range(int(input())):
	k = int(input())
	s = list(str(input()))
	l = []
	while 'A' in s:
		x = s.index('A')
		l.append(x)
		s[x] = 0
	if len(s) - 1 not in l:
		l.append(len(s))
	ans = 0
	for i in range(1, len(l)):
		ans = max(ans, l[i] - l[i - 1] - 1)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	index = 0
	while index < len(s) and s[index] == 'P':
		index += 1
	s = s[index:]
	s = max((len(x) for x in s.split('A')))
	print(s)


for _ in range(int(input())):
	input()
	s = input().lstrip('P')
	print(max(map(len, s.split('A'))))


n = input()
for _ in range(int(n)):
	_ = input()
	s = input()
	a = [1 if x == 'A' else 0 for x in s]
	first = -1
	for (i, e) in enumerate(a):
		if e == 1:
			first = i
			break
	if first == -1:
		print(0)
	else:
		maxl = 0
		while first < len(a):
			length = 0
			while first < len(a) and a[first] == 0:
				length += 1
				first += 1
			maxl = max(length, maxl)
			first += 1
		print(maxl)


for _ in range(int(input())):
	input()
	students = input()
	mx = 0
	now = 0
	flag = False
	for s in students:
		if s == 'P':
			if flag:
				now += 1
		else:
			flag = True
			mx = max(now, mx)
			now = 0
	mx = max(now, mx)
	print(mx)


for _ in range(int(input())):
	a = int(input())
	b = input()
	ans = 0
	ans1 = 0
	z = 0
	for i in b:
		if i == 'A':
			ans = max(ans, ans1)
			ans1 = 0
			z = 1
		elif z == 1:
			ans1 += 1
	ans = max(ans, ans1)
	print(ans)


from itertools import groupby
t = int(input())
for _ in range(t):
	k = int(input())
	ans = 0
	s = input() + 'A'
	s = s[s.find('A'):]
	for (k, v) in groupby(s):
		if k == 'P':
			ans = max(ans, len(list(v)))
	print(ans)


q = int(input())
for _ in range(q):
	n = int(input())
	st = input()
	if len(st) > 1 and len(set(st)) > 1:
		if st[0] != 'P':
			data = st.split('A')
		else:
			data = st.split('A')[1:]
		print(len(max(data, key=lambda x: len(x))))
	else:
		print(0)


t = int(input())
for _ in range(t):
	input()
	s = input()
	t = 0
	steps = 0
	angry = False
	for c in s:
		if c == 'A':
			angry = True
			t = 0
		elif angry:
			t += 1
			steps = max(steps, t)
	print(steps)


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep=' ', end='\n'):
	print(*var, file=sys.stderr, end=end, sep=sep)
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	s = input()
	ans = 0
	cur = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'P':
			cur += 1
		else:
			ans = max(ans, cur)
			cur = 0
	print(ans)


import sys
import math
t = int(input())
for ui in range(t):
	k = int(input())
	l = input()
	s = []
	for i in range(k):
		if l[i] == 'A':
			s.append(i)
	if s == []:
		print(0)
		continue
	maxi = -1
	for i in range(len(s) - 1):
		maxi = max(maxi, s[i + 1] - s[i] - 1)
	maxi = max(0, maxi, k - s[len(s) - 1] - 1)
	print(maxi)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	c = 0
	m = 0
	i = 0
	while i < n:
		if a[i] == 'A':
			break
		i += 1
	while i < n:
		if a[i] == 'P':
			c = c + 1
		else:
			m = max(c, m)
			c = 0
		i += 1
	m = max(c, m)
	print(m)


for _ in range(int(input())):
	l = int(input())
	s = input()
	count = 0
	count_list = [0]
	for i in range(l):
		if s[i] == 'A':
			if count != 0:
				count_list.append(count)
			count = 1
			continue
		elif s[i] == 'P' and count != 0:
			count += 1
	if count != 1:
		count_list.append(count)
	print(max(max(count_list) - 1, 0))


for _ in range(int(input())):
	n = int(input())
	s = input().split('A')
	res = 0
	for i in s[1:]:
		res = max(res, len(i))
	print(res)


t = int(input())
for q in range(t):
	k = int(input())
	s = input()
	x = 0
	c = 0
	f = 0
	for i in range(k):
		if s[i] == 'A':
			x = max(x, c)
			c = 0
			f = 1
		elif s[i] == 'P':
			if f == 1:
				c += 1
	x = max(x, c)
	print(x)


def Angry(lst):
	angry = 0
	count = 0
	oldCount = 0
	while count < len(lst) - 1:
		if lst[count] == 'A':
			newCount = 0
			count += 1
			while count < len(lst) and lst[count] != 'A':
				newCount += 1
				count += 1
			if newCount > oldCount:
				oldCount = newCount
		else:
			count += 1
	return oldCount

def main():
	n = eval(input())
	for i in range(n):
		r = input()
		nums = input()
		nums = [i for i in nums]
		print(Angry(nums))
main()


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	s = list(s)
	ss = list(s)
	ans = 0
	if 'A' not in s:
		print(0)
	else:
		n = s.index('A')
		while 'P' in ss[n:]:
			ans += 1
			for i in range(n, k):
				if i < k - 1 and s[i] == 'A':
					ss[i + 1] = 'A'
			s = list(ss)
		print(ans)


import sys
for _ in range(int(input())):
	k = int(input())
	a = input()
	res = 0
	cur = k - 1
	for i in range(k - 1, -1, -1):
		if a[i] == 'A':
			res = max(res, cur - i)
			cur = -1
		else:
			cur = max(cur, i)
	print(res)


a = int(input())
for i in range(0, a):
	c = int(input())
	s = input()
	if 'A' in s:
		pivot = s.index('A')
	else:
		pivot = 0
	count = []
	counter = 0
	z = 0
	for i in range(pivot, len(s)):
		if s[i] == 'A':
			z = 1
			count.append(counter)
			counter = 0
		if s[i] == 'P':
			if i == len(s) - 1 and z == 1:
				counter = counter + 1
				count.append(counter)
			counter = counter + 1
	if len(count) == 0:
		print(0)
	else:
		print(max(count))


t = int(input())
for you in range(t):
	n = int(input())
	s = input()
	maxa = 0
	for i in range(n):
		done = 0
		if s[i] == 'A':
			continue
		for j in range(i - 1, -1, -1):
			if s[j] == 'A':
				maxa = max(i - j, maxa)
				break
	print(maxa)


for _ in range(int(input())):
	n = int(input())
	word = list(map(str, input()))
	prev = word[:]
	cnt = 0
	while True:
		for i in range(n - 1):
			if word[i] == 'A':
				prev[i + 1] = 'A'
		cnt += 1
		if prev == word:
			break
		word = prev[:]
	print(cnt - 1)


t = int(input())
for x in range(t):
	k = int(input())
	a = input()
	C = 0
	while 'AP' in a:
		a = a.replace('AP', 'AA')
		C += 1
	print(C)


for _ in range(int(input())):
	n = int(input())
	string = str(input())
	l = []
	p = 0
	if n == 1:
		print(0)
	else:
		for i in range(n):
			if string[i] == 'A':
				p += 1
				l.append(i + 1)
		if p == 0:
			print(0)
		elif p == 1:
			if l[0] == n:
				print(0)
			else:
				print(n - l[0])
		else:
			m = 0
			for i in range(1, p):
				m = max(m, abs(l[i - 1] - l[i]) - 1)
			m = max(m, abs(l[-1] - n))
			print(m)


num_groups = int(input())
num = [None] * num_groups
a = [None] * num_groups
for kk in range(num_groups):
	num[kk] = int(input())
	a[kk] = input()
flag = False
max_times = [0] * num_groups
for kk in range(num_groups):
	time = 0
	for i in range(num[kk] - 1, -1, -1):
		if a[kk][i] == 'P':
			time += 1
		else:
			max_times[kk] = max(max_times[kk], time)
			time = 0
print(*max_times, sep='\n')


n = int(input())
for _ in range(n):
	m = int(input())
	s = input()
	prev = -1
	ans = 0
	for (i, x) in enumerate(s):
		if x == 'A':
			if prev >= 0:
				ans = max(ans, i - 1 - prev)
			prev = i
	if prev >= 0:
		ans = max(ans, len(s) - prev - 1)
	print(ans)


for i in range(int(input())):
	a = int(input())
	b = input()
	integ = 0
	done = 0
	while b[integ] != 'A':
		integ = integ + 1
		if integ == len(b):
			done = 1
			print(0)
			break
	if done == 0:
		b = b[integ:]
		ls = b.split('A')
		print(max([len(k) for k in ls]))


for _ in range(int(input())):
	n = int(input())
	s = input()
	angry = []
	res = 0
	s += 'A'
	for i in range(len(s)):
		if s[i] == 'A':
			angry.append(i + 1)
	for i in range(len(angry) - 1):
		res = max(res, angry[i + 1] - angry[i] - 1)
	print(res)


for _ in range(int(input())):
	n = int(input())
	ls = [*input()]
	c = 0
	flag = 0
	for i in range(len(ls) - 1, -1, -1):
		if ls[i] == 'P':
			c = c + 1
		else:
			flag = max(flag, c)
			c = 0
	print(flag)


t = int(input())
for i in range(t):
	k = int(input())
	s = list(input())
	minute = 0
	temp = s[:]
	while minute < k - 1:
		change = False
		for q in range(1, k):
			if s[q] == 'P' and s[q - 1] == 'A':
				temp[q] = 'A'
				change = True
		if not change:
			break
		minute += 1
		s = temp[:]
	print(minute)


n = int(input())
for _ in range(n):
	n1 = int(input())
	s = input()
	i = 0
	while i < n1:
		if s[i] == 'P':
			i += 1
		else:
			break
	s = s[i:]
	s = s.split('A')
	c = 0
	for e in s:
		if e != '' and len(e) > c:
			c = len(e)
	print(c)


def max_time(arr):
	last_angry = float('inf')
	current_time = 0
	max_time = 0
	for i in range(len(arr)):
		if arr[i] == 'A':
			current_time = 0
			last_angry = i
		elif arr[i] == 'P' and last_angry < i:
			current_time += 1
		if current_time > max_time:
			max_time = current_time
	return max_time
for _ in range(int(input())):
	n = int(input())
	print(max_time(input()))


no_of_testcases = int(input())
for i in range(no_of_testcases):
	number_of_characters = int(input())
	string = input()
	sum_now = 0
	max_sum = 0
	j = 0
	break_flag = 0
	while j < number_of_characters - 1:
		if string[j] == 'A':
			sum_now = 0
			while string[j + 1] == 'P':
				j = j + 1
				sum_now = sum_now + 1
				if j == number_of_characters - 1:
					break_flag = 1
					break
			if sum_now > max_sum:
				max_sum = sum_now
			if break_flag == 1:
				break
		j = j + 1
	print(max_sum)


def time(l, n):
	out = []
	for i in range(n):
		sub = 'AP'
		out.append(0)
		k = 0
		while sub in l[i]:
			out[i] = k + 1
			k += 1
			sub += 'P'
	return out
n = int(input())
l = []
for i in range(n):
	num = int(input())
	stud = input()
	l.append(stud)
out = time(l, n)
for i in range(len(out)):
	print(out[i])


k = int(input())
for i1 in range(k):
	l = int(input())
	s = input()
	s += 'A'
	ans = -1
	ansa = 0
	for i in s:
		if i == 'A' and ans == -1:
			ans = 0
		elif i == 'A' and ans != -1:
			ansa = max(ans, ansa)
			ans = 0
		elif i != 'A' and ans != -1:
			ans += 1
	print(ansa)


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	mx = 0
	j = -1
	for i in range(n):
		if s[i] == 'A':
			j = i + 1
			break
	if j == -1 or j == n:
		print(0)
		continue
	while j < n:
		cnt = 0
		while j < n and s[j] == 'P':
			cnt += 1
			j += 1
		mx = max(cnt, mx)
		j += 1
	print(mx)


import os
import heapq
import sys, threading
import math
import operator
from collections import defaultdict, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		ans = 0
		ct = 0
		ok = False
		for i in s:
			if i == 'A':
				ok = True
				ct = 0
			elif i == 'P' and ok:
				ct += 1
				ans = max(ct, ans)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	a = int(input())
	b = str(input())
	x = []
	for i in range(a):
		if b[i] == 'A' and i != a - 1:
			j = i + 1
			d = 0
			while j <= a - 1 and b[j] != 'A':
				d = d + 1
				j = j + 1
				x.append(d)
	if x == []:
		print(0)
	elif max(x) == 0:
		print(0)
	else:
		print(max(x))


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		idx = -1
		for i in range(n):
			if s[i] == 'A':
				idx = i
				break
		if idx == -1:
			print(0)
			continue
		ans = 0
		ls = []
		for i in range(idx, n):
			if s[i] == 'A':
				ans = 0
			else:
				ans += 1
			ls.append(ans)
		print(max(ls))
main()


tc = int(input())
for i in range(tc):
	n = int(input())
	s = input()
	count = 0
	max1 = count
	flag = 0
	for i in range(n):
		if s[i] == 'A':
			flag = 1
			count = 0
		if flag == 1 and s[i] == 'P':
			count += 1
			if max1 < count:
				max1 = count
	print(max1)


for _ in range(int(input())):
	n = int(input())
	data = input()
	count = 0
	while 'AP' in data:
		data = data.replace('AP', 'AA')
		count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	a = list(s)
	count = 0
	maxcoutn = 0
	flag = False
	for i in reversed(range(n)):
		if a[i] == 'P':
			count += 1
		elif a[i] == 'A':
			maxcoutn = max(count, maxcoutn)
			count = 0
	print(maxcoutn)


for _ in range(int(input())):
	n = int(input())
	s = input()
	A = []
	if 'A' in s:
		for i in range(n):
			if s[i] == 'A':
				A.append(i)
		A.append(n)
		maxx = 0
		for i in range(1, len(A)):
			maxx = max(maxx, A[i] - A[i - 1] - 1)
		print(maxx)
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	c = 'A'
	if c not in s:
		print(0)
	else:
		ind = s.index(c)
		s = s[ind + 1:]
		l = s.split(c)
		print(len(max(l)))


n = int(input())
for i in range(n):
	m = int(input())
	s = input()
	max_seq_len = 0
	count = 0
	angry = False
	for j in range(len(s)):
		if s[j] == 'A':
			angry = True
			max_seq_len = max(max_seq_len, count)
			count = 0
		elif angry == True:
			count += 1
	max_seq_len = max(max_seq_len, count)
	print(max_seq_len)


t = int(input())
answers = []
for ti in range(t):
	n = int(input())
	s = list(input())
	a = len(s)
	for i in range(len(s)):
		if s[i] == 'A':
			a = i
			break
	mx = 0
	curr = 0
	for i in range(a, len(s)):
		if s[i] == 'A':
			mx = max(curr, mx)
			curr = 0
			continue
		curr += 1
	mx = max(curr, mx)
	answers.append(mx)
for a in answers:
	print(a)


def rahul(word):
	return [char for char in word]
f = list()
a = int(input())
r = 0
while r < a:
	b = int(input())
	c = rahul(input())
	(s, e, d) = (0, 0, 0)
	if 'A' in c:
		while s < b:
			if s == 0:
				s = c.index('A')
			elif c[s] == 'P':
				d = d + 1
				if d > e:
					e = d
			else:
				d = 0
			s = s + 1
	f.append(e)
	r = r + 1
r = 0
while r < len(f):
	print(f[r])
	r = r + 1


for i in range(int(input())):
	n = int(input())
	s = input().split('A')
	try:
		m = max(s[1:])
		print(len(m))
	except:
		print(0)


for _ in range(int(input())):
	k = int(input())
	s = input() + 'A'
	i = s.index('A')
	(c, d) = (0, 0)
	for j in range(i + 1, k + 1):
		if s[j] == 'P':
			c += 1
		else:
			if c > d:
				d = c
			c = 0
	print(d)


import re
t = int(input())
for k in range(t):
	tem = input()
	s = input()
	arr = re.findall('AP{1,}', s)
	if len(arr) == 0:
		print(0)
	else:
		print(max([len(item) for item in arr]) - 1)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	te = 0
	if 'A' in s:
		a = s.index('A')
		for i in range(a, n):
			if s[i] == 'A':
				m = max(m, te)
				te = 0
			else:
				te += 1
		m = max(m, te)
		print(m)
	else:
		print(0)


t = int(input())
for i in range(0, t):
	n = int(input())
	p = input()
	s = []
	for i in p:
		s.append(i)
	Max = 0
	m = 0
	flag = 0
	for i in s:
		if i == 'A' and flag == 0:
			flag = 1
		if i == 'A' and flag == 1:
			if m > Max:
				Max = m
			m = 0
		elif i == 'P' and flag == 1:
			m = m + 1
	if m > Max:
		Max = m
	print(Max)


t = int(input())
for tt in range(t):
	n = int(input())
	s = input()
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			loc = 0
			for j in range(i + 1, n):
				if s[j] == 'A':
					break
				loc += 1
			ans = max(ans, loc)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	c = 0
	prev = a.copy()
	for i in range(len(a)):
		ind = []
		for j in range(len(a)):
			if a[j] == 'A':
				if j != n - 1:
					if a[j + 1] == 'P':
						ind.append(j + 1)
		for el in ind:
			a[el] = 'A'
		if prev != a:
			c += 1
			prev = a.copy()
	print(c)


t = int(input())
for i in range(t):
	l = int(input())
	s = input()
	indx = -1
	for j in range(len(s)):
		if s[j] == 'A':
			indx = j
			break
	s = s[indx:]
	sp = s.split('A')
	result = 0
	for ss in sp:
		result = max(result, len(ss))
	if indx > -1:
		print(result)
	else:
		print('0')


def g(st):
	an = []
	k = 0
	flag = True
	while flag == True:
		j = 0
		for i in range(len(st) - 1):
			if st[i] == 'A' and st[i + 1] == 'P':
				an.append(i)
				j = 1
		for i in an:
			st[i + 1] = 'A'
		k += 1
		if j == 0:
			flag = False
	return k - 1
t = int(input())
a = []
for i in range(t):
	n = int(input())
	s = input()
	st = []
	for i in s:
		st.append(i)
	a.append(g(st))
for i in a:
	print(i)


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	s = input()
	l = list(s)
	time = []
	i = 0
	while i < len(l):
		count = 0
		if l[i] == 'A':
			for j in range(i + 1, len(l)):
				if l[j] == 'P':
					count += 1
				else:
					break
			time.append(count)
		i += 1
	if len(time) == 0:
		print(0)
	else:
		print(max(time))


for _ in range(int(input())):
	length = int(input())
	s = input()
	s = [*s]
	change = True
	ans = -1
	while change:
		ans = ans + 1
		change = False
		i = 0
		while i < length - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				i = i + 1
				change = True
			i = i + 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	m = 0
	c = 0
	f = 0
	for i in s:
		if i == 'A':
			f = 1
		if f:
			if i == 'A':
				m = max(m, c)
				c = 0
			else:
				c += 1
	print(max(m, c))


I = lambda : int(input())
ID = lambda : map(int, input().split())
IL = lambda : list(ID())
t = I()
for _ in range(t):
	n = I()
	a = list(input())
	b = []
	maxi = -1
	f = True
	while f:
		f = False
		for i in range(n - 1, 0, -1):
			if a[i] == 'P' and a[i - 1] == 'A':
				f = True
				a[i] = 'A'
		maxi += 1
	print(maxi)


group = input()
students = []
PPAP = []
iteration = []
for i in range(int(group)):
	students.append(input())
	PPAP.append(input())
	iteration.append(0)
for i in range(int(group)):
	ilist = [0]
	for k in range(len(PPAP[i])):
		if k == 0:
			continue
		if PPAP[i][k] == 'A':
			ilist.append(iteration[i])
			iteration[i] = 0
		try:
			if PPAP[i][k] == 'P' and PPAP[i][k - 1] == 'A':
				s = list(PPAP[i])
				s[k] = 'A'
				PPAP[i] = ''.join(s)
				iteration[i] += 1
		except:
			pass
	if max(ilist) < iteration[i]:
		print(iteration[i])
	else:
		print(max(ilist))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	arr = [0]
	flag = 0
	idx = 0
	for c in s:
		if c == 'A':
			flag = 1
		if c == 'P' and flag == 1:
			arr[idx] += 1
		if c == 'A':
			idx += 1
			arr.append(0)
	print(max(arr))


t = int(input())
for _ in range(t):
	input()
	s = input()
	a = s.find('A')
	if a == -1:
		print(0)
	else:
		print(len(max(s[a:].split('A'), key=len)))


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	while True:
		temp = ''
		temp += s[0]
		for i in range(1, n):
			if s[i - 1] == 'A':
				temp += 'A'
			else:
				temp += s[i]
		if s == temp:
			break
		count += 1
		s = temp
	print(count)


for i in range(0, int(input())):
	n = int(input())
	try:
		arr = input().split('A')[1:]
		print(len(max(arr)))
	except:
		print(0)


n = int(input())
for i in range(n):
	s = int(input())
	a = input()
	m = 0
	c = 0
	a = a + 'A'
	t = 0
	for j in range(s + 1):
		if a[j] == 'A':
			t = 1
		if a[j] == 'P' and t == 1:
			c += 1
		else:
			if c > m:
				m = c
			c = 0
	print(m)


t = int(input())
for i in range(0, t):
	n = int(input())
	s = input()
	first = s.find('A')
	last = s.rfind('A')
	diff = []
	a = first
	if first == -1:
		print(0)
	elif s.count('A') == 1 and last == n - 1:
		print(0)
	elif s.count('A') == 1 and first == 0:
		print(n - 1)
	else:
		for l in range(0, n):
			if s[l] == 'A':
				diff.append(l - a - 1)
				a = l
		diff.append(n - last - 1)
		maximum = max(diff)
		print(maximum)


import sys
queries = int(sys.stdin.readline())

def doQueries(queries):
	for query in range(queries):
		studentsCount = int(sys.stdin.readline())
		studentsStatus = sys.stdin.readline().strip()
		posAngry = studentsStatus.find('A')
		minutes = 0
		while posAngry != -1:
			posAngryNext = studentsStatus.find('A', posAngry + 1)
			if posAngryNext != -1:
				delta = posAngryNext - posAngry - 1
			else:
				delta = studentsCount - posAngry - 1
			if delta > minutes:
				minutes = delta
			posAngry = studentsStatus.find('A', posAngry + 1)
		print(minutes)
doQueries(queries)


for _ in range(int(input())):
	n = int(input())
	s = input()
	x = []
	if s == 'A' or s == 'P':
		print(0)
	else:
		for i in range(n):
			c = 0
			if s[i] == 'A':
				for j in range(i + 1, n):
					if s[j] == 'P':
						c += 1
					else:
						break
			x.append(c)
		print(max(x))


t = int(input())
for i in range(t):
	k = int(input())
	a = input()
	ss = 0
	for j in range(k):
		s = 0
		if a[j] == 'A':
			n = j + 1
			while n < k:
				if a[n] == 'P':
					s = s + 1
					n = n + 1
				else:
					break
			ss = max(ss, s)
	print(ss)


ans = []
for _ in range(int(input())):
	n = int(input())
	s = input()
	maxx = 0
	true = 0
	count = 0
	for i in range(len(s)):
		if s[i] == 'A':
			true = 1
			count = 0
		elif s[i] == 'P' and true:
			count += 1
		if count > maxx:
			maxx = count
	ans.append(maxx)
for i in ans:
	print(i)


t = int(input())
for i in range(t):
	n = input()
	k = input().split('A')[1:]
	print(max(map(len, k), default=0))


import math
import sys
import itertools
import fractions
import re
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
for t in [wtf[i:i + 2] for i in range(1, len(wtf), 2)]:
	k = int(t[0])
	S = t[1]
	ans = max([len(s) - 1 for s in re.findall('(AP+)', S)], default=0)
	print(ans)


import sys

def tokenGen():
	(tokens, count, cur) = ([], 0, 0)
	while True:
		while cur == count:
			tokens = sys.stdin.readline().split()
			count = len(tokens)
			cur = 0
		cur += 1
		yield tokens[cur - 1]
token = tokenGen()

def nextStr():
	return next(token)

def nextInt():
	return int(nextStr())
t = nextInt()
for ti in range(t):
	ki = nextInt()
	n_peace_together_max = 0
	n_peace_together = 0
	after_angry = False
	group = nextStr()
	for pupil in group:
		if 'A' == pupil:
			after_angry = True
			n_peace_together = 0
		elif after_angry:
			n_peace_together += 1
			if n_peace_together_max < n_peace_together:
				n_peace_together_max = n_peace_together
	print(n_peace_together_max)


def find_angry(s):
	tmp = []
	n = len(s)
	for i in range(n):
		if s[i] == 'A':
			tmp.append(i)
	if len(tmp) == 0:
		return 0
	if len(tmp) >= 1:
		tmp.append(n)
	diff = [tmp[i] - tmp[i - 1] for i in range(1, len(tmp))]
	return max(diff) - 1
t = int(input())
for sub_t in range(t):
	k = int(input())
	print(find_angry(input()))


def main():
	try:
		n = int(input())
		master = list()
		for i in range(n):
			na = int(input())
			array = input()
			master.append(array)

		def assign(string, pos, value):
			str_list = [char for char in string]
			str_list[pos] = value
			return ''.join(str_list)
		for element in master:
			count = 0
			max_length = 0
			i = 0
			parse = True
			while parse == True:
				i = 0
				got_it = False
				while i < len(element) - 1:
					if element[i] == 'A' and element[i + 1] == 'P':
						element = assign(element, i + 1, 'A')
						got_it = True
						i += 2
					else:
						i += 1
				if got_it is False:
					break
				else:
					count += 1
			print(count)
	except Exception as e:
		print(e)
main()


t = int(input())
for ele in range(0, t):
	n = int(input())
	a = input()
	p = a.lstrip('P')
	r = p.split('A')
	max_len = 0
	for ele in r:
		if len(ele) > max_len:
			max_len = len(ele)
	print(max_len)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'AP' not in s:
		print(0)
		continue
	x = max(map(lambda x: len(x), s[s.index('A'):].split('A')))
	print(x)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	ss = []
	for j in s:
		ss.append(j)
	sum = 0
	f = True
	while f:
		ssn = []
		for i in ss:
			ssn.append(i)
		f = False
		sum += 1
		for j in range(len(ss) - 1):
			if ss[j] == 'A' and ss[j + 1] != 'A':
				ssn[j + 1] = 'A'
				f = True
		ss = ssn
	print(sum - 1)


for nt in range(int(input())):
	n = int(input())
	s = input()
	counts = []
	count = 0
	ind = s.find('A')
	if ind == -1:
		print(0)
		continue
	for i in range(ind, n):
		if s[i] == 'A':
			counts.append(count)
			count = 0
		else:
			count += 1
	counts.append(count)
	print(max(counts))


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	ans = 0
	flag = 0
	for j in range(k):
		if s[j] == 'A':
			flag = 1
			cur = 0
		elif flag == 1:
			cur += 1
			ans = max(ans, cur)
	print(ans)


def solve(s):
	a = [0 for i in s]
	i = 0
	while i < len(s) and s[i] == 'P':
		i += 1
	for i in range(i, len(s)):
		a[i] = 0 if s[i] == 'A' else a[i - 1] + 1
	return max(a)
t = int(input())
for i in range(t):
	input()
	print(solve(input()))


t = int(input())
for ti in range(t):
	ki = int(input())
	si = input()
	gap = 0
	maxm = 0
	yes = 0
	for st in si:
		if st == 'A':
			yes = 1
			if gap > maxm:
				maxm = gap
			gap = 0
		elif yes == 1:
			gap += 1
	if gap > maxm:
		maxm = gap
	print(maxm)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	x = 0
	changed = True
	while changed:
		changed = False
		for i in reversed(range(len(s) - 1)):
			if s[i] == 'A':
				if s[i + 1] == 'P':
					changed = True
				s[i + 1] = 'A'
		if changed:
			x += 1
	print(x)


def main():
	for _ in range(int(input())):
		n = int(input())
		x = [i for i in input()]
		minute = 0
		counted = True
		while counted:
			counted = False
			for i in range(n - 1, 0, -1):
				if x[i] == 'P' and x[i - 1] == 'A':
					x[i] = 'A'
					counted = True
			if counted:
				minute += 1
		print(minute)
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = input()
	l = l[::-1]
	ct = 0
	ans = 0
	for x in l:
		if x == 'A':
			ans = max(ans, ct)
			ct = 0
		else:
			ct += 1
	print(ans)


from sys import stdin
t = int(stdin.readline().strip())
for _ in range(t):
	stdin.readline()
	s = stdin.readline().strip().lower()
	res = 0
	counter = 0
	met = False
	for (i, e) in enumerate(s):
		if e == 'a':
			met = True
			res = max(res, counter)
			counter = 0
		elif met:
			counter += 1
	if counter:
		res = max(res, counter)
	print(res)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	dist = [0]
	for i in range(len(s)):
		if s[i] == 'A':
			for j in range(i + 1, len(s)):
				if s[j] == 'A':
					dist.append(j - i - 1)
					break
				if j == len(s) - 1:
					dist.append(j - i)
					break
	print(max(dist))


for _ in range(int(input())):
	n = int(input())
	arr = input()
	flag = 0
	f = 0
	c = 0
	for i in range(n):
		if arr[i] == 'A':
			flag = 1
			c = 0
		if arr[i] == 'P' and flag == 1:
			c += 1
		if c > f:
			f = c
	print(f)


from sys import *
from math import *
from bisect import *
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	m = 0
	x = []
	f = 0
	for i in range(n):
		if s[i] == 'A':
			x.append(m)
			m = 0
			f = 1
		elif f == 1:
			m += 1
	x.append(m)
	print(max(x))


import sys
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
t = ii()
for _ in range(t):
	n = ii()
	d = [x for x in input()]
	f = 1
	r = 0
	while True:
		f = 0
		i = 0
		while i < n - 1:
			if d[i] == 'A' and d[i + 1] == 'P':
				d[i + 1] = 'A'
				i += 2
				f = 1
			else:
				i += 1
		if not f:
			break
		r += 1
	print(r)


def solve():
	n = int(input())
	s = str(input())
	s = s.lstrip('P')
	ans = max(map(lambda x: len(x), s.split('A')))
	print(ans)
t = int(input())
for _ in range(t):
	solve()


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, K):
	indices = []
	for (i, x) in enumerate(K):
		if x == 'A':
			indices.append(i)
	if not indices:
		return 0
	indices.append(N)
	best = 0
	for (x, y) in zip(indices, indices[1:]):
		best = max(best, y - x)
	return best - 1
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	K = input().decode().rstrip()
	ans = solve(N, K)
	print(ans)


in1 = int(input())
for _ in range(in1):
	in2 = int(input())
	in3 = input()
	ans = 0
	for i in range(in2):
		temp = 0
		if in3[i] == 'A':
			i += 1
			while i < in2 and in3[i] == 'P':
				temp += 1
				i += 1
		ans = max(ans, temp)
	print(ans)


for _ in range(int(input())):
	k = int(input())
	s = list(input())
	flag = count = 0
	arr = []
	for i in range(k):
		if s[i] == 'A':
			flag = 1
			arr.append(count)
			count = 0
		if s[i] == 'P' and flag == 1:
			count += 1
		arr.append(count)
	print(max(arr))


import sys
for t in sys.stdin:
	t = int(t.strip())
	count_list = []
	while t:
		n = sys.stdin.readline()
		n = int(n.strip())
		stu_str = sys.stdin.readline()
		found_A = False
		max_count = 0
		sub_count = 0
		for i in range(n):
			if stu_str[i] == 'A':
				found_A = True
				if sub_count > max_count:
					max_count = sub_count
				sub_count = 0
			elif stu_str[i] == 'P' and found_A:
				sub_count += 1
		if sub_count > max_count:
			max_count = sub_count
		count_list.append(max_count)
		t = t - 1
	for count in count_list:
		print(count)


def solve(n, status):
	pstats = status.split('A')
	if status[0] != 'A':
		pstats.pop(0)
	if len(pstats) == 0:
		return 0
	return len(max(pstats))
numTestCase = int(input())
for i in range(numTestCase):
	n = int(input())
	status = str(input())
	print(solve(n, status))


import sys
from operator import itemgetter
input = sys.stdin.readline
from collections import defaultdict as dc
from bisect import bisect_left, bisect_right
import math
for _ in range(int(input())):
	n = int(input())
	l = list(input())
	for i in range(n):
		if l[i] == 'A':
			break
	m = 0
	c = 0
	for j in range(i + 1, n):
		if l[j] == 'P':
			c += 1
		else:
			m = max(m, c)
			c = 0
	m = max(m, c)
	print(m)


t = int(input())
for i in range(t):
	l = int(input())
	s = input()
	c = 0
	if s.count('A') > 0:
		s1 = s[0]
		while s.rfind('P') > s.find('A'):
			for j in range(len(s) - 1):
				if s[j] == 'A':
					s1 = s1 + 'A'
				else:
					s1 = s1 + s[j + 1]
			c = c + 1
			s = s1
			s1 = s[0]
	print(c)


import sys
Input = sys.stdin.readline
for i in range(int(input())):
	input()
	string = input()
	if 'A' not in string:
		print(0)
	else:
		(tmp, tmp2) = (0, 0)
		x = string[string.index('A'):]
		for j in range(len(x)):
			if x[j] == 'P':
				tmp += 1
			else:
				tmp2 = max(tmp, tmp2)
				tmp = 0
		tmp2 = max(tmp, tmp2)
		print(tmp2)


import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	k = Read.int()
	a = input()
	s = False
	count = 0
	local_count = 0
	for i in a:
		if i == 'A':
			s = True
			if local_count > count:
				count = local_count
			local_count = 0
		elif s:
			local_count += 1
	if s and local_count > count:
		count = local_count
	print(count)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


def countP(s, ind, n):
	cnt = 0
	while ind < n and s[ind] == 'P':
		ind += 1
		cnt += 1
	return cnt

def solve(a, n):
	if n == 1:
		return 0
	t = 0
	for i in range(n):
		if s[i] == 'A':
			t = max(t, countP(s, i + 1, n))
	return t
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(s, n))


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	(ans, angry_idx) = (0, None)
	for (i, ch) in enumerate(s):
		if ch == 'P':
			if angry_idx is not None:
				ans = max(ans, i - angry_idx)
		else:
			angry_idx = i
	print(ans)


for x in range(int(input())):
	n = int(input())
	s = input()
	k = 0
	i = 0
	while i < n:
		if s[i] == 'A':
			l = 0
			p = i + 1
			while p < n:
				if s[p] == 'P':
					p += 1
					l += 1
				else:
					break
			i = p
			k = max(k, l)
		else:
			i += 1
	print(k)


for s in [*open(0)][2::2]:
	s = [*s.strip()]
	cnt = 0
	while True:
		ok = False
		for i in range(len(s) - 2, -1, -1):
			if s[i] == 'A' and s[i + 1] == 'P':
				ok = True
				s[i + 1] = 'A'
		if not ok:
			break
		cnt += 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = list(s)
	ans = 0
	count = 0
	f = 0
	if s[-1] != 'A':
		s.append('A')
	for i in range(0, len(s)):
		if s[i] == 'A' and f == 0:
			f = 1
		elif s[i] == 'A' and f == 1:
			ans = max(ans, count)
			count = 0
		elif f == 1:
			count += 1
	print(ans)


s1 = input().split()
T = int(s1[0])
for case in range(0, T):
	s2 = input().split()
	n = int(s2[0])
	a = input().split()
	s = []
	for i in range(0, n):
		s.append(a[0][i])
	ss = 0
	ans = 0
	for i in range(0, n):
		if s[i] == 'A':
			for j in range(i + 1, n):
				if s[j] == 'P':
					ss += 1
				else:
					break
			ans = max(ans, ss)
		else:
			ss = 0
	print(ans)


import sys
import math
from collections import Counter
import bisect
ma = lambda : map(int, input().split())
inp = lambda : [int(i) for i in input().split()]
s = sorted

def con(A):
	strr = ''
	for i in A:
		strr += i
	return strr
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	for i in range(n):
		if s[i] == 'A':
			new = s[i:]
			break
	if 'A' not in s:
		print(0)
	else:
		new = con(new)
		new = new.split('A')
		maxi = 0
		for i in range(len(new)):
			maxi = max(maxi, len(new[i]))
		print(maxi)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	t = 0
	for i in range(n):
		if s[i] == 'A':
			t = 0
			for j in range(i + 1, n):
				if s[j] == 'P':
					t += 1
				else:
					break
		ans = max(ans, t)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	g = input()
	cnt = 0
	ans = 0
	begin = False
	for i in range(n):
		if i < n - 1 and g[i] == 'A' and (g[i + 1] == 'P'):
			begin = True
			cnt = 0
		if begin and g[i] == 'P':
			cnt += 1
			if i < n - 1 and g[i + 1] == 'A':
				begin = False
		ans = max(ans, cnt)
	print(ans)


for _ in range(int(input())):
	k = int(input())
	s = input()
	arr = []
	count = 0
	for i in range(len(s)):
		if s[i] == 'A':
			arr.append(count)
			count = 0
		else:
			count += 1
	arr.append(count)
	if len(arr) <= 1:
		print(0)
	else:
		print(max(arr[1:]))


import collections

def calcTime(A):
	angry = collections.deque()
	for i in range(len(A)):
		if i + 1 < len(A) and A[i] == 'A' and (A[i + 1] == 'P'):
			angry.append(i)
	t = 0
	while angry:
		a_len = len(angry)
		t += 1
		for _ in range(a_len):
			i = angry.popleft()
			if i + 2 < len(A) and A[i + 2] == 'P':
				angry.append(i + 1)
	return t
N = int(input())
inputs = []
for _ in range(N):
	inputs.append([int(input()), input()])
for case in inputs:
	print(calcTime(case[1]))


from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint
sys.setrecursionlimit(10 ** 8)
mod = 10 ** 9 + 7
mod2 = 998244353
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpln(n):
	return list((int(sys.stdin.readline()) for i in range(n)))
t = inp()
res = [0] * t
for i in range(t):
	n = inp()
	s = input()
	f = False
	cnt = 0
	for j in range(n):
		if s[j] == 'A':
			if f:
				res[i] = max(res[i], cnt)
				cnt = 0
			else:
				f = True
		elif f:
			cnt += 1
	res[i] = max(res[i], cnt)
for score in res:
	print(score)


t = int(input())
people = []
for i in range(2 * t):
	people.append(input())
for i in range(1, len(people), 2):
	if people[i][0] != 'P':
		print(len(max(people[i].split('A'))))
	elif people[i].count('A') == 0:
		print(0)
	else:
		b = 0
		while people[i][b] != 'A':
			b += 1
		print(len(max(people[i][b + 1:].split('A'))))


for t in range(int(input())):
	k = int(input())
	s = input()
	ans = 0
	left_A = -1
	while True:
		pos = s.find('A', left_A + 1)
		if pos == -1:
			break
		if left_A != -1:
			ans = max(ans, pos - 1 - left_A)
		left_A = pos
	if left_A != -1:
		ans = max(ans, len(s) - 1 - left_A)
	print(ans)


from math import ceil, log, floor, sqrt
import math
k = 1

def mod_expo(n, p, m):
	result = 1
	while p != 0:
		if p % 2 == 1:
			result = result * n % m
		p //= 2
		n = n * n % m
	return result

def is_prime(n):
	m = 2
	while m * m <= n:
		if n % m == 0:
			return False
		m += 1
	return True

def find_sum(n, a):
	a.insert(0, 0)
	for i in range(1, n + 1):
		prev = a[i] & a[i - 1]
		cur = a[i] | a[i - 1]
		a[i - 1] = prev
		a[i] = cur
	return sum((m * m for m in a))

def prin_abc(x, y, z):
	l = [x, y, z]
	l.sort()
	if l[1] < l[2]:
		print('NO')
		return
	a = b = l[0]
	c = l[2]
	print('YES')
	print(a, b, c)

def get_scores(n, vals):
	l = 0
	r = n - 1
	turn = 0
	sereja = 0
	dima = 0
	while l <= r:
		if vals[l] > vals[r]:
			cur = vals[l]
			l += 1
		else:
			cur = vals[r]
			r -= 1
		if turn == 0:
			sereja += cur
		else:
			dima += cur
		turn = not turn
	print(sereja, dima)

def count_lamps(n, m):
	return (n * m + 1) // 2

def find_holidays(n):
	cnt = 2 * (n // 7)
	rem = n % 7
	mx = mn = cnt
	if rem == 1:
		mx += 1
	elif rem > 1:
		mx += 2
	if rem == 6:
		mn += 1
	print(mn, mx)

def get_time(n, line):
	mx = 0
	i = 0
	while i < n:
		while i < n and line[i] == 'P':
			i += 1
		if i < n and line[i] == 'A':
			i += 1
			cnt = 0
			while i < n and line[i] == 'P':
				i += 1
				cnt += 1
			mx = max(mx, cnt)
		else:
			return mx
	return mx
t = 1
t = int(input())
while t:
	t = t - 1
	(k, g) = (0, 0)
	points = []
	n = int(input())
	a = input()
	print(get_time(n, a))


o = int(input())
for _ in range(o):
	n = int(input())
	l = [*input()]
	res = 0
	test = 0
	c = 0
	for i in range(n):
		if l[i] == 'A':
			test = 1
		if test == 1:
			if l[i] == 'P':
				c += 1
			else:
				res = max(c, res)
				c = 0
	res = max(res, c)
	print(res)


t = int(input())
for i in range(t):
	l = int(input())
	aa = input()
	s = aa.split('A')
	maxx = 0
	for i in range(1, len(s)):
		if len(s[i]) > maxx:
			maxx = len(s[i])
	print(maxx)


n = int(input())
for t in range(n):
	input()
	s = input()
	m = 0
	i = len(s) - 1
	while i >= 0:
		if s[i] == 'P':
			j = i - 1
			while j >= 0:
				if s[j] == 'A':
					if m < i - j:
						m = i - j
					break
				j -= 1
			i = j
		i -= 1
	print(m)


for i in range(int(input())):
	ln = int(input())
	s = input()
	(m, timax) = (0, 0)
	for j in range(s.find('A'), ln):
		if s[j] == 'P':
			timax += 1
		else:
			m = timax if timax > m else m
			timax = 0
	print(max(timax, m) if s.find('A') >= 0 else 0)


t = int(input())
for i in range(t):
	input()
	s = input()
	try:
		s = s[s.index('A'):]
	except:
		print(0)
		continue
	(n, m, a) = (0, 0, True)
	for x in s:
		if a:
			if x == 'P':
				n -= -1
			else:
				a = False
				if n > m:
					m = n
		elif x == 'P':
			a = True
			n = 1
	print(max(n, m))


for j in range(int(input())):
	n = int(input())
	st1 = input()
	a2 = []
	for i in range(n):
		count = 0
		if st1[i] == 'A':
			if i == n - 1:
				break
			while st1[i + 1] == 'P':
				count += 1
				i += 1
				if i == n - 1:
					break
			a2.append(count)
	a2.append(0)
	print(max(a2))


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	if s.count('P') == len(s):
		print(0)
	else:
		while s[0] == 'P':
			s.pop(0)
		cnt = 0
		ma = 0
		for i in range(len(s)):
			if s[i] == 'P':
				cnt += 1
				if cnt > ma:
					ma = cnt
			else:
				cnt = 0
		print(ma)


t = int(input())
for z in range(t):
	k = int(input())
	s = list(input())
	s.append('A')
	nb = -1
	ok = True
	while ok:
		nb += 1
		ok = False
		i = k - 1
		while i >= 0:
			if s[i] == 'A':
				if s[i + 1] == 'P':
					s[i + 1] = 'A'
					ok = True
			i -= 1
	print(nb)


for i in [0] * int(input()):
	input()
	ct = 0
	ct2 = 0
	a = input()
	if a.find('AP') + 1:
		for x in a[a.find('AP') + 1:]:
			if x == 'P':
				ct += 1
			else:
				ct2 = max(ct, ct2)
				ct = 0
	print(max(ct, ct2))


for test in range(int(input())):
	_ = input()
	l = list(input())
	maxl = 0
	i = 0
	while i < len(l) - 1:
		if l[i] == 'A' and l[i + 1] == 'P':
			starti = i + 1
			c = 0
			while starti < len(l) and l[starti] == 'P':
				starti += 1
				i += 1
				c += 1
			if c > maxl:
				maxl = c
			i -= 1
		i += 1
	print(maxl)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	i = 0
	while i < n:
		if s[i] == 'A':
			break
		i += 1
	if i == n - 1:
		print(0)
	else:
		count = []
		c = 0
		while i < n:
			if s[i] == 'A':
				count.append(c)
				c = 0
			else:
				c += 1
			i += 1
		count.append(c)
		print(max(count))


for t in range(int(input())):
	k = int(input())
	s = input()
	ans = 0
	left_A = -1
	for (idx, val) in enumerate(s):
		if val == 'A':
			if left_A != -1:
				ans = max(ans, idx - 1 - left_A)
			left_A = idx
	if left_A != -1:
		ans = max(ans, len(s) - 1 - left_A)
	print(ans)


req = int(input())
for z in range(req):
	amount = int(input())
	s = list(input())
	time = 0
	next = True
	while next:
		i = 0
		next = False
		while i < amount - 1:
			if s[i] == 'A' and s[i + 1] != 'A':
				s[i + 1] = 'A'
				i += 2
				next = True
			else:
				i += 1
		time += 1
	print(time - 1)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ss = s.split('A')
	l = [len(x) for x in ss[1:]]
	if s.count('A') == 0:
		print(0)
	else:
		print(max(l))


for i in range(int(input())):
	n = int(input())
	l = list(str(input()))
	k = []
	ans = 0
	while ''.join(l).count('AP') > 0:
		for j in range(n - 1):
			if l[j] == 'A' and l[j + 1] == 'P':
				k.append(j + 1)
		for lm in k:
			l[lm] = 'A'
		ans += 1
	print(ans)


T = int(input().strip())
debug = False
for t in range(T):
	N = int(input().strip())
	S = str(input().strip())
	maxS = 0
	count = 0
	localS = 0
	for c in S:
		if c == 'A':
			if localS > maxS:
				maxS = localS
			localS = 0
			count = 1
		if c == 'P':
			if count > 0:
				localS += 1
	if localS > maxS:
		maxS = localS
	print(maxS)


q = int(input())
for T in range(q):
	k = int(input())
	S = input()
	start = False
	max_count = 0
	count = 0
	for i in range(len(S)):
		if S[i] == 'A':
			start = True
			max_count = max(count, max_count)
			count = 0
		elif start:
			count += 1
	max_count = max(count, max_count)
	print(max_count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	curr = None
	ans = 0
	for i in s:
		if i == 'A':
			curr = 0
		elif curr != None:
			curr += 1
			ans = max(ans, curr)
	print(ans)


n = int(input())
for i in range(n):
	num = int(input())
	st = input()
	res = 0
	le = len(st)
	tag = 0
	while tag < le:
		if st[tag] == 'A':
			while tag + 1 < le and st[tag + 1] == 'A':
				tag += 1
			ed = tag + 1
			while ed < num and st[ed] == 'P':
				ed += 1
			res = max(res, ed - tag - 1)
			tag = ed
		else:
			tag += 1
	print(res)


n_total = int(input())
for _ in range(n_total):
	m = input()
	n = input()
	f = 0
	mx = 0
	num = 0
	for i in n:
		if i == 'A' and f == 0:
			f = 1
			continue
		if f == 1 and i == 'P':
			num += 1
		if f == 1 and i == 'A':
			num = 0
			if num > mx:
				mx = num
		if num > mx:
			mx = num
	print(mx)


t = int(input())
while t:
	n = int(input())
	s = input()
	s1 = list(s)
	m = 0
	l = []
	for i in range(len(s1)):
		if s1[i] == 'A':
			l.append(i)
	l.append(n)
	if len(l) == 1:
		print(0)
	else:
		for i in range(1, len(l)):
			m = max(m, l[i] - l[i - 1])
		print(m - 1)
	t = t - 1


t = int(input())
for _ in range(t):
	n = int(input())
	p = input()
	c = 0
	s = []
	v = []
	for i in range(n):
		s.append(p[i])
	s.append('A')
	for i in range(len(s)):
		if s[i] == 'A':
			c += 1
			if c == 1:
				pos = i
	if c == 1 and pos == n:
		print(0)
	else:
		p = 0
		c = 0
		for i in range(len(s)):
			if s[i] == 'A':
				c += 1
				if c == 1:
					p = i
				else:
					v.append(i - p - 1)
					p = i
		print(max(v))


q = int(input())
for _ in range(q):
	n = int(input())
	s = input()
	if s[0] == 'P':
		s = s.lstrip('P')
	s = s.split('A')
	s = [x for x in s if x != '']
	if len(s) != 0:
		print(len(max(s)))
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	maxA = 0
	firstA = False
	curr = 0
	for i in range(n):
		if s[i] == 'A':
			firstA = True
			maxA = max(curr, maxA)
			curr = 0
		if s[i] == 'P' and firstA:
			curr += 1
	maxA = max(maxA, curr)
	print(maxA)


get_int = lambda : int(input())
get_mul_int = lambda : map(int, input().rstrip().split())
get_list = lambda : list(map(int, input().rstrip().split()))
TEST_CASES = True

def main():
	n = get_int()
	s = input()
	(l, m) = ([], 0)
	if s == 'P' * n or s == 'A' * n:
		print(0)
		return
	for i in range(n):
		if s[i] == 'A':
			l.append(i)
	for i in range(len(l) - 1):
		m = max(m, l[i + 1] - l[i] - 1)
	m = max(m, n - l[-1] - 1)
	print(m)
if not TEST_CASES:
	main()
else:
	[main() for _ in range(int(input()))]


def get_time(s):
	s = list(s)
	(res, ch) = (0, 1)
	while ch != 0:
		ch = min(0, ch)
		i = 0
		while i < len(s) - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				i += 2
				ch += 1
			else:
				i += 1
		if ch != 0:
			res += 1
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(get_time(s))


cases = int(input())
while cases:
	cases -= 1
	num = input()
	s = input()
	mx = 0
	sm = 0
	start = False
	for i in s:
		if i == 'A':
			start = True
			sm = 0
		elif start:
			sm += 1
			mx = max(sm, mx)
	print(mx)


try:
	for _ in range(int(input())):
		k = int(input())
		s = input()
		l = []
		n = []
		for i in range(k):
			l.append(s[i])
		for i in range(k):
			if l[i] == 'A':
				d = 0
				for j in range(i + 1, k):
					if l[j] == 'P':
						d += 1
						n.append(d)
					else:
						n.append(d)
						break
			else:
				pass
		n.append(0)
		print(max(n))
except:
	pass


for _ in range(int(input())):
	input()
	print(max((len(x) for x in input().lstrip('P').split('A'))))


for _ in range(int(input())):
	N = int(input())
	A = input()
	val = 0
	temp = 0
	for i in range(len(A) - 1, -1, -1):
		if A[i] == 'A':
			val = max(temp, val)
			temp = 0
		else:
			temp += 1
	print(val)


for _ in range(int(input())):
	l = int(input())
	s = input()
	a = list(s.split('A'))
	if s[0] == 'A':
		print(len(max(a)))
	elif s.count('A') == 0:
		print('0')
	else:
		a.remove(a[0])
		print(len(max(a)))


T = int(input())
for t in range(T):
	n = int(input())
	s = input()
	res = 0
	last_a = n
	for i in range(n):
		if s[i] == 'A':
			last_a = i
		else:
			res = max(res, i - last_a)
	print(res)


def solve(in_str):
	mask = 0
	for c in in_str:
		mask = mask * 2 + (1 if c == 'A' else 0)
	prev = mask
	ans = 0
	while True:
		mask |= mask >> 1
		if prev == mask:
			return ans
		prev = mask
		ans += 1
samples = int(input())
for i in range(samples):
	(_, seq) = (input(), input())
	print(solve(seq))


n = int(input())
for i in range(n):
	k = int(input())
	st = str(input())
	f = 0
	time = 0
	t1 = 0
	for j in st:
		if j == 'A':
			f = 1
			if t1 > time:
				time = t1
			t1 = 0
		if f == 1 and j == 'P':
			t1 += 1
	if t1 > time:
		time = t1
	print(time)


def solve():
	N = int(input())
	L = [1 if v == 'A' else 0 for v in input()]
	Q = []
	for (i, v) in enumerate(L):
		if v == 1:
			Q.append(i)
	cnt = 0
	while Q:
		update = False
		tmp = []
		for q in Q:
			if q + 1 < N and L[q + 1] == 0:
				L[q + 1] = 1
				tmp.append(q + 1)
				update = True
		Q = tmp
		if update:
			cnt += 1
	print(cnt)
N = int(input())
for _ in range(N):
	solve()


import collections

def count_stud(students):
	students_arr = [''] * len(students)
	for i in range(len(students)):
		students_arr[i] = students[i]
	count = 0
	foundAngry = True
	while foundAngry:
		foundAngry = False
		for i in range(len(students_arr) - 1, 0, -1):
			if students_arr[i] == 'P':
				if students_arr[i - 1] == 'A':
					students_arr[i] = 'A'
					foundAngry = True
		if foundAngry:
			count += 1
	return count
t = int(input())
for i in range(t):
	num_students = int(input())
	students = input()
	ans = count_stud(students)
	print(ans)


t = input()
t = int(t)
for i in range(t):
	k = input()
	k = int(k)
	s = input()
	res = 0
	s = list(s)
	flag = True
	while flag is True:
		flag = False
		j = 0
		while j < k - 1:
			if s[j] == 'A' and s[j + 1] != 'A':
				s[j + 1] = 'A'
				flag = True
				j += 2
			else:
				j += 1
		if flag is True:
			res += 1
	print(res)


import sys
input = sys.stdin.readline
t = int(input())

def get_second(n, s):
	ss = ''
	for i in range(n):
		if s[i] == 'A':
			ss = s[i + 1:]
			break
	lstp = list(map(lambda x: len(x), ss.split('A')))
	return max(lstp)
for i in range(t):
	n = int(input())
	s = input().rstrip()
	print(get_second(n, s))


t = int(input())
for i in range(t):
	k = int(input())
	s = str(input())
	s += 'A'
	f = False
	p = 0
	now = 0
	for i in s:
		if i == 'A':
			f = True
		if i == 'P' and f:
			now += 1
		else:
			p = max(now, p)
			now = 0
	print(p)


def get_time(group):
	max_time = 0
	last = 0
	angry = False
	for s in group:
		if s == 'A':
			angry = True
			if max_time < last:
				max_time = last
			last = 0
		elif angry and s == 'P':
			last += 1
	return max(max_time, last)

def main():
	t = int(input())
	for i in range(t):
		k = int(input())
		group = input()
		print(get_time(group))
main()


def main():
	t = int(input())
	for i in range(t):
		ki = int(input())
		si = input() + 'A'
		groups = list(map(lambda x: len(x), si.split('A')[1:]))
		if groups == []:
			print(0)
			break
		print(max(groups))
main()


t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	i = 0
	max_cnt = 0
	while i < n - 1:
		cur_s = s[i]
		next_s = s[i + 1]
		if cur_s == 'A' and next_s == 'P':
			cnt = 0
			i += 1
			while i < n and s[i] == 'P':
				cnt += 1
				i += 1
			if cnt > max_cnt:
				max_cnt = cnt
		else:
			i += 1
	print(max_cnt)


def angry_studs(n, lst):
	tmp_move = 0
	min_move = 0
	for i in range(len(lst) - 1):
		tmp_move = 0
		j = 0
		while j < len(lst) - 1:
			if lst[j] == 'A' and lst[j + 1] == 'P':
				lst[j + 1] = 'A'
				j += 2
				tmp_move += 1
			else:
				j += 1
		if tmp_move >= 1:
			min_move += 1
	print(min_move)
m = int(input())
for i in range(m):
	n = int(input())
	s = input()[:n]
	lst = []
	for j in range(len(s)):
		lst.append(s[j])
	angry_studs(n, lst)


t = int(input())
while t > 0:
	t -= 1
	no_of_students = int(input())
	students = input()
	for (index, value) in enumerate(students):
		if value == 'A':
			break
	count = 0
	max = 0
	for i in range(index + 1, no_of_students):
		if students[i] == 'A':
			if count > max:
				max = count
				count = 0
			count = 0
		else:
			count += 1
	if count > max:
		max = count
	print(max)


for i in range(int(input())):
	n = int(input())
	s = input()
	ind = []
	ans = 0
	for i in range(n):
		if s[i] == 'A' and len(ind) > 0:
			ans = max(ans, i - ind[-1] - 1)
			ind.append(i)
		elif s[i] == 'A':
			ind.append(i)
	if len(ind) > 0:
		ans = max(ans, n - ind[-1] - 1)
	print(ans)


t = int(input())
l2 = []
for i in range(t):
	n = int(input())
	s = input()
	s1 = s[::-1]
	count = 0
	l = []
	for i in s1:
		if i == 'P':
			count += 1
			if 'A' not in s1:
				count = 0
				l2.append(count)
				break
		elif i == 'A':
			l.append(count)
			count = 0
	if l:
		l2.append(max(l))
print(*l2, sep='\n')


t = int(input())
groups = []
for i in range(t):
	longest = 0
	cur = 0
	started = False
	l = int(input())
	for k in input():
		if k == 'A':
			started = True
			longest = max(longest, cur)
			cur = 0
		elif started:
			cur += 1
	longest = max(longest, cur)
	print(longest)


t = int(input())
for test in range(t):
	n = int(input())
	s = input()
	max = 0
	for i in range(len(s)):
		if s[i] == 'A':
			x = 0
			if i + 1 < len(s):
				for j in range(i + 1, len(s)):
					if s[j] == 'P':
						x = x + 1
					else:
						break
				if x > max:
					max = x
				i = j
	print(max)


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()

def sieve(x):
	a = [True] * (x + 1)
	sq = floor(sqrt(x))
	for i in range(3, sq + 1, 2):
		if a[i]:
			for j in range(i * i, x + 1, i):
				a[j] = False
	if x > 1:
		p = [2]
	else:
		p = []
	for i in range(3, x + 1, 2):
		if a[i]:
			p.append(i)
	return p
for _ in range(ii()):
	n = ii()
	a = lsi()
	ans = 0
	ind = -1
	for i in range(n):
		if a[i] == 'A':
			ind = i + 1
			break
	mx = 0
	if ind == -1:
		print(0)
		continue
	for i in range(ind, n):
		if a[i] == 'P':
			mx += 1
		else:
			ans = max(ans, mx)
			mx = 0
	ans = max(mx, ans)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = input()
	t = []
	for i in range(n):
		if l[i] == 'A':
			t.append(i)
	t.append(n)
	m = 0
	for i in range(1, len(t)):
		m = max(m, t[i] - t[i - 1] - 1)
	print(m)


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = s.split('A')
	ans = 0
	if len(s) >= 2:
		s = s[1:]
		t = 0
		for i in s:
			if len(i) > ans:
				ans = len(i)
	print(ans)


t = int(input())
for i in range(0, t):
	k = int(input())
	s = str(input())
	a = []
	b = []
	if s[k - 1] != 'A':
		s = s + 'A'
	for j in range(0, len(s)):
		if s[j] == 'A':
			a.append(j)
	for j in range(0, len(a) - 1):
		b.append(a[j + 1] - a[j] - 1)
	if len(b) == 0:
		print(0)
	else:
		print(max(b))


import sys, math

def main():
	t = int(input())
	for _ in range(t):
		input()
		s = input()
		if 'A' in s:
			print(max([len(e) for e in s.split('A')[1:]]))
		else:
			print(0)
main()


t = int(input())
out = []
for i in range(t):
	k = int(input())
	stu = input()
	if stu.count('A') == 0:
		out.append(0)
		continue
	j = stu.index('A')
	check = 0
	kq = [0]
	while j < k:
		if stu[j] != 'A':
			check += 1
		else:
			if check != 0:
				kq.append(check)
			check = 0
		j += 1
	kq.append(check)
	out.append(max(kq))
for i in range(t):
	print(out[i])


t = int(input())
for o in range(t):
	n = int(input())
	a = input()
	h = a.find('A')
	if h == -1:
		print(0)
	else:
		print(max(list(map(len, a[h:].split('A')))))


test = int(input())
for i in range(test):
	n = int(input())
	s = input()
	count = 0
	maximum = 0
	flag = False
	for j in s:
		if j == 'A':
			if count > maximum:
				maximum = count
			count = 0
			flag = True
		if j == 'P' and flag:
			count += 1
	if count > maximum:
		maximum = count
	print(maximum)


t = int(input())
for i in range(t):
	k = int(input())
	string = input()
	ans = 0
	flag = False
	for (i, elem) in enumerate(string):
		if flag:
			if elem != 'A' and i == k - 1:
				if ans < abs(i - prev):
					ans = abs(i - prev)
			elif elem == 'A':
				if ans < abs(i - prev) - 1:
					ans = abs(i - prev) - 1
				prev = i
		elif elem == 'A':
			prev = i
			flag = True
	print(ans)


for t in range(int(input())):
	n = int(input())
	lt = input().split('A')[1:]
	lt = sorted(lt, key=lambda x: len(x))
	if len(lt) != 0:
		print(len(lt[-1]))
	else:
		print(0)


for i in range(int(input())):
	v = []
	ans = 0
	re = 0
	n = int(input())
	s = input() + 'A'
	for i in range(len(s)):
		if s[i] == 'A':
			v.append(i)
	for i in range(len(v) - 1):
		ans = max(ans, v[i + 1] - v[i])
	if len(v) == 1 and s[0] == 'P':
		print('0')
	else:
		print(ans - 1)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	li = []
	zi = []
	for i in range(n):
		if s[i] == 'A':
			li.append(i)
	if len(li) == 0:
		print(0)
	else:
		for i in range(1, len(li)):
			zi.append(li[i] - li[i - 1])
		zi.append(n - li[-1])
		zi = list(map(lambda i: i - 1, zi))
		print(max(zi))


t = int(input())
while t > 0:
	len_seq = int(input())
	seq_of_stud = input()
	splt = seq_of_stud.lstrip('^[P]').split('A')
	d = {}
	for i in range(len(splt)):
		d[i] = len(str(splt[i]))
	ans = max(d.values())
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	s = list(s)
	d = set()
	for i in range(n):
		if s[i] == 'A':
			d.add(i)
	ans = 0
	while True:
		flag = 0
		temp = set()
		for x in d:
			if x + 1 < n and s[x + 1] == 'P':
				s[x + 1] = 'A'
				flag = 1
				temp.add(x + 1)
		if flag == 0:
			break
		for x in temp:
			d.add(x)
		ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	for i in range(n):
		if s[i] == 'A':
			c = 0
			for j in range(i + 1, n):
				if s[j] == 'P':
					c += 1
				else:
					break
			if c > m:
				m = c
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	st = input()
	maxi = 0
	z = 0
	f = 0
	for i in st:
		if i == 'A':
			f = 1
			if z > maxi:
				maxi = z
			z = 0
		elif i == 'P' and f != 0:
			z += 1
	if z > maxi:
		maxi = z
	print(maxi)


t = int(input())
for i in range(t):
	n = int(input())
	s = input() + 'A'
	count = 0
	x = s.index('A')
	while x < n:
		x1 = s[x + 1:].index('A')
		count = max(count, x1)
		x += x1 + 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	i = s.find('A')
	if i == -1:
		print(0)
	else:
		l = [p for p in s[i:].split('A')]
		l1 = [len(i) for i in l]
		print(max(l1))


t = int(input())
ans = []

def isElseGame(s):
	for i in range(len(s) - 1):
		if s[i] == 'A' and s[i + 1] == 'P':
			return True
	return False
for i in range(t):
	k = int(input())
	s = input()
	count = 0
	while isElseGame(s):
		old_s = list(s)
		new_s = list(s)
		for i in range(len(old_s) - 1):
			if old_s[i] == 'A' and old_s[i + 1] == 'P':
				new_s[i + 1] = 'A'
		s = ''.join(new_s)
		count += 1
	ans.append(count)
print(*ans, sep='\n')


N = int(input())
while N != 0:
	x = int(input())
	st = input()
	maxm = 0
	while 'AP' in st:
		st = st.replace('AP', 'AA')
		maxm = maxm + 1
	print(maxm)
	N = N - 1


t = int(input())
for r in range(t):
	input()
	print(max([len(w) for w in (input() + 'A').split('A')[1:]]))


import re
pattern = re.compile('AP+')

def solve(s: str) -> int:
	global pattern
	subs = pattern.findall(s)
	answ = max(map(len, subs)) - 1 if len(subs) != 0 else 0
	return answ
num = int(input())
for _ in range(num):
	input()
	s = input()
	print(solve(s))


x = int(input())
for i in range(x):
	y = int(input())
	a = input()
	liste = a.split('A')
	z = 0
	for j in range(1, len(liste)):
		if len(liste[j]) > z:
			z = len(liste[j])
		j += 1
	print(z)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
from math import gcd
INF = 10 ** 18
MOD = 10 ** 9 + 7

def lcm(a, b):
	return a * b // gcd(a, b)
for _ in range(int(ri())):
	n = int(ri())
	s = ri()
	left = [0] * n
	cnt = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'P':
			cnt += 1
		else:
			left[i] = cnt
			cnt = 0
	maxx = 0
	for i in range(n):
		if s[i] == 'A':
			maxx = max(left[i], maxx)
	print(maxx)


t = int(input())
for q in range(0, t, 1):
	n = int(input())
	a = input()
	m = 0
	i = len(a)
	for j in range(0, len(a)):
		if a[j] == 'A':
			i = j
			break
	for j in range(i, len(a), 1):
		if 'A' == a[j]:
			m = max(m, j - i - 1)
			i = j
	m = max(len(a) - i - 1, m)
	print(m)


t = int(input())
for i in range(t):
	l = int(input())
	curr = [m for m in input()]
	prev = []
	tim = 0
	while prev != curr:
		prev = [k for k in curr]
		j = 0
		while j < l - 1:
			if curr[j] == 'A' and curr[j + 1] == 'P':
				curr[j + 1] = 'A'
				j += 2
			else:
				j += 1
		tim += 1
	print(tim - 1)


for i in range(int(input())):
	n = int(input())
	studentString = input()
	j = 0
	count = 0
	while j < n - 1:
		if studentString[j:j + 2] == 'AP':
			startInd = j + 1
			j = startInd
			while j < n and studentString[j] == 'P':
				j += 1
			if count < j - startInd:
				count = j - startInd
			continue
		j += 1
	print(count)


t = int(input())
for _ in range(t):
	input()
	counter = 0
	mx = 0
	line = input()
	i = 0
	length = len(line)
	while i < length and line[i] != 'A':
		i += 1
	for el in line[i + 1:]:
		if el == 'A':
			if counter > mx:
				mx = counter
			counter = 0
		else:
			counter += 1
	print(mx if mx > counter else counter)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	st = s.split('A')
	if s[0] == 'P':
		st = st[1:]
	mx = 0
	for stt in st:
		mx = max(mx, len(stt))
	print(mx)


for i in range(int(input())):
	n = input()
	(c, f, m, l) = (0, 0, 0, [0])
	lst = list(map(str, input()))
	for (i, j) in enumerate(lst):
		if lst[i] == 'A' and f == 0:
			m = 1
		if lst[i] == 'P' and m == 1:
			c = c + 1
			f = 1
		else:
			l.append(c)
			c = 0
	l.append(c)
	print(max(l))


for k in range(int(input())):
	x = int(input())
	p = input()
	if 'A' not in p:
		print(0)
		continue
	p = p[p.index('A'):]
	Max = 0
	Sum = 0
	for i in range(len(p)):
		if p[i] == 'P':
			Sum += 1
		else:
			Max = max(Max, Sum)
			Sum = 0
	Max = max(Max, Sum)
	print(Max)


t = int(input())

def solve(k, S):
	ans = 0
	cnt = 0
	a_flg = False
	for s in S:
		if s == 'A':
			a_flg = True
		if a_flg == True and s == 'P':
			cnt += 1
			ans = max(ans, cnt)
		else:
			cnt = 0
	return ans
for i in range(t):
	k = int(input())
	S = input()
	print(solve(k, S))


t = int(input())
for i in range(t):
	n = int(input())
	s = [len(j) for j in input().split('A')]
	output = max(s[1:] + [0])
	print(output)


for _ in range(int(input())):
	a = int(input())
	s = input()
	count = 0
	for _ in range(a):
		b = ''
		m = 0
		count1 = 0
		for i in range(a):
			if s[i] == 'A':
				m += 1
				b += 'A'
			elif s[i] == 'P':
				if m >= 1:
					m = 0
					b += 'A'
					count1 += 1
				else:
					b += 'P'
		s = b
		if count1 != 0:
			count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' not in s:
		print(0)
		continue
	i = s.find('A')
	k = 0
	m = 0
	i += 1
	while i < n:
		if s[i] == 'P':
			k += 1
		else:
			m = max(m, k)
			k = 0
		i += 1
	m = max(m, k)
	print(m)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	temp = 0
	res = 0
	a = 0
	for j in range(k):
		if s[j] == 'A':
			a += 1
			temp = 0
		if a != 0 and s[j] == 'P':
			temp = temp + 1
			if temp > res:
				res = temp
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	c = 0
	while s.find('AP') != -1:
		s = s.replace('AP', 'AA')
		c += 1
	print(c)


angrytime = 0

def compute_angrytime(l, string):
	if not 'A' in string:
		return 0
	elif not 'P' in string[string.index('A'):]:
		return 0
	else:
		newstring = [i for i in string]
		for i in range(l - 1):
			if string[i] == 'P':
				pass
			elif string[i] == 'A':
				newstring[i + 1] = 'A'
		global angrytime
		angrytime += 1
		compute_angrytime(l, ''.join(newstring))
for group in range(int(input())):
	num_people = int(input())
	string = input()
	angrytime = 0
	compute_angrytime(num_people, string)
	print(angrytime)


from sys import stdin, stdout
import bisect
import math

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
			n = n - 1
		else:
			a = a * a
			n = n >> 1
	return r

def solve():
	n = inp()
	ma = -10000
	s = st()
	if 'A' not in s:
		pr('0')
	else:
		a = s.index('A')
		while a < n:
			if s[a] == 'P':
				c = 1
				j = a + 1
				while j < n and s[j] == 'P':
					c += 1
					j += 1
				a = j
				if c > ma:
					ma = c
			else:
				a += 1
		if ma == -10000:
			pr(0)
		else:
			pr(ma)
for _ in range(inp()):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	m = 0
	n = 0
	for i in s:
		if i == 'A':
			t = max(m, t)
			n = 1
			m = 0
		elif n > 0:
			m += 1
		t = max(t, m)
	print(t)


for _ in range(int(input())):
	a = int(input())
	b = str(input())
	b = b.lstrip('P')
	c = b.split('A')
	d = []
	for x in c:
		d.append(len(x))
	print(max(d))


test_cases = int(input())

def remove_patients(students):
	if students[0] == 'A':
		return students
	return remove_patients(students[1:])
for i in range(test_cases):
	size = int(input())
	students = input()
	if 'A' not in students:
		print(0)
	else:
		students_type = remove_patients(students)
		print(max([len(i) for i in students_type.split('A')]))


t = int(input())
arr = [0] * t
for i in range(t):
	k = int(input())
	s = input()
	Mx_P = 0
	kol_P = 0
	for j in range(k - 1):
		kol_P = 0
		if s[j] == 'A' and s[j + 1] == 'P':
			kol_P += 1
			for m in range(j + 2, k):
				if s[m] == 'P':
					kol_P += 1
				else:
					break
		Mx_P = max(Mx_P, kol_P)
	arr[i] = Mx_P
for i in range(t):
	print(arr[i])


t = int(input())
templist = []
while t:
	k = int(input())
	str = input()
	c = 0
	while str.count('AP') > 0:
		str = str.replace('AP', 'AA')
		c += 1
	templist.append(c)
	t -= 1
for x in templist:
	print(x)


for i in range(int(input())):
	n = int(input())
	s = list(input())
	l = []
	for i in range(n):
		if s[i] == 'A':
			l.append(i)
	if len(l) == 0:
		print(0)
	else:
		m = 0
		for i in range(len(l) - 1):
			if abs(l[i] - l[i + 1]) - 1 > m:
				m = abs(l[i] - l[i + 1]) - 1
		if n - l[-1] - 1 > m:
			m = n - l[-1] - 1
		print(m)


t = int(input())
for i in range(t):
	k = int(input())
	st = input()
	prov = False
	now = 0
	maxx = 0
	for l in range(k):
		if st[l] == 'A':
			prov = True
			maxx = max(now, maxx)
			now = 0
		elif prov:
			now += 1
	maxx = max(now, maxx)
	print(maxx)


t = int(input())
while t:
	k = int(input())
	s = input()
	c = 1
	q = 0
	while c:
		c = 0
		i = 0
		while i < len(s):
			st = ''
			if s[i:i + 2] == 'AP':
				st = s[:i + 1] + 'A' + s[i + 2:]
				s = st
				i = i + 2
				c = c + 1
			else:
				i = i + 1
		q = q + 1
	print(q - 1)
	t = t - 1


l = int(input())
for z in range(l):
	a = int(input())
	strng = input()
	str = []
	for j in range(len(strng)):
		str.append(strng[j])
	mini = 0
	while True:
		i = 0
		l = 0
		while i < len(str) - 1:
			if str[i] == 'A' and str[i + 1] == 'P':
				str[i + 1] = 'A'
				i += 2
				continue
			i += 1
			l += 1
		if l == len(str) - 1:
			print(mini)
			break
		mini += 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	ct = [0]
	count = 0
	i = n - 1
	while i >= 0:
		if a[i] == 'P':
			count += 1
			i -= 1
		else:
			ct.append(count)
			count = 0
			i -= 1
	if n == 1:
		print(0)
	else:
		print(max(ct))


import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
from itertools import permutations
input = lambda : sys.stdin.readline().rstrip()
read = lambda : list(map(int, input().split()))
go = lambda : 1 / 0

def write(*args, sep='\n'):
	for i in args:
		sys.stdout.write('{}{}'.format(i, sep))
INF = float('inf')
MOD = int(1000000000.0 + 7)
YES = 'YES'
NO = 'NO'
for _ in range(int(input())):
	try:
		n = int(input())
		arr = list(input())
		ans = 0
		while 1:
			s = arr[:]
			flag = True
			for i in range(n - 1):
				if arr[i + 1] == 'P' and arr[i] == 'A':
					flag = False
					s[i + 1] = 'A'
			if flag:
				print(ans)
				go()
			ans += 1
			arr = s[:]
	except:
		continue


for z in range(int(input())):
	a = int(input())
	s = input() + 'A'
	(l, c) = ([], 0)
	for x in range(a + 1):
		if s[x] == 'A':
			l.append(x - c - 1)
			c = x
	if len(l) == 1:
		print(0)
	else:
		print(max(l[1:]))


N = int(input())
answer = []
for i in range(N):
	sh_amount = int(input())
	sh_row = input()
	current_time = 0
	max_time = 0
	extr = 0
	for k in range(sh_amount):
		if sh_row[k] == 'A':
			current_time = 0
			extr = extr + 1
		elif extr > 0:
			current_time = current_time + 1
			if max_time < current_time:
				max_time = current_time
	answer.append(max_time)
for i in range(N):
	print(answer[i])


t = int(input())
for i in range(t):
	k = int(input())
	st = list(input())
	count = 0
	while True:
		j = 0
		c = False
		while j < k - 1:
			if st[j] == 'A' and st[j + 1] == 'P':
				st[j + 1] = 'A'
				j += 2
				c = True
			else:
				j += 1
		if c:
			count += 1
		else:
			break
	print(count)


import sys

def main():
	tests = int(sys.stdin.readline()[:-1])
	for i in range(tests):
		n = int(sys.stdin.readline()[:-1])
		s = sys.stdin.readline()[:-1]
		counting = False
		ps = [0]
		for c in s:
			if c == 'P':
				if counting:
					ps[-1] += 1
			else:
				counting = True
				ps.append(0)
		print(max(ps))
main()


t = int(input())
for i in range(0, t, 1):
	n = int(input())
	c = input()
	a = []
	for j in range(0, n, 1):
		a.append(c[j])
	zl = 1
	q = 0
	while zl > 0:
		zl = 0
		q += 1
		for j in range(0, n - 1):
			if 'A' == a[j] and a[j + 1] == 'P':
				zl += 1
		b = []
		for j in range(0, n):
			b.append(a[j])
		for j in range(0, n - 1):
			if 'A' == b[j] and b[j + 1] == 'P':
				a[j + 1] = 'A'
	print(q - 1)


n = int(input())
for _ in range(n):
	input()
	x = input()
	t = 0
	angry = -1
	for (i, c) in enumerate(list(x)):
		if c == 'A':
			if angry != -1:
				t = max(t, i - angry - 1)
			angry = i
	if angry != -1:
		t = max(t, i - angry)
	print(t)


n = int(input())
res = []
for i in range(n):
	s = int(input())
	t = input()
	cnt = 0
	mx = 0
	isA = False
	for i in range(len(t)):
		if t[i] == 'A':
			if not isA:
				isA = True
			else:
				if i > 0 and t[i - 1] == 'P':
					isA = False
					if cnt > mx:
						mx = cnt
					cnt = 0
				if i != len(t) - 1 and t[i + 1] == 'P':
					isA = True
		elif isA:
			cnt += 1
			if i == len(t) - 1:
				isA = False
				if cnt > mx:
					mx = cnt
				cnt = 0
	res.append(str(mx))
print('\n'.join(res))


for _ in range(int(input())):
	n = int(input())
	A = input()
	a = []
	for i in range(n):
		a.append(A[i])
	cnt = 0
	check = False
	while not check:
		i = 0
		Cnt = 0
		while i < n - 1:
			if a[i] == 'A' and a[i + 1] == 'P':
				a[i + 1] = 'A'
				i += 2
				Cnt += 1
			else:
				i += 1
		cnt += 1
		if Cnt == 0:
			break
	print(cnt - 1)


t = int(input())
import copy
for j in range(t):
	k = int(input())
	s = str(input())
	temp = []
	temp_old = []
	v = 0
	for i in range(k):
		temp.append(s[i])
	ch = 0
	while temp != temp_old:
		temp_old = copy.copy(temp)
		for i in range(k - 1, 0, -1):
			if temp[i] == 'P' and temp[i - 1] == 'A':
				ch += 1
				temp[i] = 'A'
		if ch > 0:
			v += 1
	print(v if v == 0 else v - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input())
	c = 0
	ai = -1
	for i in range(n):
		if arr[i] == 'A':
			ai = i
			arr[i] = 0
		elif arr[i] == 'P' and ai == -1:
			arr[i] = 0
		elif arr[i] == 'P' and ai != -1:
			arr[i] = i - ai
	print(max(arr))


T = int(input())
for i in range(0, T):
	k = int(input())
	group = input()
	maxLenOfPeace = 0
	currentLenOfPeace = 0
	for j in reversed(range(0, k)):
		if group[j] == 'P':
			currentLenOfPeace += 1
		elif group[j] == 'A':
			if currentLenOfPeace > maxLenOfPeace:
				maxLenOfPeace = currentLenOfPeace
			currentLenOfPeace = 0
	print(maxLenOfPeace)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	ans = 0
	flag = 0
	a = 0
	for i in range(n):
		if s[i] == 'A' and flag == 0:
			flag = 1
		elif s[i] == 'P' and flag == 1:
			a += 1
		else:
			a = 0
		ans = max(ans, a)
	print(ans)


from math import ceil
from collections import deque
for _ in range(int(input())):
	n = int(input())
	s = input() + 'A'
	ans = c = 0
	j = 0
	while s[j] == 'P' and j < n:
		j += 1
	while j < n:
		if s[j] == 'A':
			c = 0
		else:
			c += 1
			ans = max(ans, c)
		j += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	s.reverse()
	f = 'A'
	ans = 0
	cur = 0
	for i in range(n):
		if s[i] == 'P':
			if f == 'A':
				f = 'P'
				cur += 1
			else:
				cur += 1
		elif f == 'P':
			ans = max(ans, cur)
			cur = 0
			f = 'A'
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().lstrip('P')
	t = []
	a = 0
	(b, c) = (0, 0)
	for j in range(1, len(s)):
		if s[j] == 'P':
			c = j
		else:
			if c - b > a:
				a = c - b
			b = c = j
	if c - b > a:
		a = c - b
	print(a)


t = int(input())
while t != 0:
	k = int(input())
	s = input()
	i = 0
	q = 0
	while 1 == 1:
		i = s.find('A')
		if i == -1:
			break
		j = s.find('A', i + 1)
		if j != -1:
			if q < j - i - 1:
				q = j - i - 1
			s = s[j:]
		else:
			if q < len(s) - i - 1:
				q = len(s) - i - 1
			break
	print(q)
	t = t - 1


t = int(input())
for i in range(0, t):
	n = int(input())
	initialString = str(input())
	counter = 0
	while 'AP' in initialString:
		counter += 1
		initialString = initialString.replace('AP', 'AA')
	print(counter)


groups = int(input())
while groups:
	n = int(input())
	students = str(input())
	moment = 0
	while students.count('AP') > 0:
		students = students.replace('AP', 'AA')
		moment += 1
	print(moment)
	groups -= 1


for _ in [0] * int(input()):
	input()
	print(max((len(v) for v in input().lstrip('P').split('A'))))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	k = s.find('A')
	if k != -1:
		ans = 0
		d = 0
		for j in range(k + 1, n):
			if s[j] == 'P':
				d += 1
				if d > ans:
					ans += 1
			else:
				d = 0
		print(ans)
	else:
		print(0)


def deadline(a, s):
	count = 0
	m = 0
	i = 0
	while i < a - 1:
		if s[i] == 'A' and s[i + 1] != 'A':
			while i < a - 1 and s[i + 1] != 'A':
				count += 1
				i += 1
			m = max(count, m)
			count = 0
		else:
			i += 1
	return m
t = int(input())
for i in range(t):
	a = int(input())
	s = input()
	print(deadline(a, s))


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	(p, k, l) = (0, 0, 0)
	for i in range(n):
		if s[i] == 'A':
			p = 0
			l = 1
		elif l == 1:
			p += 1
			if k < p:
				k = p
	if l == 0:
		print(l)
	else:
		print(k)


a = int(input())
for i in range(a):
	b = int(input())
	s = input()
	count = 0
	while True:
		c = 0
		for j in range(b - 2, -1, -1):
			if s[j] == 'A':
				if s[j + 1] != 'A':
					s = s[:j + 1] + 'A' + s[j + 2:]
					c += 1
		if c == 0:
			break
		count += 1
	print(count)


for t in range(int(input())):
	n = int(input())
	s = input()
	d = [n]
	for i in range(n - 1, -1, -1):
		if s[i] == 'A':
			d.append(i)
	e = []
	for i in range(len(d) - 1):
		e.append(d[i] - d[i + 1])
	if len(e) > 0:
		print(max(e) - 1)
	else:
		print('0')


def solve():
	n = int(input())
	s = str(input())
	ans = 0
	cnt = 0
	found = False
	for id in range(n):
		if s[id] == 'P':
			cnt += 1
		else:
			if found:
				ans = max(ans, cnt)
			found = True
			cnt = 0
	if found:
		ans = max(ans, cnt)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	maxm = 0
	tr = False
	cnt = 0
	for s1 in s:
		if s1 == 'A':
			tr = True
			cnt = 0
		if tr and s1 == 'P':
			cnt += 1
		if cnt > maxm:
			maxm = cnt
	print(maxm)


mins = 0
t = input()
out = 0
i = 0
inc = 0
maxx = []
itdoescontain = 1
for _ in range(0, int(t)):
	x = int(input())
	s = input()
	maxx = [0]
	for q in range(0, len(s)):
		i = 0
		if 'AP' in s:
			if s[q] == 'A':
				for w in range(q + 1, len(s)):
					if s[w] == 'P':
						i += 1
					else:
						break
				maxx.append(i)
		i = 0
	m = sorted(maxx)
	print(m[-1])


for i in range(0, int(input())):
	input()
	s = input().split('A')[1:]
	max = 0
	for j in s:
		if len(j) > max:
			max = len(j)
	print(max)


import sys
q = int(input())
results = []
for _ in range(q):
	input()
	result = tmp = 0
	s = list(next(sys.stdin))[:-1]
	met_anger = False
	for c in s:
		if c == 'P':
			if met_anger:
				tmp += 1
		else:
			met_anger = True
			result = max(tmp, result)
			tmp = 0
	result = max(tmp, result)
	results.append(str(result))
print('\n'.join(results))


for _ in range(int(input())):
	n = int(input())
	s = input().strip()
	cnt = 0
	mx = -1
	flag = False
	for i in range(n):
		if s[i] == 'A':
			flag = True
		if flag:
			if s[i] == 'A':
				mx = max(cnt, mx)
				cnt = 0
			else:
				cnt += 1
	mx = max(mx, cnt)
	print(mx)


def make_angry(group):
	group = list(group)
	i = 0
	while i < len(group) - 1:
		if group[i] == 'A' and group[i + 1] == 'P':
			group[i + 1] = 'A'
			i += 2
		else:
			i += 1
	return ''.join(group)
groups = [i for i in [input() for i in range(2 * int(input()))] if i.isalpha()]
for group in groups:
	if 'A' not in group or 'P' not in group:
		print(0)
	else:
		i = 0
		while group != make_angry(group):
			group = make_angry(group)
			i += 1
		print(i)


def solve():
	n = int(input())
	s = input()
	ans = 0
	i = 0
	while i < n:
		if i > 0 and s[i - 1] == 'A' and (s[i] == 'P'):
			cnt = 0
			while i < n and s[i] == 'P':
				cnt += 1
				i += 1
			ans = max(ans, cnt)
		i += 1
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	i = 0
	max1 = 0
	while n > 1 and i < n:
		count = 0
		if s[i] == 'A':
			j = 1
			k = i + 1
			while k < n and j == 1 and (s[k] == 'P'):
				count += 1
				k += 1
			i = k
			max1 = max(max1, count)
		else:
			i += 1
	print(max1)


for i in range(int(input())):
	k = int(input())
	s = input()
	i = s.find('A')
	if i != -1:
		s = list(s[i + 1:].split('A'))
		s.sort(reverse=True)
		print(len(s[0]))
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	maxi = cnt = 0
	for i in range(n):
		if s[i] == 'A':
			j = i + 1
			while j < n and s[j] == 'P':
				cnt += 1
				j += 1
			maxi = max(maxi, cnt)
			cnt = 0
	print(maxi)


import re
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	cnt = 0
	for i in range(len(s)):
		if 'AP' in s:
			cnt += 1
			s = s.replace('AP', 'AA')
		else:
			print(cnt)
			break
	else:
		print(0)


import sys
t = int(sys.stdin.readline())
for i in range(t):
	count = 0
	n = int(sys.stdin.readline())
	s = sys.stdin.readline()
	while s != s.replace('AP', 'AA'):
		s = s.replace('AP', 'AA')
		count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	(ans, count, flag) = (0, 0, False)
	for i in s:
		if i == 'A':
			count = 0
			flag = True
		elif flag:
			count += 1
		if count > ans:
			ans = count
	print(ans)


for _ in range(int(input())):
	n = int(input())
	(m, t) = (0, 0)
	a = input()
	if n == 1 or len(set(a)) == 1:
		print(0)
		continue
	j = a.index('A')
	for i in range(j + 1, n):
		if a[i] == 'P':
			t += 1
		else:
			m = max(m, t)
			t = 0
	print(max(m, t))


t = int(input())
for s in range(t):
	k = int(input())
	line = input()
	start = -1
	i = 0
	maks = 0
	curr_len = 0
	f_A = False
	for l in line:
		if l == 'A':
			f_A = True
		if f_A:
			if l == 'P' and start == -1:
				start = i
			if l == 'A':
				start = -1
		if start != -1:
			if i - start + 1 > maks:
				maks = i - start + 1
		i += 1
	print(maks)


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	max_ = 0
	index = n
	for i in range(n):
		if s[i] == 'A':
			index = i
			break
	for i in range(index, n):
		if s[i] == 'A':
			count = 0
		else:
			count += 1
			if max_ < count:
				max_ = count
	print(max_)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	N = int(input())
	T = input().rstrip()
	cnt = 0
	ans = 0
	flag = False
	for t in T:
		if t == 'A':
			flag = True
		if not flag:
			continue
		if t == 'P':
			cnt += 1
		if t == 'A':
			ans = max(ans, cnt)
			cnt = 0
	ans = max(ans, cnt)
	print(ans)


t = int(input())
for _ in range(t):
	_ = input()
	std = input()
	ans = 0
	aux = 0
	flag = False
	for i in std:
		if i == 'A':
			if flag == False:
				flag = True
				aux = 0
				continue
			if flag:
				ans = max(ans, aux)
				aux = 0
		else:
			aux += 1
	if flag:
		ans = max(ans, aux)
	print(ans)


t = int(input())
m_t = []

def mut(s):
	s_x = s.copy()
	for i in range(len(s)):
		if s[i] == 'A':
			try:
				s_x[i + 1] = 'A'
			except IndexError:
				pass
	return s_x
for i in range(t):
	input()
	t = 0
	s = list(str(input()))
	l_s = ''
	while l_s != s:
		l_s = s
		s = mut(s)
		t += 1
	m_t.append(t - 1)
for i in m_t:
	print(i)


t = int(input())
for __ in range(t):
	n = int(input())
	s = input()
	index = [i for (i, j) in enumerate(s) if j == 'A']
	size = len(index)
	i = 0
	mom = 0
	change = False
	while True:
		if i == size:
			if not change:
				break
			else:
				i = 0
				mom += 1
				change = False
		if index[i] + 1 < n and s[index[i] + 1] == 'P':
			index[i] += 1
			change = True
		i += 1
	print(mom)


T = int(input())
for i in range(T):
	n = int(input())
	a = list(input())
	ind = []
	ind1 = []
	for i in range(1, n):
		if a[i] == 'P':
			if a[i - 1] == 'A':
				ind.append(i)
	if len(ind) == 0:
		print(0)
	else:
		a.append(0)
		for j in ind:
			ct = 1
			k = j + 1
			while a[k] == 'P':
				ct += 1
				k += 1
			ind1.append(ct)
		print(max(ind1))


for _ in range(int(input())):
	p = int(input())
	hint = input().split('A')
	hint.pop(0)
	if len(hint) == 0:
		print(0)
	else:
		print(len(max(hint)))


t = int(input())
for _ in range(t):
	n = int(input())
	s = str(input())
	s = s + 'A'
	ls = []
	ls1 = []
	for i in range(n + 1):
		if s[i] == 'A':
			ls.append(i)
	if len(ls) > 1:
		for j in range(len(ls) - 1):
			k = ls[j + 1] - ls[j] - 1
			ls1.append(k)
		ans = max(ls1)
		print(ans)
	else:
		print('0')


INF = 10 ** 10

def main():
	n = get_int()
	st = list(input().lstrip('P'))
	c = 0
	maxi = 0
	for i in st:
		if i == 'P':
			c += 1
		else:
			maxi = max(c, maxi)
			c = 0
	print(max(maxi, c))
import sys
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]


for _ in range(int(input())):
	n = int(input())
	count = 0
	best = 0
	arr = input()
	arr2 = list(arr)
	if len(arr) == 1 or set(arr2) == {'A'} or set(arr2) == {'P'}:
		print(0)
	else:
		zz = arr2.index('A')
		if zz != 0:
			arr = arr.replace('P', '', zz)
			arr2 = list(arr)
		for k in range(len(arr2)):
			if arr2[k] == 'P':
				count += 1
				best = max(best, count)
			elif arr2[k] == 'A' or k == len(arr2) - 1:
				count = 0
		print(best)


def students(list):
	k = 0
	while 'AP' in ''.join([elem for elem in list]):
		g = 0
		while g < len(list) - 1:
			if list[g] == 'A':
				if list[g + 1] == 'P':
					list[g + 1] = 'A'
					g += 1
			g += 1
		k += 1
	return k
for i in range(int(input())):
	n = int(input())
	st = list(input())
	print(students(st))


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	if 'A' not in s:
		print(0)
	else:
		a = s.split('A')
		if len(a) == 1:
			print(0)
		else:
			a = [len(x) for x in a[1:]]
			print(max(a))


t = int(input())
for test in range(t):
	n = int(input())
	angry = [ch == 'A' for ch in input()[:n]]
	last_angry = -1
	mlen = 0
	for i in range(n):
		if angry[i]:
			last_angry = i
		elif last_angry >= 0:
			mlen = max(mlen, i - last_angry)
	print(mlen)


for i in range(int(input())):
	input()
	s = input().lstrip('P')
	print(len(max(map(str, s.split('A')))))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	i = 0
	while s[i] == 'P':
		i += 1
		if i == k:
			print(0)
			break
	if i == k:
		continue
	highest = 0
	curr = 0
	i += 1
	while i < k:
		if s[i] == 'P':
			curr += 1
		else:
			highest = max(highest, curr)
			curr = 0
		i += 1
	highest = max(highest, curr)
	print(highest)


z = int(input())
while z > 0:
	b = -999
	bmax = 0
	n = int(input())
	a = input()
	for i in range(0, n):
		if a[i] == 'A':
			if bmax < b:
				bmax = b
			b = 0
		else:
			b += 1
	if bmax < b:
		bmax = b
	z -= 1
	print(bmax)


t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	mx = 0
	if not 'A' in s:
		print(0)
	else:
		r = 0
		for i in range(n):
			if s[i] == 'A':
				r = 1
				c = 0
			if r == 1 and s[i] == 'P':
				c += 1
				mx = max(c, mx)
		print(mx)


groups = int(input())
for i in range(groups):
	students = int(input())
	queue = input().strip()
	moment = 0
	while queue != queue.replace('AP', 'AA'):
		queue = queue.replace('AP', 'AA')
		moment += 1
	print(moment)


def solve():
	n = int(input())
	a = input()
	b = []
	for i in range(len(a)):
		if a[i] == 'A':
			b.append(i)
	b.append(n)
	b.append(n + 1)
	r = 0
	for i in range(1, len(b)):
		r = max(r, b[i] - b[i - 1])
	print(r - 1)
for i in range(int(input())):
	solve()


def solve():
	n = int(input())
	s = input()
	ans = 0
	last_bad = None
	for (i, c) in enumerate(s):
		if c == 'A':
			last_bad = i
		if last_bad is not None:
			ans = max(ans, i - last_bad)
	print(ans)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	i = 0
	counter = 0
	start = 0
	flag = 0
	while i < n:
		if a[i] == 'A':
			count = 0
			flag = 0
			start = 1
		if start == 1:
			if a[i] == 'P':
				count = count + 1
				flag = 1
		if flag == 1:
			if counter < count:
				counter = count
		i = i + 1
	print(counter)


t = int(input())
a = []
for i in range(t):
	b = int(input())
	c = input()
	a.append(c)
for i in a:
	l = []
	max = 0
	for j in i:
		l.append(j)
	for j in range(len(l) - 1):
		t = 0
		if l[j] == 'A':
			p = j + 1
			t = 0
			while p < len(l) and l[p] != 'A':
				p += 1
				t += 1
		if t > max:
			max = t
	print(max)


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	s.reverse()
	if not 'P' in s:
		print(0)
		continue
	isA = 0
	noP = 0
	mx = -1
	for i in range(s.index('P'), n):
		if s[i] == 'P':
			noP += 1
		else:
			mx = max(mx, noP)
			noP = 0
	if mx == -1:
		print(0)
	else:
		print(mx)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		s = S()
		t = 0
		a = []
		for c in s:
			if c == 'A':
				a.append(t)
				t = 0
			else:
				t += 1
		a.append(t)
		r = 0
		if len(a) > 1:
			r = max(a[1:])
		rr.append(r)
	return JA(rr, '\n')
print(main())


for i in range(int(input())):
	n = int(input())
	s = input()
	while len(s) > 0 and s[0] == 'P':
		s = s[1:]
	n = len(s)
	ans = 0
	pa = 0
	for i in range(n):
		if s[i] == 'A':
			ans = max(pa, ans)
			pa = 0
		else:
			pa += 1
	print(max(pa, ans))


t = int(input())
while t > 0:
	length = int(input())
	str_input = input()
	max_count = 0
	stack = []
	for stu in str_input[::-1]:
		if stu != 'A':
			stack.append(stu)
		else:
			temp = ''
			while stack:
				temp += stack.pop()
			max_count = max(max_count, len(temp))
	print(max_count)
	t = t - 1


import sys
import math
import heapq
import bisect
import re
from collections import deque
sys.setrecursionlimit(1000000000)

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())

def modst(w, s):
	res = 1
	while s:
		if s % 2:
			res *= w
		w *= w
		s //= 2
	return res

def error(n):
	return 'YES'

def main():
	n = iinput()
	q = list(input().strip()) + ['A']
	(res, f) = (0, -1)
	for i in range(n + 1):
		if q[i] == 'A':
			if f == -1:
				f = i
			else:
				res = max(res, i - f - 1)
				f = i
	print(res)
for qwerty in range(iinput()):
	main()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = list(input())
	d = []
	prev = -9
	if a.count('A') == 0:
		print(0)
		continue
	for i in range(n):
		if a[i] == 'A' and prev != -9:
			d.append(i - prev - 1)
			prev = i
		elif a[i] == 'A':
			prev = i
	d.append(n - 1 - prev)
	print(max(d))


input = __import__('sys').stdin.readline
print = __import__('sys').stdout.write
for _ in range(int(input())):
	n = int(input())
	a = input()
	val = [0]
	tt = 0
	start_idx = False
	for i in range(1, n):
		if a[i] == 'P':
			if not start_idx:
				if a[i - 1] == 'A':
					start_idx = True
					tt = 1
			else:
				tt += 1
		elif start_idx:
			val.append(tt)
			tt = 0
			start_idx = False
	if start_idx:
		val.append(tt)
	print(str(max(val)) + '\n')


for i in range(int(input())):
	l = int(input())
	s = str(input())
	k = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		k += 1
	print(k)


def solve(k, s):
	t = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		t += 1
	return t

def main():
	for _ in range(int(input())):
		k = int(input())
		s = input()
		print(solve(k, s))
main()


import copy
t = int(input())
for i in range(t):
	k = int(input())
	s = list(input())
	ans = 0
	flg = True
	while flg:
		flg = False
		new_s = copy.deepcopy(s)
		for i in range(1, k):
			if s[i] == 'P' and s[i - 1] == 'A':
				new_s[i] = 'A'
				flg = True
		if flg:
			ans += 1
			s = new_s
	print(ans)


t = int(input())
while t:
	n = int(input())
	s = input()
	cnt = 0
	arr = []
	s = s + 'A'
	for i in range(n + 1):
		if s[i] == 'A':
			arr.append(i)
	diff = []
	for i in range(len(arr) - 1):
		diff.append(abs(arr[i] - arr[i + 1]) - 1)
	if len(diff):
		print(max(diff))
	else:
		print(0)
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	l = len(s)
	ti = 0
	for j in range(l - 1):
		if s.count('AP') != 0:
			s = s.replace('AP', 'AA')
			ti += 1
		else:
			break
	print(ti)


for i in range(int(input())):
	k = int(input())
	s = list(input())
	x = True
	f = -1
	while x:
		x = False
		for j in range(k - 1, 0, -1):
			if s[j] == 'P' and s[j - 1] == 'A':
				x = True
				s[j] = 'A'
		f += 1
	print(f)


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = s.lstrip('P')
	s = s.split('A')
	ans = 0
	for c in s:
		ans = max(ans, len(c))
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	m = 0
	if s.count('A') == 1 and s[-1] == 'A':
		print(0)
	else:
		c = 0
		for i in range(n - 1):
			if s[i] == 'A' and s[i + 1] == 'P':
				c += 1
				s[i + 1] = 'A'
			else:
				m = max(m, c)
				c = 0
		m = max(m, c)
		print(m)


import sys
import atexit
import io
from collections import defaultdict, Counter
sys.setrecursionlimit(10 ** 9)
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def main():
	for _ in range(int(input())):
		k = int(input())
		s = [0 if i == 'A' else 1 for i in input()]
		mx = 0
		f = -1
		lst = 0
		for (ii, i) in enumerate(s):
			if f == -1 and i == 0:
				f = 1
				lst = ii
			elif f == 1:
				if i == 0:
					mx = max(mx, ii - lst - 1)
					lst = ii
		if f == 1:
			mx = max(mx, k - lst - 1)
		print(mx)
main()


n = int(input())
for _ in range(n):
	size = int(input())
	a = input().split('A')
	if len(a) == 1:
		print(0)
		continue
	else:
		a = a[1:]
	print(len(max(a)))


n = int(input())
for i in range(n):
	a = int(input())
	print(max([len(a) for a in (input() + 'A').split('A')][1:]))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def calc(s):
	flag = False
	result = 0
	tmp = 0
	s += 'A'
	for l in s:
		if l == 'A':
			result = max(result, tmp)
			flag = True
			tmp = 0
		elif l == 'P':
			if flag:
				tmp += 1
	return result

def do():
	N = inp()
	ans = []
	for i in range(N):
		n = inp()
		s = insr()
		ans.append(calc(s))
	return ans
ans = do()
for i in ans:
	print(i)


n = int(input())
for i in range(n):
	t = int(input())
	s = input() + 'A'
	l = [i for i in range(t + 1) if s[i] == 'A']
	l2 = [l[i + 1] - l[i] for i in range(len(l) - 1)]
	if l2:
		print(max(l2) - 1)
	else:
		print(0)


for i in range(int(input())):
	count = int(input())
	seq = input()
	max_len = 0
	current = 0
	start = 0
	try:
		start = seq.index('A')
	except:
		start = count
	for j in range(start + 1, count):
		if seq[j] == 'A':
			start = j
			max_len = current if current > max_len else max_len
			current = 0
		else:
			current += 1
	print(max_len if max_len > current else current)


for i in range(int(input())):
	p = int(input())
	s = list(input().split('A'))
	if len(s) > 0:
		t = []
		for k in range(len(s)):
			if 'P' in s[k]:
				if k > 0:
					t.append(len(s[k]))
		if len(t) > 0:
			print(max(t))
		else:
			print(0)
	else:
		print(0)


t = input()
t = int(t)
while t != 0:
	n = input()
	n = int(n)
	s = input()
	l = []
	for i in range(n):
		if s[i] == 'A':
			l.append(i)
	max = 0
	for i in l:
		c = 0
		index = i
		while index + 1 < n and s[index + 1] == 'P':
			c = c + 1
			index = index + 1
		if max < c:
			max = c
	print(max)
	t = t - 1


t = int(input())
for i in range(0, t):
	m = int(input())
	s = str(input())
	s = s.lstrip('P')
	s = s.split('A')
	a = 0
	for j in range(0, len(s)):
		if len(s[j]) > a:
			a = len(s[j])
	print(a)


def angry_students():
	cases = input()
	number_letters = []
	output = 0
	contador = 0
	checked = False
	letters = [[] for i in range(int(cases))]
	for i in range(int(cases)):
		number_letters.append(input())
		aux = input()
		for j in aux:
			letters[i].append(j)
	for i in range(len(letters)):
		while contador == 0:
			for letter in range(len(letters[i]) - 1):
				if checked:
					checked = False
				elif letters[i][letter] == 'A':
					if letters[i][letter + 1] == 'P':
						letters[i][letter + 1] = 'A'
						contador += 1
						checked = True
			if contador != 0:
				contador = 0
				output += 1
			else:
				print(output)
				output = 0
				contador = 0
				break
	return
angry_students()


for i in range(int(input())):
	input()
	a = input()
	try:
		a = a[a.index('A'):]
		a = map(len, a.split('A'))
		print(max(a))
	except:
		print(0)


def do_taks():
	n = int(input())
	current = list(input())
	steps = 0
	while True:
		updated = current.copy()
		have_changes = False
		for i in range(n - 1):
			if current[i] == 'A' and i < n - 1 and (current[i + 1] != 'A'):
				have_changes |= True
				updated[i + 1] = 'A'
		if have_changes:
			steps += 1
			current = updated
		else:
			return steps
tests_count = int(input())
for _ in range(tests_count):
	print(do_taks())


from sys import stdin, stdout
nCases = int(stdin.readline())
for i in range(0, nCases):
	k = int(stdin.readline())
	s = stdin.readline()
	lastInstant = 0
	foundOneA = False
	currentCount = 0
	for c in s:
		if c == 'A':
			currentCount = 0
			if not foundOneA:
				foundOneA = True
		elif c == 'P':
			if foundOneA:
				currentCount = currentCount + 1
				if currentCount > lastInstant:
					lastInstant = currentCount
	print(str(lastInstant))


n = int(input())
for i in range(0, n):
	pass
	a = int(input())
	s = input()
	cnt_max = 0
	cnt = 0
	tr = 0
	for j in range(0, a):
		pass
		if s[j] == 'A':
			tr = 1
		if tr and s[j] == 'P':
			cnt = cnt + 1
		if s[j] == 'A':
			if cnt > cnt_max:
				cnt_max = cnt
			cnt = 0
	if tr == 1 and cnt > cnt_max:
		cnt_max = cnt
	print(cnt_max)


t = int(input())
k = [[0]] * t
s = [[0]] * t
for i in range(t):
	k[i] = int(input())
	s[i] = input()
for i in range(t):
	RA_ind = s[i].rfind('A')
	n = len(s[i])
	cntA = s[i].count('A')
	if cntA == 0 or (cntA == 1 and RA_ind == n - 1) or cntA == n:
		print(0)
	else:
		while s[i].rfind('A') == n - 1:
			s[i] = s[i][:s[i].rfind('A')]
			n = len(s[i])
		if s[i].count('A') == 0:
			print(0)
		else:
			ind = s[i].find('A') + 1
			max_cntP = 0
			cntP = 0
			while ind != n:
				if s[i][ind] == 'A':
					ind += 1
					cntP = 0
				else:
					cntP += 1
					ind += 1
				if cntP > max_cntP:
					max_cntP = cntP
			print(max_cntP)


t = int(input())
s = []
for khk in range(0, t):
	n = int(input())
	a = input()
	k = 0
	z = []
	if 'A' in a:
		for i in range(0, n):
			if a[i] == 'A':
				j = i
				break
		for i in range(j, n):
			if a[i] == 'A':
				z.append(k)
				k = 0
			elif i == n - 1 and a[i] == 'P':
				k += 1
				z.append(k)
				k = 0
			else:
				k += 1
		s.append(max(z))
	else:
		s.append(0)
for i in s:
	print(i)


t = int(input())
for tt in range(t):
	n = int(input())
	s = input().strip()
	arr = []
	i = 0
	while i < len(s):
		if s[i] == 'A':
			j = i + 1
			c = 0
			while j < len(s) and s[j] == 'P':
				j += 1
				c += 1
			arr.append(c)
			i = j
		else:
			i += 1
	if len(arr) == 0:
		print(0)
	else:
		print(max(arr))


t = int(input())
for x in range(t):
	n = int(input())
	s = input()
	(ans, last) = (0, -1)
	for i in range(n):
		if s[i] == 'P':
			continue
		if last != -1:
			ans = max(ans, i - last - 1)
		last = i
	if last != -1:
		ans = max(ans, n - last - 1)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	if 'A' not in s:
		print(0)
	else:
		count = 0
		a = s.index('A')
		while s.count('A') != n - a:
			s = s.replace('AP', 'AA')
			count += 1
		print(count)


for i in range(int(input())):
	n = int(input())
	a = list(input())
	mx = 0
	cnt = 0
	hasAngry = False
	for i in range(len(a)):
		if a[i] == 'A':
			hasAngry = True
		if a[i] == 'P' and hasAngry:
			cnt += 1
		else:
			mx = max(mx, cnt)
			cnt = 0
	mx = max(mx, cnt)
	print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	x = 0
	k = n
	ans = 0
	for j in range(n):
		if a[j] == 'A':
			k = j
			break
	for j in range(k + 1, n):
		if a[j] == 'A':
			ans = max(ans, x)
			x = 0
		else:
			x += 1
	ans = max(ans, x)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	ans = 0
	ansmax = 0
	flag = 0
	flag2 = 0
	for i in a:
		if i == 'A':
			flag2 = 1
			break
	if flag2 == 0:
		print(0)
		continue
	for i in range(n):
		if a[i] == 'A' and flag == 1:
			ansmax = max(i - ans - 1, ansmax)
			ans = i
		elif a[i] == 'A' and flag == 0:
			ans = i
			flag = 1
	ansmax = max(ansmax, n - ans - 1)
	print(ansmax)


for i in range(int(input())):
	input()
	a = input().lower().lstrip('p')
	print(max(map(len, a.split('a'))))


t = int(input())
for i in range(t):
	a = int(input())
	b = input()
	b = list(b)
	z = []
	j = 0
	s = 0
	while j < a - 1:
		s = 0
		if b[j] == 'A':
			if b[j + 1] == 'P':
				j = j + 1
				for k in range(j, a):
					if b[k] == 'P':
						s = s + 1
						j = j + 1
					else:
						break
				z.append(s)
			else:
				j = j + 1
		else:
			j = j + 1
	if len(z) > 0:
		print(max(z))
	else:
		print('0')


for t in range(int(input())):
	n = int(input())
	s = input().split('A') + ['']
	print(max((len(el) for el in s[1:])))


for _ in range(int(input())):
	n = int(input())
	s = input()
	(res, prev) = (0, -1)
	for (i, e) in enumerate(s):
		if e == 'A':
			prev = i
		elif prev != -1:
			res = max(res, i - prev)
	print(res)


t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	s = [x for x in s]
	time = 0
	count = 10 * 10
	while True:
		if count == 0:
			break
		time += 1
		i = 0
		count = 0
		while i < k - 1:
			if s[i] == 'A':
				if s[i + 1] == 'P':
					s[i + 1] = 'A'
					i += 2
					count += 1
					continue
			i += 1
	print(time - 1)


from sys import stdin, stdout
t = int(stdin.readline())

def refresh(group, length):
	group1 = group
	for j in range(length):
		if group[j] == 'A':
			group1 = group1[:j + 1] + 'A' + group1[j + 2:]
	return group1

def last_moment(group, length):
	cnt = 0
	for i in range(length - 1):
		if group[i] == 'A' and group[i + 1] != 'A':
			cnt += 1
			group = refresh(group, length)
	return str(cnt)
for _ in range(t):
	n = int(stdin.readline())
	new = stdin.readline()
	stdout.write(last_moment(new, n) + '\n')


for t in range(int(input())):
	n = int(input())
	s = input()
	max_count = 0
	count = 0
	i = 0
	while i < n:
		if s[i] == 'A':
			count = 0
			i += 1
			while i < n and s[i] == 'P':
				count += 1
				i += 1
			max_count = max(max_count, count)
		else:
			i += 1
	print(max_count)


def fun():
	a = int(input())
	b = list(input())
	x = 0
	z = []
	flag = 0
	try:
		b.index('A')
	except ValueError:
		return 0
	for i in range(b.index('A'), len(b)):
		if b[i] == 'P':
			x += 1
		if b[i] == 'A':
			z.append(x)
			x = 0
	if x > 0:
		z.append(x)
	if len(z) > 0:
		return max(z)
	else:
		return 0
for i in range(int(input())):
	print(fun())


import sys, math
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
t = 1
t = inp()
for tt in range(t):
	n = inp()
	a = insr()
	a = ' '.join(a)
	c = 0
	while True:
		if 'A P' in a:
			a = a.replace('A P', 'A A')
			c = c + 1
		else:
			break
	print(c)


for _ in range(int(input())):
	N = int(input())
	S = input()
	print(max([len(i) for i in S.lstrip('P').split('A')]))


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(str(input()))
	s.reverse()
	l = []
	cnt = 0
	for i in range(n):
		if s[i] == 'A':
			l.append(cnt)
			cnt = 0
		else:
			cnt += 1
	if len(l) == 0:
		print(0)
	else:
		print(max(l))


t = int(input())
for i in range(t):
	n = int(input())
	inp = input()
	count = 0
	count_max = 0
	flag = False
	for j in range(n):
		if inp[j] == 'A':
			flag = True
			if count_max < count:
				count_max = count
			count = 0
		elif flag == True:
			count += 1
	if count > count_max:
		count_max = count
	print(count_max)


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = list(a)
	c = []
	k = 0
	i = 0
	while i < n - 1:
		if b[i] == 'A':
			for j in range(i + 1, n):
				if b[j] == 'P':
					k += 1
					i = j
				else:
					c.append(k)
					k = 0
					i = j
					break
		else:
			i += 1
	c.append(k)
	if len(c) == 0:
		print(0)
	else:
		print(max(c))


import sys

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def fun(a):
	cnt = 0
	curr_cnt = 0
	f = False
	for i in a:
		if not f:
			if i == 'A':
				f = True
				curr_cnt = 0
		elif i == 'A':
			curr_cnt = 0
		else:
			curr_cnt += 1
		if cnt < curr_cnt:
			cnt = curr_cnt
	return cnt
t = read_int()
k = []
st = []
for i in range(t):
	k.append(read_int())
	st.append(read_line())
for i in range(t):
	print(fun(st[i]))


for _ in range(int(input())):
	input()
	s = input()
	print(0 if 'A' not in s else max(map(len, s[s.index('A'):].split('A'))))


n = int(input())
for i in range(n):
	b = int(input())
	s = input()
	sol = -10000
	acum = -10000
	for x in s:
		if x == 'P':
			acum += 1
			sol = max(acum, sol)
		else:
			sol = max(0, sol)
			acum = 0
	print(max(sol, 0))


from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline

def factors(n):
	return sorted(list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))))

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n').split(' ')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	s = st()
	last = float('inf')
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			last = i
		else:
			ans = max(ans, i - last)
	print(ans)


for _ in range(int(input())):
	k = int(input())
	s = input()
	(ans, flag, c) = (0, 0, 0)
	for i in s:
		if i == 'A':
			c = 0
			flag = 1
		elif i == 'P':
			if flag == 1:
				c += 1
				ans = max(ans, c)
	print(max(ans, c))


colomns = int(input())
t = 0
for i in range(colomns):
	l = []
	n = int(input())
	k = str(input())
	for j in range(len(k)):
		l.append(k[j])
	l1 = l.copy()
	c = 0
	t = 0
	while l.count('P') > 0:
		t = l.count('P')
		for m in range(len(l) - 1):
			if l[m] == 'A' and l[m + 1] == 'P':
				l1[m + 1] = 'A'
		l = l1.copy()
		if t == l.count('P'):
			break
		c += 1
	print(c)


def calculate():
	total_students = int(input())
	position = list(input())
	count = 0
	while 1:
		new_position = ['A' if position[index - 1 if index - 1 > 0 else 0] == 'A' else position[index] for index in range(total_students)]
		if new_position == position:
			break
		count += 1
		position = new_position
	print(count)
times_to_calculate = int(input())
for i in range(times_to_calculate):
	calculate()


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	ind = s.find('A')
	if ind == -1:
		print(0)
	else:
		count = 0
		arr = []
		for j in range(ind, n):
			if s[j] == 'A':
				arr.append(count)
				count = 0
			else:
				count += 1
		arr.append(count)
		print(max(arr))


a = int(input())
t = []
for i in range(a):
	z = []
	b = int(input())
	l = list(map(str, input().split('A')))
	l.pop(0)
	for j in range(len(l)):
		z.append(len(l[j]))
	if len(z) > 0:
		t.append(max(z))
	else:
		t.append(0)
for i in range(a):
	print(t[i])


import sys
import io
import os
total = 0
failed = 0

def run(test, res):
	x = io.StringIO()
	with io.StringIO(test) as sys.stdin:
		with x as sys.stdout:
			work()
			z = x.getvalue().strip()
	sys.stdout = sys.__stdout__
	print('Passed?', z == res)
	print('Expected: ', res)
	print('Actual  : ', z)
	global total, failed
	total += 1
	failed += 1 if z != res else 0

def work():
	t = int(input())
	for i in range(0, t):
		n = int(input())
		f = input()
		p = ''
		c = -1
		while p != f:
			c += 1
			p = f
			s = f[0]
			for j in range(1, n):
				s += 'A' if f[j - 1] == 'A' else f[j]
			f = s
		print(c)

def test():
	run('1\n4\nPPAP', '1')
	run('3\n12\nAPPAPPPAPPPP\n3\nAAP\n3\nPPA', '4\n1\n0')
if 'LOCALTEST' in os.environ:
	test()
	print('\n Result: %s (%d total, %d failed)' % ('FAILED' if failed > 0 else 'PASSED', total, failed))
else:
	work()


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	l = []
	if n == 1:
		print(0)
	else:
		for i in range(n):
			if s[i] == 'A':
				l.append(i + 1)
		k = len(l)
		if k == 1:
			if l[0] == n:
				print(0)
			else:
				print(n - l[0])
		elif k == 0:
			print(0)
		else:
			m = 0
			for i in range(1, k):
				m = max(m, abs(l[i - 1] - l[i]) - 1)
			m = max(m, abs(l[-1] - n))
			print(m)


t = int(input())
for i in range(t):
	nStudents = int(input())
	line = input()
	p = line.split('A')
	p.pop(0)
	nPs = [len(x) for x in p]
	if nPs:
		print(max(nPs))
	else:
		print(0)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	c = 0
	m = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 'A':
			if c > m:
				m = c
			c = 0
		else:
			c = c + 1
	print(m)
	t = t - 1


for i in range(int(input())):
	n = int(input())
	data = input() + 'A'
	count = 0
	c = 0
	h = False
	for j in range(n + 1):
		if data[j] == 'P' and h:
			count += 1
		elif data[j] == 'A':
			h = True
			c = max(c, count)
			count = 0
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = (input() + 'A').split('A')
	mp = list(map(len, s[1:]))
	print(max(mp))


t = int(input())
for _ in range(t):
	m = int(input())
	s = input()
	if s.count('A') == 0 or s.count('P') == 0:
		print(0)
		continue
	result = count = 0
	pre = 0
	for i in range(-1, -m - 1, -1):
		if s[i] == 'A':
			count = i - pre + 1
			pre = i
			if count < result:
				result = count
	print(-result)


def time_taken(s):
	time = 0
	while 'AP' in s:
		s = s.replace('AP', 'AA')
		time += 1
	return time
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(time_taken(s))


for t in range(int(input())):
	n = int(input())
	s = list(input())
	i = 0
	while i < n and s[i] == 'P':
		i += 1
	seq = ''.join(s[i:])
	print(max(map(len, seq.split('A'))))


t = int(input())
a = ''
l = []
for i in range(t):
	b = int(input())
	a = list(input())
	n = 0
	while 1:
		k = 0
		n = n + 1
		j = 0
		while j <= b - 2:
			if a[j] == 'A' and a[j + 1] == 'P':
				a[j + 1] = a[j]
				j = j + 2
				k = 1
			else:
				j = j + 1
		if k == 0:
			l.append(n - 1)
			break
for i in l:
	print(i)


def function(str_):
	list_num = []
	temp = 0
	for i in range(len(str_) - 1):
		if str_[i] == 'A' and str_[i + 1] == 'P':
			j = i + 1
			while str_[j] == 'P':
				temp += 1
				j += 1
				if j == len(str_):
					break
		if temp != 0:
			list_num.append(temp)
		temp = 0
	if len(list_num) > 0:
		print(max(list_num))
	else:
		print('0')
t = int(input())
for i in range(t):
	k = int(input())
	str_ = input()
	function(str_)


t = int(input().strip())
for i in range(t):
	L = int(input().strip())
	text = input().strip()
	firstA = -1
	for j in range(L):
		if text[j] == 'A':
			firstA = j
			break
	if firstA == -1 or firstA == L - 1:
		print(0)
	else:
		textList = text[j + 1:].split('A')
		if len(textList) == 0:
			print(0)
		else:
			max = 0
			for k in range(len(textList)):
				if len(textList[k]) > max:
					max = len(textList[k])
			print(max)


cnt = 11
for i in range(100000):
	cnt += 11
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	ans = 0
	temp = s.copy()
	for time in range(1, 200):
		i = 0
		while i < n - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				i += 2
			else:
				i += 1
		if s == temp:
			break
		temp = s.copy()
	print(time - 1)


for _ in range(int(input())):
	N = int(input())
	L = list(input())
	Max = 0
	for i in range(N - 1):
		count = 0
		if L[i] == 'A':
			for j in range(i + 1, N):
				if L[j] == 'P':
					count += 1
				else:
					if Max < count:
						Max = count
					break
			if Max < count:
				Max = count
	print(Max)


for i in range(int(input())):
	n = int(input())
	a = list(input())
	(ans, flag, j, pointer) = (0, 0, 0, 0)
	freq = [0] * len(a)
	for i in range(len(a)):
		if a[i] == 'A':
			pointer = 1
		if a[i] == 'P' and i != 0 and (pointer == 1):
			if flag == 0:
				freq[j] += 1
			else:
				j += 1
				flag = 1
		elif flag == 1:
			pass
		else:
			j += 1
			flag = 0
	print(max(freq))


for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' not in s:
		print('0')
	else:
		j = s.index('A')
		l = s[j:].split('A')
		print(len(max(l)))


t = int(input())
num = list()
string = list()
for j in range(t):
	num.append(int(input()))
	string.append(input())
for j in range(t):
	new = list()
	for i in range(len(string[j])):
		if string[j][i] == 'A':
			new.append(i)
	new.append(num[j] - 1)
	ans = list()
	if len(new) == 1:
		print(0)
	else:
		for j in range(len(new) - 2):
			ans.append(new[j + 1] - new[j] - 1)
		ans.append(new[-1] - new[-2])
		print(max(ans))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	p = 0
	a = 0
	z = []
	for i in range(len(s)):
		if s[i] == 'A':
			a += 1
			z.append(i)
		else:
			p += 1
	z.append(n)
	for i in range(len(z) - 1):
		z[i] = z[i + 1] - z[i]
	z[len(z) - 1] = 0
	maxx = max(z)
	if maxx == 0:
		print(0)
	else:
		print(maxx - 1)


t = int(input())
while t:
	n = int(input())
	s = input()
	pos = -1
	Max = 0
	for i in range(len(s)):
		if s[i] == 'A':
			if pos == -1:
				pos = i
				continue
			Max = max(Max, i - pos - 1)
			pos = i
	if pos != -1:
		Max = max(Max, len(s) - pos - 1)
	print(Max)
	t -= 1


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	k = int(input())
	students = input()
	angry = []
	for s in range(k):
		if students[s] == 'A':
			angry.append(s)
	if len(angry) == 0:
		print(0)
	else:
		angry.append(k)
		maxsofar = 0
		for s in range(1, len(angry)):
			diff = angry[s] - angry[s - 1] - 1
			if diff > maxsofar:
				maxsofar = diff
		print(maxsofar)


def a_number(group):
	group_list = group.split('A')
	if len(group_list) == 1:
		if group_list[-1] == 'A':
			return 1
		else:
			return 0
	if group_list[-1]:
		group_list[-1].replace('P', '')
	if group_list[0]:
		group_list.pop(0)
	res = len(group_list[0])
	for i in group_list:
		res = max(res, len(i))
	return res
k = int(input())
answers = []
for i in range(k):
	input()
	group = input()
	answers.append(a_number(group))
for i in answers:
	print(i)


t = int(input())
while t > 0:
	n = int(input())
	s = ''
	for val in input():
		if val == 'P':
			s += '0'
		else:
			s += '1'
	s = s.lstrip('0')
	if s != '':
		num = int(s, 2)
	p = pow(2, len(s)) - 1
	i = 0
	if s != '':
		while num != p:
			num |= num >> 1
			i += 1
	print(i)
	t -= 1


t = int(input())
for awfd in range(t):
	n = int(input())
	s = input()
	li = 1000000000000
	mc = 0
	for i in range(n):
		if s[i] == 'A':
			mc = max(mc, i - li - 1)
			li = i
	mc = max(mc, n - 1 - li)
	print(mc)


t = int(input())
for _ in range(t):
	n = int(input())
	l = input()
	i = 0
	c = 0
	while i < n - 1:
		if 'AP' in l:
			l = l.replace('AP', 'AA')
			c += 1
		i += 1
	print(c)


def trans(k, s):
	ns = ''
	for i in range(k):
		if s[i] == 'A' or (i > 0 and s[i - 1] == 'A'):
			ns = ns + 'A'
		else:
			ns = ns + 'P'
	return (ns == s, ns)
t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	c = 0
	B = False
	while not B:
		(B, s) = trans(k, s)
		if B == False:
			c += 1
	print(c)


n = int(input())
for i in range(n):
	a = int(input())
	b = input()
	e = 0
	s = 0
	while 'AP' in b:
		b = b.replace('AP', 'AA')
		s = s + 1
	print(s)


t = int(input())
for q in range(t):
	n = int(input())
	s = input()
	cnt = 0
	ans = 0
	for i in range(len(s)):
		if s[i] == 'A':
			for j in range(i + 1, len(s)):
				if s[j] == 'P':
					cnt += 1
				else:
					break
			ans = max(ans, cnt)
			cnt = 0
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l1 = list(input())
	flag = 0
	shit = 0
	ans = 0
	for i in l1:
		if i == 'A':
			flag = 1
			if shit > ans:
				ans = shit
			shit = 0
		elif flag == 1:
			shit += 1
	if l1[-1] == 'P':
		if shit > ans:
			ans = shit
	if flag == 1:
		print(ans)
	else:
		print(0)


t = int(input())
for x in range(t):
	n = int(input())
	ink = input()
	p = ink.split('A')
	if len(p) == 1:
		print(0)
	elif 'P' not in ink:
		print('0')
	elif p[0] == 'A' and 'P' in ink:
		ans = len(max(p, key=len))
		print(ans)
	else:
		p = p[1:]
		ans = len(max(p, key=len))
		print(ans)


for i in range(int(input())):
	(n, s, ans) = (int(input()), input(), 0)
	(a, c, flag) = (list(s), 0, 0)
	for j in range(n):
		if s[j] == 'A':
			ans = max(ans, c)
			(c, flag) = (0, 1)
		elif flag:
			c += 1
		ans = max(c, ans)
	print(ans)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	s = input()
	a = []
	for i in s:
		a.append(i)
	c = 0
	while c >= 0:
		c = c + 1
		co = a.count('A')
		i = 0
		while i < n - 1:
			if a[i] == 'A' and a[i + 1] == 'P':
				a[i + 1] = 'A'
				i = i + 1
			i = i + 1
		co1 = a.count('A')
		if co == co1:
			break
	print(c - 1)


m = int(input())
for i in range(m):
	k = int(input())
	s = input()
	t = 0
	while s.find('AP') != -1:
		t += 1
		s = s.replace('AP', 'AA')
	print(t)


t = int(input())
while t:
	n = int(input())
	s = input()
	l = list(s)
	x = [0]
	for i in range(len(l) - 1):
		if l[i] == 'A':
			p = 0
			j = i + 1
			while 1:
				if l[j] == 'P':
					p = p + 1
				if j == len(l) - 1 or l[j + 1] == 'A':
					x.append(p)
					break
				j = j + 1
	print(max(x), '\n')
	t -= 1


n = int(input())
while n > 0:
	k = int(input())
	g = input()
	summ = 0
	count = 0
	for p in range(k - 1, -1, -1):
		if g[p] == 'P':
			count += 1
		else:
			summ = max(count, summ)
			count = 0
	print(summ)
	n = n - 1


for _ in range(int(input())):
	b = int(input())
	l = input()
	c = 0
	m = 0
	flag = False
	for i in l:
		if i == 'A':
			if not flag:
				flag = True
			else:
				if c > m:
					m = c
				c = 0
		elif flag:
			c += 1
	if c > m:
		m = c
	print(m)


s = int(input())
count = 0
ans = []
while count < s:
	num = int(input())
	q = input()
	v = []
	for x in range(num):
		if q[x] == 'A':
			v.append(x)
	if len(v) != 0:
		tot_an = num - v[0]
	else:
		tot_an = 0
	q = list(q)
	timer = 0
	v3 = []
	while len(v) != tot_an:
		timer += 1
		v2 = []
		v2 = [x + 1 for x in v if x != num - 1 and x + 1 not in v and (x not in v3)]
		v3 += v
		v += v2
	ans.append(timer)
	count += 1
for y in ans:
	print(y)


t = int(input())
for tt in range(t):
	k = int(input())
	s = input()
	sol = -99999
	max_p = -99999
	for child in s:
		if child == 'A':
			max_p = 0
		else:
			max_p += 1
			sol = max(sol, max_p)
	print(max(sol, 0))


n = int(input())
for x in range(n):
	stu = int(input())
	array = input()
	array = [x for x in array]
	original = ''.join(array)
	count = 0
	index = 0
	while True:
		if index >= stu - 1:
			if original == ''.join(array):
				print(count)
				break
			else:
				count += 1
				original = ''.join(array)
				index = 0
		else:
			try:
				if array[index] == 'A' and array[index + 1] == 'P':
					array[index + 1] = 'A'
					index += 2
				elif array[index] == 'A' and array[index + 1] == 'A':
					index += 1
				elif array[index] == 'P' and array[index + 1] == 'A':
					index += 1
				else:
					index += 2
			except:
				print(index, array)
				break


for _ in range(int(input())):
	n = int(input())
	L = input()
	m = s = 0
	for i in range(1, n + 1):
		if L[-i] == 'A':
			m = max(m, i - s - 1)
			s = i
	print(m)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	ma = 0
	if 'A' in a:
		t = 0
		p = a.index('A')
		for i in range(p + 1, n):
			if a[i] == 'P':
				t += 1
			else:
				ma = max(t, ma)
				t = 0
		ma = max(t, ma)
		print(ma)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	s1 = list(s)
	c = 0
	for j in range(1, n):
		x = j * 'P'
		if s.count('A' + x) > 0:
			c = j
	print(c)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	for _ in range(ii()):
		n = ii()
		s = list(si())
		ans = 0
		c = 0
		while True:
			c += 1
			f = 0
			i = 0
			while i < n - 1:
				if s[i] == 'A' and s[i + 1] == 'P':
					s[i + 1] = 'A'
					ans = c
					f = 1
					i += 2
				else:
					i += 1
			if f == 0:
				break
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input()) + ['A']
	cnt = 0
	for z in range(100):
		for i in range(n - 1, -1, -1):
			if arr[i] == 'A' and arr[i + 1] == 'P':
				arr[i + 1] = 'A'
				cnt = z + 1
	print(cnt)


t = int(input())
for i in range(t):
	num = int(input())
	a = input()
	ali = []
	for j in range(num):
		if a[j] == 'A':
			ali.append(j)
	ali.append(num)
	sa = [0]
	for j in range(len(ali) - 1):
		sa.append(ali[j + 1] - ali[j] - 1)
	print(max(sa))


def fun(n, str1):
	list1 = list(str1)
	c = 0
	while 1:
		k = 0
		i = 0
		while i < n - 1:
			if list1[i] == 'A' and list1[i + 1] == 'P':
				list1[i + 1] = 'A'
				i = i + 2
				k = k + 1
			else:
				i = i + 1
		if k > 0:
			c = c + 1
		else:
			return c
t = int(input())
for i in range(0, t):
	n = int(input())
	str1 = input()
	p = fun(n, str1)
	print(p)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input())
	i = -10 ** 9
	ans = [0] * n
	for j in range(n):
		if arr[j] == 'A':
			i = 0
			ans[j] = i
		else:
			i += 1
			ans[j] = i
	print(max(0, max(ans)))


from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, groupby
import sys
import bisect
import string
import math
import time
import random

def S_():
	return input()

def LS():
	return [i for i in input().split()]

def I():
	return int(input())

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def StoI():
	return [ord(i) - 97 for i in input()]

def ItoS(nn):
	return chr(nn + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def GI(V, E, Directed=False, index=0):
	org_inp = []
	g = [[] for i in range(n)]
	for i in range(E):
		inp = LI()
		org_inp.append(inp)
		if index == 0:
			inp[0] -= 1
			inp[1] -= 1
		if len(inp) == 2:
			(a, b) = inp
			g[a].append(b)
			if not Directed:
				g[b].append(a)
		elif len(inp) == 3:
			(a, b, c) = inp
			aa = (inp[0], inp[2])
			bb = (inp[1], inp[2])
			g[a].append(bb)
			if not Directed:
				g[b].append(aa)
	return (g, org_inp)

def bit_combination(k, n=2):
	rt = []
	for tb in range(n ** k):
		s = [tb // n ** bt % n for bt in range(k)]
		rt += [s]
	return rt

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
input = lambda : sys.stdin.readline().rstrip()

def ran_input():
	import random
	n = random.randint(4, 16)
	(rmin, rmax) = (1, 10)
	a = [random.randint(rmin, rmax) for _ in range(n)]
	return (n, a)
show_flg = False
show_flg = True
ans = 0
t = I()
for _ in range(t):
	n = I()
	s = input()
	ans = 0
	cnt = 0
	for i in range(n):
		c = s[n - i - 1]
		if c == 'P':
			cnt += 1
		else:
			ans = max(cnt, ans)
			cnt = 0
	print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	ptr = len(s)
	for i in range(0, len(s)):
		if s[i] == 'A':
			ptr = i
			break
	c = 0
	flag = 0
	for i in range(ptr, len(s)):
		if s[i] == 'P':
			c += 1
		else:
			flag = max(flag, c)
			c = 0
	flag = max(flag, c)
	print(flag)


cases = int(input())
for a in range(cases):
	b = input()
	students = list(str(input()))
	rounds = 0
	while True:
		i = 0
		inc = 0
		while i < len(students) - 1:
			if students[i] == 'A' and students[i + 1] == 'P':
				students[i + 1] = 'A'
				i += 2
				inc = 1
			else:
				i += 1
		if inc:
			rounds += 1
		else:
			break
	print(rounds)


def main():
	n = int(input())
	s = []
	k = []
	for i in range(n):
		tmps = int(input())
		tmpk = input()
		s.append(tmps)
		k.append(tmpk)
	for idx in range(n):
		tmps = s[idx]
		tmpk = k[idx]
		if tmps == 1:
			print(0)
			continue
		longest = 0
		prev = False
		cur = 'P'
		curlen = 0
		for i in range(tmps):
			if tmpk[i] == cur:
				curlen += 1
			elif curlen > longest and prev:
				longest = curlen
				curlen = 0
			else:
				prev = True
				curlen = 0
		if curlen > longest and prev:
			longest = curlen
		print(longest)
	return
main()


T = int(input())
for _ in range(T):
	n = int(input())
	s = list(input())
	m = 0
	k = 0
	f = 0
	for c in s:
		if c == 'P':
			k += 1 * f
			if k > m:
				m = k
		else:
			f = 1
			k = 0
	print(m)


for case in range(int(input())):
	k = int(input())
	s = input()
	s = s.lstrip('P')
	temp = s.split('A')
	ans = 0
	for x in temp:
		ans = max(ans, len(x))
	print(ans)


a = int(input())
for i in range(a):
	z = 0
	d = 0
	l = int(input())
	s = input()
	while s != s.replace('AP', 'AA'):
		s = s.replace('AP', 'AA')
		z += 1
	print(z)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(input())
	b = [0] * n
	deff = 0
	k = 0
	for j in range(n):
		if a[j] == 'A':
			deff = 1
			k += 1
		if deff == 1 and a[j] == 'P':
			b[k] += 1
	print(max(b))


from sys import stdin, stdout
read = lambda : map(int, stdin.readline().split())
I = lambda : stdin.readline().strip()
for _ in range(int(I())):
	n = int(I())
	s = I()
	flag = False
	count = 0
	maxx = 0
	for i in s + 'A':
		if flag:
			if i == 'A':
				maxx = max(count, maxx)
				count = 0
			else:
				count += 1
		if i == 'A':
			flag = True
	print(maxx)


t = int(input())
for _ in range(t):
	k = int(input())
	S = list(input())
	ans = 0
	while True:
		next_S = [S[0]]
		for i in range(1, k):
			if S[i] == 'P':
				if S[i - 1] == 'P':
					next_S.append('P')
				else:
					next_S.append('A')
			else:
				next_S.append('A')
		if S == next_S:
			print(ans)
			break
		ans += 1
		S = next_S


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input().rstrip().lstrip('P'))
	if not s:
		print(0)
		continue
	maxSeries = 0
	series = 0
	for c in s:
		if c == 'P':
			series += 1
		elif c == 'A':
			maxSeries = max(maxSeries, series)
			series = 0
	maxSeries = max(maxSeries, series)
	print(maxSeries)


def panduan(klist):
	if 'A' not in klist:
		return 0
	start = 0
	count = 0
	maxcount = -1
	for i in range(0, len(klist)):
		if klist[i] == 'A' and start == 0:
			start = 1
			if i == len(klist) - 1:
				return 0
			continue
		if klist[i] == 'A' and start > 0:
			if count > maxcount:
				maxcount = count
				count = 0
			else:
				count = 0
		if klist[i] == 'P' and start > 0:
			count = count + 1
		if i == len(klist) - 1:
			if count > maxcount:
				maxcount = count
				return maxcount
			else:
				return maxcount
t = int(input())
for i in range(0, t):
	n = int(input())
	klist = input()
	result = panduan(klist)
	print(result)


a = int(input())
for i in range(a):
	b = int(input())
	c = list(input())
	count = 0
	count1 = 0
	d = 0
	c += ['A']
	for j in range(len(c)):
		if c[j] == 'P':
			count1 += 1
		else:
			break
	for j in range(count1, len(c)):
		if c[j] == 'P':
			d += 1
		else:
			if d > count:
				count = d
			d = 0
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	c = 0
	g = []
	for i in range(n):
		if s[i] == 'P':
			c += 1
		if i == n - 1 or s[i] == 'A':
			g.append(c)
			c = 0
	g[0] = 0
	print(max(g))


test_cases = int(input())
for i in range(0, test_cases):
	total_students = int(input())
	student_string = input()
	angry = True
	result_time = int(0)
	while angry:
		result_time += 1
		angry = False
		skip = False
		for idx in range(0, total_students - 1):
			if skip:
				skip = False
				continue
			if student_string[idx] == 'A' and student_string[idx + 1] == 'P':
				skip = True
				angry = True
				student_string = student_string[:idx + 1] + 'A' + student_string[idx + 2:]
				idx += 1
	print(result_time - 1)


t = int(input())
for _ in range(t):
	k = int(input())
	if k == 0:
		continue
	s = input()
	s = s.lstrip('P')
	a = []
	num = 0
	for i in range(len(s)):
		if s[i] == 'A':
			a.append(num)
			num = 0
		else:
			num += 1
	if num != 0:
		a.append(num)
	if a == []:
		print(0)
		continue
	print(max(a))


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	broken = False
	for i in range(n):
		if s[i] == 'A':
			broken = True
			break
	if broken == False:
		print(0)
	else:
		s = s[i:] + 'A'
		ans = 0
		cnt = 0
		for c in s:
			if c == 'A':
				ans = max(ans, cnt)
				cnt = 0
			else:
				cnt += 1
		print(ans)


def angery(students):
	anger = False
	longest_p = 0
	long_p = 0
	for s in students:
		if s == 'A':
			anger = True
			longest_p = max(longest_p, long_p)
			long_p = 0
		if anger and s == 'P':
			long_p += 1
	longest_p = max(longest_p, long_p)
	return longest_p
T = int(input())
for t in range(T):
	input()
	print(angery(input()))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	maxx = 0
	ind = 0
	for el in s:
		if el == 'A':
			break
		else:
			ind += 1
	cur = 0
	for k in range(ind, len(s)):
		if s[k] == 'A':
			if cur > maxx:
				maxx = cur
			cur = 0
		else:
			cur += 1
	if cur > maxx:
		maxx = cur
	print(maxx)


import sys
input = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	k = int(input())
	s = input()
	a = [*(i for i in range(k) if s[i] == 'A'), k]
	print(max((j - i - 1 for (i, j) in zip(a, a[1:])), default=0))


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	l = list()
	for i in range(n):
		l.append(s[i])
	ans = 0
	for i in range(n):
		curr = 0
		j = 1
		while j < n:
			if l[j - 1] == 'A' and l[j] == 'P':
				curr = 1
				l[j] = 'A'
				j += 2
			else:
				j += 1
		ans += curr
	print(ans)


T = int(input())
for t in range(T):
	k = int(input())
	s = list(input())
	i = 0
	to_change = [i for (i, x) in enumerate(s) if i > 0 and s[i - 1] == 'A' and (s[i] == 'P')]
	while len(to_change) > 0:
		for tc in to_change:
			s[tc] = 'A'
		to_change = [i for (i, x) in enumerate(s) if i > 0 and s[i - 1] == 'A' and (s[i] == 'P')]
		i += 1
	print(i)


from sys import stdin, stdout
import math
from bisect import bisect_left, bisect_right
input = lambda : stdin.readline().strip()
print = lambda s: stdout.write(s)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	for i in range(n):
		if s[i] == 'A':
			break
	if s[i] == 'P':
		print('0\n')
	else:
		j = i
		m = 0
		while j < n:
			cnt = 0
			while j < n and s[j] == 'P':
				cnt += 1
				j += 1
			j += 1
			m = max(m, cnt)
		print(str(m) + '\n')


import re
for i in range(int(input())):
	l = int(input())
	str = input()
	count = 0
	while str.find('AP') >= 0:
		str = re.sub('AP', 'AA', str)
		count += 1
	print(count)


t = int(input())
for loop in range(t):
	k = int(input())
	s = input()
	la = None
	ans = 0
	for i in range(k):
		if s[i] == 'A':
			la = i
		elif la != None:
			ans = max(ans, i - la)
	print(ans)


from operator import itemgetter
t = int(input())

def solve(s):
	flag0 = False
	ans = 0
	tmp = 0
	for l in s:
		if l == 'A':
			if flag0:
				ans = max(ans, tmp)
			flag0 = True
			tmp = 0
		else:
			tmp += 1
	if flag0:
		ans = max(ans, tmp)
	print(ans)
for i in range(t):
	input()
	solve(input())


exec(int(input()) * 'n=int(input());print(max(0,*map(len,input().lstrip("P").split("A"))));')


t = int(input())
l1 = []
for i in range(t):
	n = int(input())
	s = input()
	l2 = []
	for j in range(len(s)):
		if s[j] == 'A':
			l2.append(j)
	l2.append(n)
	max1 = 0
	for k in range(1, len(l2)):
		if max1 < l2[k] - l2[k - 1] - 1:
			max1 = l2[k] - l2[k - 1] - 1
	l1.append(max1)
for i in range(t):
	print(l1[i])


final = []
for i in range(int(input())):
	t = int(input())
	word = input()
	count = 1
	length = []
	true = 0
	m = 0
	if len(word) > 1:
		for i in range(1, len(word)):
			if word[i - 1] == word[i]:
				count += 1
			else:
				length.append([word[i - 1], str(count)])
				count = 1
		length.append([word[i], str(count)])
	else:
		i = 0
		length.append([word[i], str(count)])
	ans = 0
	for j in length:
		if j[0] == 'A':
			true = 1
		elif j[0] == 'P' and true:
			ans = int(j[1])
		if ans > m:
			m = ans
	final.append(m)
for i in final:
	print(i)


(t,) = map(int, input().split())
for _ in range(t):
	(c,) = map(int, input().split())
	s = input().strip()
	r = 0
	f = False
	rr = 0
	for c in s:
		if c == 'A':
			rr = max(r, rr)
			r = 0
			f = 1
		elif f:
			r += 1
	rr = max(r, rr)
	print(rr)


t = int(input())
for test in range(t):
	n = int(input())
	x = input()
	maxlen = 0
	current = 0
	flag = False
	for i in range(n):
		if x[i] == 'A':
			flag = True
			maxlen = max(maxlen, current)
			current = 0
		elif flag:
			current += 1
	maxlen = max(maxlen, current)
	print(maxlen)


def foo(s, n):
	tm = 0
	s = list(s)
	i = 0
	while i < n:
		mx_count = 0
		if s[i] == 'A':
			i += 1
			while i < n and s[i] == 'P':
				mx_count += 1
				i += 1
		else:
			i += 1
		tm = max(tm, mx_count)
	return tm
n = int(input())
for i in range(n):
	t = int(input())
	print(foo(input().strip(), t))


t = int(input())
while t:
	n = int(input())
	s = input()
	l = list(s)
	ans = 0
	cnt = 0
	after = 0
	for i in range(n - 1, -1, -1):
		if s[i] == 'A':
			if ans < cnt:
				ans = cnt
				cnt = 0
			cnt = 0
		else:
			cnt += 1
	print(ans)
	t -= 1


t = int(input())
while t > 0:
	n = int(input())
	s = list(input())
	m = 0
	k = 0
	flag = 0
	for i in range(n):
		if s[i] == 'A':
			flag = 1
			k = i
		elif flag == 1:
			m = max(m, i - k)
	print(m)
	t -= 1


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7
for _ in range(iinput()):
	n = iinput()
	s = input()
	ans = 0
	temp = 0
	if s.count('A') > 0:
		start = s.index('A')
	else:
		start = n
	for i in range(start, n):
		if s[i] == 'P':
			temp += 1
		else:
			ans = max(ans, temp)
			temp = 0
	print(max(temp, ans))


t = int(input())
for i in range(t):
	length = int(input())
	k = str(input())
	k += '!'
	arr = [str(i) for i in k]
	answer = [0]
	for j in range(len(arr)):
		if arr[j] == 'A':
			pos = j + 1
			count = 0
			while arr[pos] == 'P':
				count += 1
				pos += 1
			answer.append(count)
	print(max(answer))


import atexit
import io
import sys
input = sys.stdin.readline
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
for _ in range(int(input())):
	n = int(input())
	s = input()
	if 'A' in s:
		ai = s.index('A')
		c = 0
		max_val = 0
		for i in s[ai:]:
			if i == 'P':
				c += 1
			else:
				max_val = max(c, max_val)
				c = 0
		print(max_val)
	else:
		print(0)


import os, sys
from io import BytesIO, IOBase
from fractions import Fraction

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
import math
for i in range(ii()):
	k = ii()
	s = si()
	l = []
	for i in range(len(s)):
		if s[i] == 'A':
			l.append(i)
	l.append(k)
	if len(l) == 1:
		print(0)
	else:
		l1 = []
		mx = 0
		for i in range(len(l) - 1):
			l1.append(l[i + 1] - l[i])
		print(max(l1) - 1)


t = int(input())
for _ in range(t):
	n = int(input())
	print(max(list(map(len, input().lstrip('P').split('A')))))


t = int(input())
for i in range(t):
	n = int(input())
	ar = input()
	maxc = 0
	count = -1
	for i in ar:
		if i == 'A' and count != -1:
			maxc = max(count, maxc)
			count = 0
		if i == 'A':
			count = 0
		elif count != -1:
			count += 1
	print(max(maxc, count))


for _ in range(int(input())):
	n = int(input())
	s = input()
	ma = 0
	ps = 0
	a = False
	for i in range(n):
		if a == True and s[i] == 'P':
			ps += 1
		elif s[i] == 'A':
			ma = max(ma, ps)
			ps = 0
			a = True
	ma = max(ma, ps)
	print(ma)


t = int(input())
for case in range(t):
	k = int(input())
	s = input()
	flag_saw_angry = False
	max_inrow_peaceful = 0
	inrow_peaceful = 0
	for item in s:
		if item == 'A':
			flag_saw_angry = True
			if inrow_peaceful > max_inrow_peaceful:
				max_inrow_peaceful = inrow_peaceful
			inrow_peaceful = 0
		if item == 'P':
			if flag_saw_angry:
				inrow_peaceful += 1
	if inrow_peaceful > max_inrow_peaceful:
		max_inrow_peaceful = inrow_peaceful
	print(max_inrow_peaceful)


for _ in range(int(input())):
	n = int(input())
	s = input()
	c = -1
	for i in range(n):
		if s[i] == 'P':
			c = i
		else:
			break
	if c >= 0:
		s = s[c + 1:]
		n = n - (c + 1)
	z = [0]
	flag = 1
	count = 0
	for i in range(n):
		if s[i] == 'A' and flag == 0:
			z.append(count)
			count = 0
			flag = 1
		elif s[i] == 'P':
			flag = 0
			count += 1
	z.append(count)
	print(max(z))


import sys
input = sys.stdin.readline
t = int(input().strip())
while t > 0:
	k = int(input().strip())
	s = input().strip()
	tmp = 1
	ans = 0
	change = 0
	for i in range(0, k - 1):
		if s[i] == 'A' and s[i + 1] == 'P':
			change += 1
			s = s[i:]
			break
	for i in range(0, len(s) - 1):
		if s[i] == 'P' and s[i] == s[i + 1]:
			tmp += 1
			ans = max(tmp, ans)
		else:
			ans = max(tmp, ans)
			tmp = 1
		if s[i] != s[i + 1]:
			change += 1
	if change is 0 or (change is 1 and s[-1] == 'A'):
		ans = 0
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	p = 0
	ma = 0
	while p < len(s) - 1:
		if s[p] == 'A':
			p2 = p + 1
			while p2 < len(s) and s[p2] == 'P':
				p2 += 1
			if p2 - p > ma:
				ma = p2 - p - 1
		p += 1
	print(ma)


from sys import stdin
for _ in range(int(stdin.readline().strip())):
	n = int(stdin.readline().strip())
	res = 0
	s = stdin.readline().strip()
	previous_s = s
	while True:
		s = s.replace('AP', 'AA')
		if previous_s == s:
			break
		res += 1
		previous_s = s
	print(res)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	s = list(s)
	s.append('#')
	(x, y) = (-1, -1)
	maxi = 0
	for (j, i) in enumerate(s):
		if n == j:
			break
		if i == 'A':
			if x == -1:
				x = j
				continue
			if maxi < j - x - 1:
				y = 1
				maxi = j - x - 1
			x = j
	if x != n and x != -1:
		if n - x - 1 > maxi:
			maxi = n - x - 1
	print(maxi)


for _ in range(int(input())):
	n = int(input())
	S = input()
	streak = 0
	prevA = False
	ans = 0
	for i in range(n):
		if prevA == True and S[i] == 'P':
			streak += 1
		if S[i] == 'A':
			prevA = True
			streak = 0
		ans = max(ans, streak)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	b = []
	c = 0
	for j in range(n):
		if a[j] == 'P':
			c = c + 1
		else:
			b.append(c)
			c = 0
	b.append(c)
	if len(b) == 1:
		print(0)
	else:
		print(max(b[1:]))


t = int(input())
while t:
	l = int(input())
	str = input().split('A')
	if str.index(max(str)):
		print(len(max(str)))
	else:
		str.remove(max(str))
		if len(str):
			print(len(max(str)))
		else:
			print(0)
	t = t - 1


while True:
	x = int(input(''))
	if 1 <= x <= 100:
		break
d = []
for i in range(x):
	while True:
		n = int(input(''))
		if 1 <= n <= 100:
			break
	while True:
		test = True
		ch = input('')
		if len(ch) == n:
			for j in range(n):
				if ch[j] not in ['A', 'P']:
					test = False
					break
		else:
			test = False
		if test == True:
			break
	d.append(ch)
for i in range(x):
	mx = 0
	k = 0
	while k < len(d[i]) - 1:
		if d[i][k] == 'A':
			nb = 0
			for j in range(k + 1, len(d[i])):
				if d[i][j] == 'P':
					nb = nb + 1
					if j == len(d[i]) - 1:
						if mx < nb:
							mx = nb
						k = j
				else:
					if mx < nb:
						mx = nb
					k = j
					break
		else:
			k = k + 1
	print(mx)


import sys, math
input = sys.stdin.readline
t = int(input())
while t > 0:
	n = int(input())
	a = input().strip()
	ans = 0
	j = 0
	c = 0
	for i in range(1, n):
		if a[j] == 'A':
			if a[i] == 'P':
				c += 1
			else:
				if c > ans:
					ans = c
				c = 0
				j = i
		else:
			j += 1
	if c > ans:
		ans = c
	print(ans)
	t -= 1


q = int(input())

def engine(s):
	s = list(s)
	temp = ans = boo = 0
	for i in s:
		if i == 'A':
			boo = 1
			temp = 0
			continue
		if i == 'P' and boo == 1:
			temp += 1
		if ans < temp:
			ans = temp
	return ans
for i in range(q):
	n = int(input())
	s = input()
	print(engine(s))


for _ in range(int(input())):
	n = int(input())
	word = list(input())
	b = 0
	for i in range(n):
		if word[i] == 'A':
			a = 0
			for j in range(i + 1, n):
				if word[j] == 'P':
					a += 1
				else:
					break
			b = max(b, a)
	print(b)


n = int(input())
for i in range(n):
	a = int(input())
	s = list(input().split('A'))
	a = []
	for i in s:
		a.append(len(i))
	if len(a) > 1:
		print(max(a[1:]))
	else:
		print(0)


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = []
	c = -1
	f = 0
	for i in range(len(s)):
		if s[i] == 'A':
			f = 1
			l.append(c)
			c = 0
		elif f == 1:
			c += 1
		else:
			c = 0
	l.append(c)
	print(max(l))


t = int(input())
for i in range(t):
	k = int(input())
	a = input()
	angry = False
	patient = 0
	ans = 0
	for j in range(len(a)):
		if a[j] == 'A':
			angry = True
			ans = max(ans, patient)
			patient = 0
		elif a[j] == 'P' and angry:
			patient += 1
	ans = max(ans, patient)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = str(input())
	try:
		k = s.index('A')
		arr = []
		count = 0
		for i in range(k, n):
			if s[i] == 'P':
				count += 1
			else:
				arr.append(count)
				count = 0
		if count > 0:
			arr.append(count)
		print(max(arr))
	except:
		print(0)


t = int(input())
for i in range(t):
	k = int(input())
	row = input()
	a = row.find('A')
	if a == -1:
		print(0)
		continue
	b = row.find('A', a + 1)
	dist = 0
	while 0 < b < len(row):
		dist = max(dist, b - 1 - a)
		a = b
		b = row.find('A', b + 1)
	dist = max(dist, len(row) - 1 - a)
	print(dist)


for _ in range(int(input())):
	k = int(input())
	m = 0
	x = 0
	for c in input().lstrip('P'):
		if c == 'A':
			x = 0
		else:
			x += 1
			m = max(m, x)
	print(m)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = 0
	te = 0
	a = False
	for i in range(n):
		if s[i] == 'A':
			m = max(m, te)
			te = 0
			a = True
		elif a:
			te += 1
	m = max(m, te)
	print(m)


a = int(input())
r = []
for i in range(a):
	b = int(input())
	c = input()
	if 'A' in c:
		d = c[c.find('A'):]
		d = d.split('A')
		k = [len(i) for i in d]
		r.append(max(k))
	else:
		r.append(0)
for i in r:
	print(i)


for _ in range(int(input())):
	k = int(input())
	s = input()
	c = 0
	for i in range(k):
		if s[i] == 'A' and s[i + 1:].count('P') > 0:
			ind = s.find('A', i + 1)
			if ind != -1:
				c = max(c, ind - i - 1)
			else:
				c = max(c, k - i - 1)
	print(c)


for i in range(int(input())):
	n = int(input())
	a = input()
	l = []
	for j in a:
		l.append(j)
	c = l.count('A')
	if c == 1 and l[len(l) - 1] != 'A':
		d = l.index('A')
		print(len(l) - 1 - d)
		continue
	i = [0]
	for j in range(len(l)):
		if l[j] == 'A':
			for k in range(j + 1, len(l)):
				if l[k] == 'A':
					i.append(k - j - 1)
					break
	l.reverse()
	for j in range(len(l)):
		if l[j] == 'A' and j != 0 and (j != len(l) - 1):
			if l[0] == 'A':
				i.append(j - 1)
			else:
				i.append(j)
			break
	i.sort()
	print(i[len(i) - 1])


import sys
tests = int(sys.stdin.readline())
for _ in range(tests):
	n = int(sys.stdin.readline())
	string = list(sys.stdin.readline())
	encounter = False
	last_ind = -1
	max_seen = 0
	mm = 0
	for i in range(len(string)):
		if string[i] == 'A':
			encounter = True
		if encounter and string[i] == 'P':
			if i == last_ind + 1 or last_ind == -1:
				max_seen += 1
			else:
				max_seen = 1
			last_ind = i
		mm = max(max_seen, mm)
	print(mm)


def main():

	def solve():
		k = int(input())
		ss = list(input())
		for i in range(k - 1):
			if ss[i] == 'A':
				break
		else:
			print(0)
			return
		ss = ss[i:k]
		mx = 0
		count = 0
		for s in ss:
			if s == 'A':
				count = 0
			else:
				count += 1
				if count > mx:
					mx = count
		print(mx)
	q = int(input())
	for _ in range(q):
		solve()
main()


T = int(input())
for t in range(T):
	n = int(input())
	a = list(input())
	a.append('A')
	M = 0
	if a.count('A') == 1:
		print(0)
	else:
		last = a.index('A')
		for i in range(last + 1, n + 1):
			if a[i] == 'A':
				if i - last - 1 > M:
					M = i - last - 1
				last = i
		print(M)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	l = 0
	count = 0
	start = False
	for c in s:
		if c == 'A':
			l = max(l, count)
			count = 0
			start = True
		elif start:
			count += 1
	l = max(l, count)
	print(l)


t = int(input())
while t > 0:
	n = input()
	l1 = list(input())
	while 1:
		if len(l1) == 0:
			break
		elif l1[0] == 'P':
			l1.remove(l1[0])
		else:
			break
	if len(l1) == 0:
		print('0')
	else:
		c = 0
		l = 0
		for i in l1:
			if i == 'P':
				c += 1
			elif c > l:
				l = c
				c = 0
			else:
				c = 0
		if l == 0:
			print(c)
		else:
			if c > l:
				l = c
			print(l)
	t -= 1


t = int(input())
for i in range(t):
	k = int(input())
	s = str(input())
	s = s[s.find('A'):]
	if s.count('A') == 0:
		print(0)
		continue
	maxi = 0
	l = 0
	for j in s:
		if j == 'A':
			l = 0
		if j == 'P':
			l += 1
		if maxi < l:
			maxi = l
	print(maxi)


t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	l = []
	l = list(s)
	count = 0
	while 1:
		flag = 0
		i = 0
		while i < len(l) - 1:
			if l[i] == 'A' and l[i + 1] == 'P':
				l[i + 1] = 'A'
				flag = 1
				i += 2
			else:
				i += 1
		if flag == 0:
			break
		count += 1
	print(count)


for i in range(int(input())):
	k = int(input())
	BIG = input()
	max_ = 0
	calcer = 0
	f = 0
	for j in range(k):
		if BIG[j] == 'A':
			f = 1
			max_ = max(calcer, max_)
			calcer = 0
		if BIG[j] == 'P' and f:
			calcer += 1
	max_ = max(max_, calcer)
	print(max_)


t = int(input())
for i in range(t):
	lent = int(input())
	stu = input()
	maxi = 0
	count = 0
	found = False
	for student in range(lent):
		if stu[student] == 'P' and (not found):
			pass
		elif stu[student] == 'P' and found:
			count = count + 1
			if student == lent - 1 and count >= maxi:
				maxi = count
		elif stu[student] == 'A' and found:
			if count >= maxi:
				maxi = count
			count = 0
		elif stu[student] == 'A' and (not found):
			found = True
	print(maxi)


n = int(input())
for i in range(n):
	m = int(input())
	s = str(input())
	s = s[::-1]
	e = 0
	l = []
	for i in range(0, len(s)):
		if s[i] == 'A':
			d = s[0:i]
			e = d.count('P')
			l.append(e - sum(l))
			e = 0
	if len(l) > 0:
		print(max(l))
	else:
		print(0)


def solve(s):
	last = -1
	c = 0
	m = 0
	for x in range(len(s)):
		if s[x] == 'A':
			last = x
			c = 0
		elif last != -1:
			c += 1
			m = max(c, m)
	return m
n = int(input())
for x in range(n):
	k = int(input())
	s = input()
	print(solve(s))


for _ in range(int(input())):
	input()
	s = input()
	no_evil = False
	if s[0] == 'P':
		ind = 1
		while ind < len(s):
			if s[ind] == 'A':
				break
			ind += 1
		s = s[ind:]
	s += 'A'
	max_row = 0
	inside_row = False
	curr_row = 0
	for i in range(len(s)):
		if inside_row:
			if s[i] == 'P':
				curr_row += 1
			else:
				max_row = max(max_row, curr_row)
				inside_row = True
				curr_row = 0
		elif s[i] == 'P':
			curr_row = 1
			inside_row = True
	print(max_row)


T = int(input())
for i in range(T):
	X = int(input())
	Y = input()
	ans = 0
	count = 0
	start = False
	for j in range(len(Y)):
		if Y[j] == 'A':
			start = True
			ans = max(ans, count)
			count = 0
		elif start:
			count += 1
	ans = max(ans, count)
	print(ans)


for _ in range(0, int(input())):
	n = int(input())
	c = 0
	m = 0
	f = False
	for i in input():
		if i == 'A':
			c = 0
			f = True
		if i == 'P' and f:
			c += 1
			m = max(m, c)
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	ar = list(input())
	ma = 0
	m = 0
	fl = False
	for i in ar:
		if i == 'A':
			fl = True
	if fl:
		x = ar.index('A')
		for i in range(x, n):
			if ar[i] == 'P':
				ma += 1
			else:
				m = max(m, ma)
				ma = 0
		m = max(m, ma)
		print(m)
	else:
		print('0')
		continue


def I():
	return map(int, input().split())
for _ in range(int(input())):
	input()
	s = sorted(input().lstrip('P').rstrip('A').split('A'))
	print(len(s[-1]))


import os
import sys
from io import BytesIO, IOBase
import itertools

def solve(n, a):
	a = list(a)
	if 'A' not in a:
		return 0
	b = [(label, sum((1 for _ in group))) for (label, group) in itertools.groupby(a[a.index('A'):])]
	return max([t[1] if t[0] == 'P' else 0 for t in b])

def main():
	for _ in range(int(input().strip())):
		n = int(input().strip())
		a = list(input().strip())
		print(solve(n, a))
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


for _ in range(int(input())):
	n = int(input())
	l = input()
	if 'A' in l:
		x = [l.find('A')]
		d = 0
		for i in range(x[0] + 1, n):
			if l[i] == 'A':
				d = max(d, i - x[-1] - 1)
				x.append(i)
		print(max(d, n - 1 - x[-1]))
	else:
		print(0)


n = int(input())
for i in range(n):
	a = int(input())
	l = input()
	if 'A' not in l:
		print(0)
	else:
		x = l.index('A')
		l = l[x:].split('A')
		print(len(max(l)))


t = int(input())
for _ in range(t):
	s = int(input())
	s1 = input()
	count = 0
	q = 0
	l = []
	for i in range(s):
		if s1[i] == 'A':
			count = 0
			q = 1
		elif q == 1:
			count += 1
		l.append(count)
	m = max(l)
	print(m)


for i in range(int(input())):
	n = int(input())
	kids = input()
	first = kids.find('A')
	kids = kids[first:]
	arr = kids.split('A')
	max = 0
	for i in range(len(arr)):
		if len(arr[i]) > max:
			max = len(arr[i])
	if first == -1:
		print(0)
	else:
		print(max)


from sys import stdin
r = stdin.readline

def solve(line):
	(i, n, ans, ac) = (0, len(line), 0, 0)
	state = 0
	while i < n:
		if state == 0 and line[i] == 'A':
			state = 1
		elif state == 1 and line[i] == 'P':
			ac += 1
		elif state == 1 and line[i] == 'A':
			ans = max(ans, ac)
			ac = 0
		i += 1
	ans = max(ans, ac)
	return ans

def main():
	cases = int(r())
	for case in range(cases):
		n = int(r())
		line = r().strip()
		print(solve(line))
main()


for i in range(int(input())):
	input()
	students = input()
	count = 0
	res = 0
	first = False
	for c in students:
		if not first:
			if c == 'A':
				first = True
			continue
		if c == 'P':
			count += 1
		else:
			res = max(res, count)
			count = 0
	print(max(res, count))


for i in range(int(input())):
	n = int(input())
	s = input()
	d = []
	c = -1
	for i in s:
		if i == 'A':
			d.append(c)
			c = 0
		elif i == 'P' and c != -1:
			c += 1
	d.append(c)
	if max(d) != -1:
		print(max(d))
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	i = 0
	mx = 0
	flag = False
	while i < n:
		cnt = 0
		if st[i] == 'A':
			flag = True
			i += 1
		if flag:
			while i < n and st[i] != 'A':
				cnt += 1
				i += 1
			mx = max(cnt, mx)
		i += 1
	print(mx)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	k = int(sys.stdin.readline())
	S = sys.stdin.readline()
	m = 0
	i = 0
	c = 0
	while i != k:
		if S[i] == 'P':
			if c != 0:
				c += 1
		elif S[i] == 'A':
			if c != 0:
				if m < c - 1:
					m = c - 1
			c = 1
		i += 1
	if m < c - 1:
		m = c - 1
	print(m)


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [0] * n
	for i in range(1, n):
		if s[i - 1:i + 1] == 'AP':
			a[i] = 1
		elif a[i - 1] and s[i] != 'A':
			a[i] = a[i - 1] + 1
	print(max(a))


for t in range(int(input())):
	n = int(input())
	s = input()
	(a, maxi) = ([i for i in range(n) if s[i] == 'A'], 0)
	if len(a) == 0 or len(a) == n:
		print(0)
	elif len(a) == 1:
		print(n - a[0] - 1)
	else:
		for i in range(1, len(a)):
			maxi = max(maxi, a[i] - a[i - 1] - 1)
		print(max(maxi, n - a[-1] - 1))


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	N = int(input())
	S = input().rstrip()
	Query.append((N, S))
for (N, S) in Query:
	ang = False
	c = 0
	ans = 0
	for i in range(N):
		if S[i] == 'A':
			ang = True
			c = 0
		elif ang:
			c += 1
			ans = max(ans, c)
	print(ans)


def angry(string):
	index = 0
	for c in string:
		if c == 'A':
			break
		index += 1
	spl = string[index:].split('A')
	lens = [len(s) for s in spl]
	print(max(lens))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	angry(s)


for _ in range(int(input())):
	n = int(input())
	string = input()
	ind = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		if string[i] == 'A':
			ans = max(ans, ind)
			ind = 0
		else:
			ind += 1
	print(ans)


reps = int(input())
ans = []
while reps > 0:
	total_length = input()
	str1 = input()
	if 'A' in str1:
		split_str = str1.split('A')
		if str1[0] != 'A':
			del split_str[0]
		ans.append(max([len(x) for x in split_str]))
	else:
		ans.append(0)
	reps -= 1
for x in ans:
	print(x)


for t in range(int(input())):
	k = int(input())
	s = input()
	a = 0
	for (i, c) in enumerate(s):
		counter = 0
		if c == 'A':
			for x in range(i + 1, len(s)):
				if s[x] == 'P':
					counter += 1
				if s[x] == 'A':
					break
		if counter > a:
			a = counter
	print(a)


t = int(input())
for test in range(t):
	k = int(input())
	s = input()
	curr = 0
	maxx = 0
	for i in range(1, k):
		if s[i - 1] == 'A' and s[i] == 'P':
			curr += 1
		elif curr > 0 and s[i] == 'P' and (s[i - 1] == 'P'):
			curr += 1
		else:
			maxx = max(maxx, curr)
			curr = 0
	maxx = max(maxx, curr)
	print(maxx)


for _ in range(int(input())):
	n = int(input())
	s = input()
	last = -1
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			if last == -1:
				last = i
			else:
				ans = max(ans, i - last - 1)
				last = i
	if last != -1:
		ans = max(ans, n - last - 1)
	print(ans)


def func(s):
	ans = 0
	cnt = -10000
	for c in s:
		if c == 'A':
			cnt = 0
		else:
			cnt += 1
		ans = max(ans, cnt)
	return ans
t = int(input())
for _ in range(t):
	k = int(input())
	s = input()
	print(func(s))


t = int(input())
for i in range(0, t):
	n = int(input())
	s = input()
	p = []
	g = 0
	for j in s:
		p.append(j)
	m = p[0:]
	for j in range(0, len(s)):
		f = 0
		for k in range(0, len(p) - 1):
			if p[k] == 'A' and p[k + 1] == 'P':
				m[k + 1] = 'A'
				f = 1
		p = m[0:]
		if f == 1:
			g = g + 1
		if 'P' not in p:
			break
	print(g)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	s = s + 'A'
	(ok, res, ans) = (0, 0, 0)
	for x in s:
		if x == 'P' and ok:
			ans += 1
		if x == 'A':
			res = max(res, ans)
			ok = 1
			ans = 0
	print(res)


n = int(input())
w = ''
lst = []
for i in range(n):
	lst = []
	n1 = int(input())
	v = input()
	if v.count('A') >= 1:
		n2 = v[v.index('A'):]
		s = n2.split('A')
		for j in s:
			if j != '':
				lst.append(len(j))
		if lst == []:
			print('0')
		else:
			print(max(lst))
	else:
		print('0')


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	res = 0
	curr = int(''.join(['1' if i == 'A' else '0' for i in s]), 2)
	nxt = curr >> 1
	while curr | nxt != curr:
		res += 1
		curr = curr | nxt
		nxt = curr >> 1
	print(res)


from sys import stdin
t = int(stdin.readline())
while t:
	n = int(stdin.readline())
	s = input()
	temp = -1
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			ans = max(ans, temp)
			temp = 0
		elif temp != -1:
			temp += 1
	ans = max(temp, ans)
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	k = int(input())
	a = input()
	m = 0
	s = 0
	flag = False
	for j in a:
		if j == 'A':
			flag = True
			if s > m:
				m = s
			s = 0
		elif flag:
			s += 1
	if s > m:
		m = s
	print(m)


t = int(input())
for hatt in range(t):
	k = int(input())
	s = input()
	maxx = 0
	coun = 0
	flag = False
	for i in s:
		if i == 'A':
			flag = True
			coun = 0
		elif flag:
			coun += 1
			maxx = max(maxx, coun)
	print(maxx)


for i in range(int(input())):
	n = int(input())
	a = list(input())
	cntp = 0
	cnta = 0
	maxi = 0
	for i in a:
		if i == 'P':
			if cnta > 0:
				cntp += 1
				if cntp > maxi:
					maxi = cntp
		if i == 'A':
			cntp = 0
			cnta += 1
	print(maxi)


def anger(s):
	ans = 0
	while True:
		ang = False
		i = 0
		while i < len(s):
			if s[i] == 'A' and i + 1 != len(s) and (s[i + 1] != 'A'):
				s[i + 1] = 'A'
				i += 1
				ang = True
			i += 1
		if ang == False:
			return ans
		else:
			ans += 1
t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	print(anger(list(s)))


t = int(input())
for _ in range(t):
	n = int(input())
	l = input().strip()
	s = list(l)
	i = 0
	try:
		while s[i] != 'A':
			i += 1
		print(len(max(l[i:].split('A'))))
	except:
		print('0')


for _ in range(int(input())):
	n = int(input())
	s = [i == 'A' for i in list(input())]
	tim = [s]
	ans = 0
	while True:
		t = [s[0]]
		for i in range(n - 1):
			if tim[-1][i] == 1:
				t.append(True)
			else:
				t.append(tim[-1][i + 1])
		if tim[-1] == t:
			break
		ans += 1
		tim.append(t)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = []
	for i in range(n):
		if s[i] == 'A':
			d.append(i)
	d.append(n)
	diff = 0
	for i in range(len(d) - 1):
		diff = max(diff, d[i + 1] - d[i] - 1)
	print(diff)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	i = c = 0
	res = 0
	while i < n:
		if s[i] == 'A':
			i += 1
			c = 0
			while i < n and s[i] == 'P':
				i += 1
				c += 1
			res = max(res, c)
		else:
			i += 1
	print(res)
	t -= 1


def angry(s):
	if len(s) == 1:
		return 0
	elif s.count('A') == 1 and s[-1] == 'A':
		return 0
	elif s.count('A') == 0:
		return 0
	else:
		ii = s.index('A')
		ls = s[ii:].split('A')
		return len(max(ls))
tc = int(input())
for i in range(tc):
	n = int(input())
	ss = input()
	print(angry(ss))


t = int(input())
for zz in range(t):
	n = int(input())
	a = input()
	try:
		sI = a.index('A')
		c = 0
		mc = 0
		for i in range(sI + 1, n):
			if a[i] == 'P':
				c += 1
			else:
				mc = max(c, mc)
				c = 0
		mc = max(mc, c)
		print(mc)
	except:
		print(0)


from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
	n = I()
	for _ in range(n):
		t = I()
		s = input()
		ans = [0] * t
		for j in range(t):
			if s[j] == 'A':
				break
		for i in range(j + 1, t):
			if s[i] != 'A':
				ans[i] = ans[i - 1] + 1
		print(max(ans))
	return
solve()


from math import ceil
from sys import stdout
import random

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
t = ii()
while t:
	t -= 1
	n = ii()
	s = si()
	b = []
	b.append(0)
	f = 0
	c = n
	for i in range(n):
		if s[i] == 'A':
			if f == 0:
				c = i + 1
				f = 1
			else:
				x = i - c
				if x < 0:
					x = 0
				b.append(x)
				c = i + 1
	b.append(n - c)
	print(max(b))


t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	maxc = 0
	q = 0
	count = 0
	for i in range(0, n):
		if s[i] == 'A':
			q = 1
			count = 0
		if q == 1 and s[i] == 'P':
			count += 1
		maxc = max(maxc, count)
	print(maxc)


t = int(input())
for q in range(t):
	s = int(input())
	ap = input()
	pgroups = []
	for x in range(s - 1):
		try:
			if ap[x] == 'A' and ap[x + 1] == 'P':
				a2 = ap[x + 1:].index('A')
				pgroups.append(a2)
		except:
			pgroups.append(s - 1 - x)
	if len(pgroups) == 0:
		print(0)
	else:
		print(max(pgroups))


for t in range(int(input())):
	x = int(input())
	s = input()
	c = 0
	arr = []
	s = s.lstrip('P')
	for i in range(len(s)):
		if s[i] == 'P':
			c += 1
		else:
			arr.append(c)
			c = 0
	arr.append(c)
	print(max(arr))


for i in range(int(input())):
	n = int(input())
	s = input()
	j = 0
	count = 0
	for j in range(0, n - 1):
		temp = 0
		if s[j] == 'A':
			for k in range(j + 1, n):
				if s[k] == 'P':
					temp = temp + 1
				else:
					count = max(count, temp)
					temp = 0
					break
				count = max(count, temp)
	print(count)


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	res = 0
	if s.count('A') != 0:
		l = s.index('A')
		s = s[l + 1:]
		while s != '':
			if s.count('A') != 0:
				l = s.index('A')
				s = s[l + 1:]
			else:
				l = len(s)
				s = ''
			if l > res:
				res = l
	print(res)


for i in range(int(input())):
	b = int(input())
	a = input()
	i = -1
	s = 0
	for c in range(len(a)):
		if a[c] == 'A':
			if i == -1:
				i = c
			else:
				s = max(s, c - i - 1)
				i = c
	if i != -1:
		print(max(s, len(a) - i - 1))
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	x = 0
	m = 0
	o = s[::-1]
	if s.count('A') == 0:
		print(0)
	elif s.count('P') == 0:
		print(0)
	else:
		y = s.index('A')
		z = n - o.index('P') - 1
		if y > z:
			print(0)
		else:
			i = s.index('A')
			while i <= len(s) - 1:
				if i < len(s) and s[i] == 'P':
					while i < len(s) and s[i] == 'P':
						x += 1
						i += 1
					if x > m:
						m = x
				else:
					i += 1
				x = 0
			print(m)


t = int(input())
for i in range(t):
	n = int(input())
	grp = input()
	s = 0
	count = 0
	for j in range(n):
		if grp[j] == 'A':
			count = 1
		elif count >= 1:
			count = count + 1
		s = max(s, count)
	s = max(s, count)
	if s == 0:
		print(0)
	else:
		print(max(s - 1, 0))


for t in range(int(input())):
	k = int(input())
	s = input()
	if 'A' not in s:
		print(0)
	else:
		pos = [i + 1 for i in range(k) if s[i] == 'A']
		pos.append(k + 1)
		print(max([pos[i] - pos[i - 1] - 1 for i in range(1, len(pos))]))


for i in range(int(input())):
	n = int(input())
	a = list(input())
	mx = 0
	j = 0
	t = 1
	for i in range(n):
		if t == 1:
			if a[i] == 'A':
				t = 0
		elif a[i] == 'A':
			mx = max(j, mx)
			j = 0
		else:
			j += 1
	mx = max(mx, j)
	print(mx)


for _ in range(int(input())):
	n = int(input())
	s = input()
	(i1, i2) = (-1, -1)
	ans = -1
	for i in range(n):
		if s[i] == 'A':
			i1 = i
		if s[i] == 'P' and i == n - 1:
			i2 = i
			if i1 != -1:
				ans = max(ans, i2 - i1)
		if s[i] == 'P' and i < n - 1:
			if s[i + 1] == 'A':
				i2 = i
				if i1 != -1:
					ans = max(ans, i2 - i1)
	if ans == -1:
		print(0)
	else:
		print(ans)


count = int(input())
for i in range(count):
	s = input()
	s = input()
	lst = [len(x) for x in s.split('A')]
	if s[0] == 'P':
		lst.pop(0)
	if len(lst) == 0:
		print(0)
	else:
		print(max(lst))


c = []
for i in range(int(input())):
	n = int(input())
	b = list(input())
	if list(set(b)) == ['P']:
		c.append(0)
		continue
	k = 0
	a = []
	for j in range(b.index('A'), n):
		a.append(b[j])
	n = len(a)
	while list(set(a)) != ['A']:
		for j in range(n - 1):
			if a[j] == 'A' and a[j + 1] == 'P':
				a[j + 1] = 'a'
		for j in range(n):
			if a[j] == 'a':
				a[j] = 'A'
		k += 1
	c.append(k)
for i in range(len(c)):
	print(c[i])


import sys
input = lambda : sys.stdin.readline()
n = int(input())
for i in range(n):
	her = int(input())
	a = list(input().strip()) + ['A']
	ans = 0
	prev = -1
	for j in range(her + 1):
		if a[j] == 'A':
			if prev == -1:
				prev = j
			else:
				ans = max(ans, j - prev - 1)
				prev = j
	print(ans)


import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7
t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	ans = 0
	cnt = 0
	first_a = False
	for j in s:
		if j == 'A':
			first_a = True
			ans = max(ans, cnt)
			cnt = 0
		elif first_a:
			cnt += 1
	if first_a:
		ans = max(ans, cnt)
	print(ans)


def changement(L):
	r = len(L)
	for i in range(r - 1, 0, -1):
		if L[i] == 'P' and L[i - 1] == 'A':
			L[i] = 'A'
	return L

def saisie():
	while True:
		r = int(input(''))
		if 1 <= r <= 100:
			break
	return r
t = saisie()
i = 0
V = []
while i < t:
	n = saisie()
	test = False
	while test == False:
		test = True
		ch = input('')
		if len(ch) == n:
			for j in range(n):
				if ch[j] not in ['A', 'P']:
					test = False
					break
		else:
			test = False
		if test == True:
			V.append(ch)
	i += 1
j = 0
while j < t:
	p = 0
	L2 = []
	L = V[j]
	L2 += [L[k] for k in range(len(V[j]))]
	L1 = L2[:]
	while L1 != changement(L2):
		p += 1
		L2 = changement(L2)
		L1 = changement(L1)
	print(p)
	j += 1


def solve(students):
	moment = 0
	angry = patient = 0
	for student in students:
		if student == 'A':
			angry += 1
			moment = max(moment, patient)
			patient = 0
		elif student == 'P' and angry > 0:
			patient += 1
	moment = max(moment, patient)
	return moment
for T in range(int(input())):
	k = input()
	print(solve(input()))


t = int(input())
countA = 0
countP = 0
count = [0]
max = 0
while t:
	n = int(input())
	x = input()
	for i in x:
		if i == 'A':
			countA += 1
			count.append(0)
		else:
			if countA > 0:
				count[countA] += 1
			countP += 1
	countP = 0
	countA = 0
	max = 0
	for i in count:
		if max < i:
			max = i
	print(max)
	max = 0
	countP = 0
	countA = 0
	max = 0
	count = [0]
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	(count, a, b) = (0, 0, 0)
	for i in range(n):
		if s[i] == 'P':
			if a == 1:
				count += 1
				if count > b:
					b = count
		else:
			count = 0
			a = 1
	print(b)


t = int(input())
for i in range(t):
	k = int(input())
	s = input().lstrip('P').split('A')
	if s:
		print(max([len(s[i]) for i in range(len(s))]))
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	for i in range(n):
		if s[i] == 'A':
			break
	print(max(map(len, s[i + 1:].split('A'))))


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = 0
	l = []
	a = -1
	for i in range(n):
		if a == -1 and s[i] == 'A':
			a = i
		elif s[i] == 'P' and a != -1:
			p += 1
		elif s[i] == 'A':
			l.append(p)
			p = 0
		if i == n - 1:
			l.append(p)
	print(max(l))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	m1 = 0
	l = []
	if 'A' in s:
		o = s.find('A')
		q = s[o:]
		p = q.split('A')
		for i in p:
			d = i.count('P')
			l.append(d)
		m1 = max(l)
		print(m1)
	else:
		print(0)


import math
x = list(map(int, input().split()))
n = []
s = []
q = []
for i in range(0, x[0]):
	n.append(int(input()))
	s.append(input())
	q.append(0)
for i in range(0, x[0]):
	temp = 0
	flag = 0
	for j in range(0, len(s[i])):
		if s[i][j] == 'A':
			flag = 1
		if s[i][j] == 'P' and flag == 1:
			temp = temp + 1
			q[i] = max(q[i], temp)
		else:
			temp = 0
for i in range(0, x[0]):
	print(q[i])


def greaterthan(ar, target, end):
	start = 0
	ans = -1
	while start <= end:
		mid = (start + end) // 2
		if ar[mid] <= target:
			start = mid + 1
		else:
			ans = mid
			end = mid - 1
	return ans
for i in range(int(input())):
	n = int(input())
	s = input()
	s = s[::-1]
	x = 0
	ans = []
	for j in range(n):
		if s[j] == 'A':
			ans.append(j - x)
			x = j + 1
	if len(ans) == 0:
		print(0)
	else:
		print(max(ans))


T = int(input())
for i in range(T):
	k = int(input())
	s = input()
	A = []
	ind = s.find('A')
	if ind == -1:
		print(0)
	else:
		counter = ind
		for si in s[ind:]:
			if si == 'A':
				A.append(counter)
			counter += 1
		maxlen = len(s) - A[-1] - 1
		a0 = A[0]
		for ax in range(1, len(A)):
			a1 = A[ax]
			if a1 - a0 - 1 > maxlen:
				maxlen = a1 - a0 - 1
			a0 = a1
		print(maxlen)


T = int(input())
for test in range(T):
	n = int(input())
	s = input()
	ans = 0
	tans = 0
	if 'A' not in s:
		print(0)
		continue
	j = s.index('A')
	for i in range(j, n):
		if s[i] == 'P':
			tans += 1
		else:
			ans = max(tans, ans)
			tans = 0
	print(max(tans, ans))


def count(s, n):
	c = 0
	t = ''
	flag = 0
	while t != s:
		t = ''
		t += s
		j = ''
		flag = 0
		for i in range(len(s)):
			if flag == 0:
				j += s[i]
				if i == n - 1:
					break
			else:
				flag = 0
				continue
			if s[i] == 'A' and s[i + 1] == 'P':
				j += 'A'
				flag = 1
		s = j
		c += 1
	print(c - 1)
	return

def main():
	n = int(input())
	for i in range(n):
		j = int(input())
		s = input()
		count(s, j)
main()


T = int(input())
for cs in range(T):
	n = int(input())
	s = input()
	mx = 0
	prev = -1
	for i in range(n):
		if s[i] == 'A':
			prev = i
			break
	if prev != -1:
		for i in range(prev + 1, n):
			if s[i] == 'A':
				mx = max(mx, i - prev - 1)
				prev = i
		mx = max(mx, n - prev - 1)
	print(mx)


n = int(input())
for _ in range(n):
	j = input()
	a = input()
	(mx, cur, flag) = (0, 0, 0)
	for i in a:
		if i == 'A' and (not flag):
			flag = 1
		elif i == 'A' and flag:
			mx = max(mx, cur)
			cur = 0
		elif i != 'A' and flag:
			cur += 1
	mx = max(mx, cur)
	print(mx)


t = int(input())
for _ in range(t):
	l = int(input())
	s = input()
	arr = [x for x in s]
	if 'A' not in arr:
		print(0)
		continue
	ind = arr.index('A')
	ans = 0
	c = 0
	for x in range(ind, l):
		if arr[x] == 'A':
			ans = max(ans, c)
			c = 0
		else:
			c += 1
	print(max(ans, c))


n = int(input())
for i in range(n):
	c = 0
	q = input()
	s = list(input())
	while s[0] == 'P':
		s.pop(0)
		if len(s) == 0:
			break
	s += ['A']
	(c, d) = (0, 0)
	for j in s[1:]:
		if j == 'P':
			d += 1
		else:
			c = max(c, d)
			d = 0
	print(c)


T = int(input())
for z in range(T):
	n = int(input())
	line = str(input())
	v = [c for c in line]
	ans = 0
	cur = 0
	first = True
	for val in v:
		if val == 'P':
			cur += 1
		else:
			if first == True:
				first = False
			else:
				ans = max(ans, cur)
			cur = 0
	if first == False:
		ans = max(ans, cur)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	f = 0
	li = []
	flag = 0
	for j in range(n):
		if s[j] == 'A':
			flag = 1
			li.append(f)
			f = 0
		elif flag == 1:
			f += 1
	li.append(f)
	print(max(li))


test_case = int(input())
for _ in range(test_case):
	n = int(input())
	student = input().rstrip('A')
	student = student[student.find('A'):]
	minute = 0
	while not len(set(student)) == 1 and (not student == ''):
		student = student.replace('AP', 'AA')
		minute += 1
	print(minute)


n = int(input())
for i in range(n):
	a = int(input())
	b = input()
	cnt = 0
	l = []
	b += 'C'
	for j in range(len(b)):
		if b[j] == 'A':
			for k in range(j + 1, len(b)):
				if b[k] == 'P':
					cnt = cnt + 1
				else:
					l.append(cnt)
					cnt = 0
					break
		elif b[j] == 'C':
			l.append(cnt)
	l.sort()
	print(l[-1])


t = int(input())
for _ in range(t):
	count = 0
	n = int(input())
	s = list(input())
	prevs = []
	while len(set(s)) != 1:
		prevs = list(s)
		x = 0
		while x < n - 1:
			if s[x] == 'A' and s[x + 1] == 'P':
				s[x + 1] = 'A'
				x = x + 2
			else:
				x = x + 1
		if prevs == s:
			break
		count = count + 1
	print(count)


def solve():
	k = int(input())
	group = input() + 'A'
	prev = -1
	ans = 0
	for i in range(k + 1):
		if group[i] == 'A':
			if prev != -1:
				ans = max(i - prev - 1, ans)
			prev = i
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	l = int(input())
	a = list(input())
	maxi = 0
	for i in range(l - 1):
		if a[i] == 'A':
			j = i + 1
			coun = 0
			while j < l and a[j] == 'P':
				coun += 1
				maxi = max(maxi, coun)
				j += 1
		else:
			continue
	print(maxi)


t = int(input())
for i in range(t):
	n1 = int(input())
	l = str(input())
	c = 0
	m = 0
	j = 0
	n = 0
	for j in range(n1):
		if l[j] == 'A':
			n = 1
			if m < c:
				m = c
			c = 0
		elif n == 1:
			c += 1
	if m < c:
		m = c
	print(m)


for _ in range(int(input())):
	n = int(input())
	ar = list(input())
	ans = 0
	flag = 1
	while flag == 1:
		flag = 0
		ar1 = ar.copy()
		for i in range(1, n):
			if ar[i - 1] == 'A' and ar[i] == 'P':
				ar1[i] = 'A'
				flag = 1
		ans += 1
		ar = ar1.copy()
	print(ans - 1)


import sys
from bisect import bisect
input = sys.stdin.readline
q = int(input())
x = []
y = []
for _ in range(q):
	n = int(input())
	x = input()
	cmax = 0
	counter = -10 ** 10
	for a in x:
		if a == 'P':
			counter += 1
			cmax = max(cmax, counter)
		else:
			counter = 0
	print(cmax)


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = list(s)
	ctr = 0
	while True:
		t = list(l)
		for i in range(n - 1):
			if l[i] == 'A':
				if l[i + 1] == 'P':
					t[i + 1] = 'A'
		if t == l:
			break
		l = t
		ctr += 1
	print(ctr)


t = int(input())
for i in range(t):
	n = int(input())
	s = input().split('A')
	if len(s) == 1:
		s = 0
	else:
		l = []
		for j in s[1:]:
			l.append(len(j))
		s = max(l)
	print(s)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def prime_factors(n):
	i = 2
	factors = set()
	while i * i <= n:
		if n % i:
			i += 1
		else:
			factors.add(n // i)
			n = n // i
			factors.add(i)
	if n > 1:
		factors.add(n)
	return factors

def isPrime(n):
	if n == 2 or n == 3:
		return True
	if n % 2 == 0 or n < 2:
		return False
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True

def inar():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		st = input()
		pos = []
		ans = 0
		for i in range(n):
			if st[i] == 'A':
				pos.append(i)
		for i in range(1, len(pos)):
			ans = max(ans, pos[i] - pos[i - 1] - 1)
		if len(pos) == 0:
			print(0)
			continue
		print(max(ans, n - pos[-1] - 1))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solution(input_str):
	count = 0
	while True:
		transformed = input_str
		for i in range(len(input_str) - 1):
			if input_str[i] == 'A':
				transformed = transformed[:i + 1] + 'A' + transformed[i + 2:]
		if transformed == input_str:
			return count
		count += 1
		input_str = transformed
test_cases = int(input())
for _ in range(test_cases):
	_ = input()
	input_str = input()
	print(solution(input_str))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	p = 0
	count = 0
	temp = list()
	for i in range(n):
		if p == 0 and a[i] == 'A':
			p = 1
		elif p == 1:
			if a[i] == 'A':
				temp.append(count)
				count = 0
			else:
				count += 1
	if count != 0:
		temp.append(count)
	if len(temp) == 0:
		print(0)
	else:
		print(max(temp))


for _ in range(int(input())):
	n = input()
	s = input()
	cnt = -1
	max_ = 0
	for c in s:
		if c == 'A':
			if cnt > max_:
				max_ = cnt
			cnt = 0
		elif cnt >= 0:
			cnt += 1
	if cnt > max_:
		max_ = cnt
	print(max_)


import queue
import math
t = int(input())
while t:
	n = int(input())
	a = input()
	m = 0
	flag = False
	cnt = 0
	for i in range(n):
		if a[i] == 'P':
			cnt += 1
		else:
			if flag:
				m = max(m, cnt)
			else:
				flag = True
			cnt = 0
	if flag:
		m = max(m, cnt)
	print(m)
	t -= 1


for counter in range(int(input())):
	n = int(input()) + 1
	s = input() + 'A'
	ans = 0
	for i in range(n):
		if s[i] == 'A':
			preans = 0
			for j in range(i + 1, n):
				if s[j] == 'A':
					break
				preans += 1
			ans = max(ans, preans)
	print(ans)


t = int(input())
while t:
	n = int(input())
	s = input()
	index = []
	for i in range(n):
		if s[i] == 'A':
			index.append(i)
	if len(index) > 0:
		index.append(n)
	maxx = 0
	i = 0
	while i < len(index) - 1:
		maxx = max(index[i + 1] - index[i] - 1, maxx)
		i += 1
	print(maxx)
	t -= 1


t = int(input())
for i in range(t):
	k = int(input())
	row = input()
	plist = list(map(len, row.split('A')))
	if row == 'P' * len(row):
		print(0)
	elif row[0] == 'P':
		del plist[0]
		print(max(plist))
	else:
		print(max(plist))


t = int(input())
while t > 0:
	l = int(input())
	s = list(input())
	ans = 0
	while True:
		flag = 0
		i = 0
		while i < l - 1:
			if s[i] == 'A' and s[i + 1] == 'P':
				s[i + 1] = 'A'
				flag = 1
				i += 2
			else:
				i += 1
		ans += 1
		if flag == 0:
			break
	print(ans - 1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	arr = list(input())
	prev = n
	ans = 0
	for i in range(n - 1, -1, -1):
		if arr[i] == 'A':
			ans = max(ans, prev - i - 1)
			prev = i
	print(ans)


t = int(input())
for i in range(t):
	result = 0
	k = 0
	n = int(input())
	s = input()
	for i in range(len(s)):
		if s[i] == 'A':
			break
		else:
			k = k + 1
	s = s[k:]
	res = s.split('A')
	for i in res:
		result = max(result, len(i))
	print(result)


for _ in range(int(input())):
	k = int(input())
	arr = list(input())
	angry = []
	for i in range(0, k):
		if arr[i] == 'A':
			angry.append(i)
	ans = 0
	for j in range(0, len(angry) - 1):
		ans = max(ans, angry[j + 1] - angry[j] - 1)
	if arr[-1] == 'P' and len(angry) > 0:
		ans = max(ans, k - 1 - angry[-1])
	print(ans)


for _ in range(int(input())):
	r = 0
	p = 0
	b = False
	input()
	s = input()
	s += 'A'
	for c in s[s.find('A'):]:
		if c == 'A':
			r = max(r, p)
			p = 0
		else:
			p += 1
	print(r)


tt = int(input())
while tt > 0:
	n = int(input())
	ar = list(map(lambda x: 0 if x == 'A' else 1, input()))
	count = 0
	flag = 0
	maxi = 0
	for x in ar:
		if x == 0:
			flag = 1
			count = 0
		elif x and flag:
			count += 1
			if maxi < count:
				maxi = count
	print(maxi)
	tt = tt - 1


t = int(input())
while t != 0:
	t -= 1
	l = int(input())
	s = input()
	a = []
	for i in range(len(s)):
		if s[i] == 'A':
			a.append(i)
	if len(a) == 0:
		print(0)
		continue
	if len(a) == 1:
		print(len(s) - 1 - a[0])
		continue
	else:
		m = 0
		for i in range(len(a)):
			if i != len(a) - 1 and a[i + 1] - a[i] > m and (a[i + 1] - a[i] != 1):
				m = a[i + 1] - a[i] - 1
		if m > 0:
			m = max(m, len(s) - 1 - a[len(a) - 1])
			print(m)
		else:
			print(len(s) - 1 - a[len(a) - 1])


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	cnt = 0
	if s.count('A') == 0:
		print(cnt)
	else:
		start = -1
		end = -1
		for i in range(n):
			if s[i] == 'A':
				start = i
				break
		for i in range(n - 1, 0, -1):
			if s[i] == 'P':
				end = i
				break
		temp = 0
		for i in range(start, end + 1):
			if s[i] == 'P':
				temp += 1
			else:
				cnt = max(cnt, temp)
				temp = 0
		cnt = max(cnt, temp)
		print(cnt)


for _ in range(int(input())):
	n = int(input())
	l = list(input())
	a = 0
	count = -1
	for i in range(n):
		if l[i] == 'A' and count == -1:
			count += 1
		if l[i] == 'P' and count > -1:
			count += 1
		if l[i] == 'A' and count > 0:
			if a < count:
				a = count
			count = 0
	if a < count:
		a = count
	print(a)


t = int(input())
for _ in range(t):
	n = int(input())
	string = str(input())
	ans = 0
	i = string.find('A')
	if i == -1:
		print(0)
		continue
	for j in range(i + 1, n):
		if string[j] == 'A':
			maxi = j - i - 1
			if ans < maxi:
				ans = maxi
			i = j
	maxi = n - i - 1
	if ans < maxi:
		ans = maxi
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	c = input()
	ans = 0
	while c.count('AP') > 0:
		ans += 1
		c = c.replace('AP', 'AA')
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	(count, mx) = (0, 0)
	i = 0
	a += 'A'
	while a[i] != 'A':
		i += 1
	for l in range(i, n + 1):
		if a[l] == 'P':
			count += 1
		else:
			mx = max(count, mx)
			count = 0
	print(mx)


n = int(input())
for i in range(n):
	no_stud = input()
	lst = [x for x in input()]
	index = 0
	angry = False
	for i in range(len(lst)):
		if lst[i] == 'A':
			index = i
			angry = True
			break
	if angry == False:
		print(0)
		continue
	lst = lst[index:]
	count = []
	init = 0
	for e in lst:
		if e == 'A':
			count.append(init)
			init = 0
		else:
			init += 1
	count.append(init)
	print(max(count))


for _ in range(int(input())):
	input()
	str = input()
	count = 0
	while count < len(str) and str[count] == 'P':
		count += 1
	str = str[count:]
	count = 0
	maxVar = 0
	for letter in str:
		if letter == 'P':
			count += 1
		else:
			maxVar = max(maxVar, count)
			count = 0
	maxVar = max(maxVar, count)
	print(maxVar)


from fractions import gcd
from heapq import *
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
MOD = 10 ** 9 + 7

def main():
	a = int(input())
	for i in range(a):
		t = int(input())
		S = input()
		ans = 0
		f = True
		c = 0
		for i in range(t):
			if S[i] == 'A' and f:
				f = False
			elif S[i] == 'P' and (not f):
				c += 1
			elif S[i] == 'A' and (not f):
				ans = max(ans, c)
				c = 0
		ans = max(ans, c)
		print(ans)
main()


def main():
	n = int(input())
	for _ in range(0, n):
		num = int(input())
		num -= 1
		student = input()
		student = list(student)
		end = False
		counter = 0
		while not end:
			endd = True
			i = 0
			while i < num:
				if student[i] == 'A' and student[i + 1] == 'P':
					student[i + 1] = 'A'
					endd = False
					i += 1
				i += 1
			if endd:
				end = True
			counter += 1
		print(counter - 1)
main()


T = int(input())
answer = ''
for t in range(T):
	k = int(input())
	p = input()
	p2 = [''] * k
	for i in range(k):
		if p[i] == 'P':
			p2[i] = 'P'
		elif i == k - 1:
			p2[i] = 'A'
		elif p[i] != p[i + 1]:
			p2[i] = 'A'
	p3 = ''.join(p2)
	p4 = p3.split('A')
	p4[0] = []
	result = 0
	for r in p4:
		result = max(len(r), result)
	answer += str(result) + '\n'
print(answer)


t = int(input())
while t:
	n = int(input())
	l = list(input())
	l.append('A')
	i = 0
	a = False
	k = 0
	last = 0
	while i < n + 1:
		if l[i] == 'P':
			if a:
				k += 1
		if l[i] == 'A':
			a = True
			if k > last:
				last = k
			k = 0
		i += 1
	print(last)
	t -= 1


groups = int(input())
for _ in range(groups):
	n = int(input())
	s = input()
	li = [0]
	m = 0
	for i in range(1, len(s)):
		if s[i] == 'P' and s[i - 1] == 'A':
			m += 1
			for j in range(i, len(s) - 1):
				if s[j + 1] == 'P':
					m += 1
				elif s[j + 1] == 'A':
					break
			li.append(m)
			m = 0
	print(max(li))


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	k = int(input())
	s = input().strip() + 'A'
	i = s.index('A') + 1
	c = 0
	ans = 0
	while i < k + 1:
		if s[i] == 'A':
			ans = max(ans, c)
			c = 0
		else:
			c += 1
		i += 1
	print(ans)


l = int(input())
for _ in range(l):
	_ = input()
	s = input()
	ans = 0
	cnt = -10 ** 19
	for i in range(len(s)):
		if s[i] == 'A':
			ans = max(ans, cnt)
			cnt = 0
		else:
			cnt += 1
	ans = max(ans, cnt)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	a = list(a)
	k = 0
	m = 0
	l = 0
	for i in range(len(a)):
		if a[i] == 'A':
			break
		else:
			l = l + 1
	for i in range(l, len(a)):
		if a[i] == 'A':
			m = 0
		else:
			m = m + 1
		k = max(m, k)
	print(k)


n = int(input())
for i in range(n):
	a = int(input())
	s = list(input().split('A'))
	N = 0
	for j in range(1, len(s)):
		if len(s[j]) > N:
			N = len(s[j])
	print(N)


import sys
import heapq
import math
from collections import deque
from collections import defaultdict
from collections import Counter
modH = int(10 ** 9) + 7
I = lambda : list(map(int, input().split(' ')))

def PRINT(lst, sep=' '):
	print(sep.join(map(str, lst)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	mm = 0
	for i in range(len(s)):
		if s[i] == 'A':
			cnt = 0
			for j in range(i + 1, len(s)):
				if s[j] == 'P':
					cnt += 1
					mm = max(cnt, mm)
				else:
					break
	print(mm)


def angry_students(s):
	a_found = False
	ap_count = 0
	max = 0
	i = 0
	n = len(s)
	while i < n:
		if s[i] == 'A' and a_found == False:
			a_found = True
			i += 1
		elif s[i] == 'P' and a_found:
			i += 1
			ap_count += 1
		elif s[i] == 'A' and a_found:
			if max < ap_count:
				max = ap_count
			i += 1
			ap_count = 0
		else:
			i += 1
	if max < ap_count:
		max = ap_count
	return max
t = int(input())
while t > 0:
	k = int(input())
	s = input()
	print(angry_students(s))
	t -= 1


I = input
for _ in [0] * int(I()):
	I()
	print(max(0, 0, *map(len, I().split('A')[1:])))


t = int(input())
for i in range(t):
	k = int(input())
	s = input()
	flag = False
	tempp = 0
	maxp = 0
	for j in range(k):
		if s[j] == 'A':
			flag = True
			if tempp > maxp:
				maxp = tempp
			tempp = 0
		elif flag:
			tempp += 1
			if j == k - 1 and tempp > maxp:
				maxp = tempp
	print(maxp)


n = int(input())
for i in range(n):
	m = int(input())
	s = input()
	res = 0
	now = -1
	for i in range(m):
		if s[i] == 'A':
			res = max(res, now)
			now = 0
		if s[i] == 'P' and now != -1:
			now += 1
	res = max(res, now)
	print(res)


Q = int(input())
for _ in range(Q):
	n = int(input())
	s = input()
	while s != '' and s[0] == 'P':
		s = s[1:]
	ans = 0
	now = 0
	for i in s:
		if i == 'P':
			now += 1
		else:
			ans = max(ans, now)
			now = 0
	ans = max(ans, now)
	print(ans)


t = int(input())
for i in range(0, t):
	k = input()
	s = input()
	max = 0
	lst = s.split('A')
	for j in range(1, len(lst)):
		sub = lst[j]
		if len(sub) > max:
			if sub[0] == 'P':
				max = len(sub)
	print(max)


for _ in range(int(input())):
	n = int(input())
	l = str(input())
	k = []
	for i in range(len(l)):
		if l[i] == 'A':
			k.append(i)
	if len(k) == 0:
		print(0)
	elif len(k) != 0:
		k.append(n)
		p = []
		for i in range(len(k) - 1):
			p.append(int(k[i + 1]) - int(k[i]))
		if n != 1:
			print(max(p) - 1)
		if n == 1:
			print(0)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	for _ in range(t):
		k = int(input())
		s = input()
		print(max(map(len, s.split('A')[1:]), default=0))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def get_minutes(group):
	count = 0
	while True:
		threw = False
		i = 0
		while i < len(group) - 1:
			if group[i] == 'A' and group[i + 1] == 'P':
				threw = True
				group[i + 1] = 'A'
				i += 2
			else:
				i += 1
		if threw:
			count += 1
			threw = False
		else:
			break
	return count
t = int(input())
for i in range(t):
	g = int(input())
	group = list(input())
	print(get_minutes(group))


for tc in range(int(input())):
	n = int(input())
	s = input()
	a = 0
	while 'AP' in s:
		s = s.replace('AP', 'A')
		a += 1
	print(a)


kl = int(input())
for l in range(kl):
	n = int(input())
	pr = 0
	tt = 0
	tm = 0
	p = str(input())
	s = p + 'A'
	k = s.index('A')
	for i in range(k, n):
		if p[i] == 'A':
			tm = max(tm, tt)
			tt = 0
			pr = 1
		else:
			tt += 1
	tm = max(tm, tt)
	print(tm)


t = int(input())
while t:
	n = int(input())
	s = input()
	l = s.split('A')
	maxLen = 0
	for i in range(1, len(l)):
		maxLen = max(maxLen, len(l[i]))
	print(maxLen)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	i = 0
	m = 0
	while i < n:
		c = 0
		z = 0
		if i < n and s[i] == 'A':
			i += 1
			c = 0
			while i < n and s[i] == 'P':
				c += 1
				i += 1
			if c > m:
				m = c
			z = 1
		if z == 0:
			i += 1
	print(m)


t = int(input())

def cal(s):
	leng = len(s)
	now = int(0)
	flag = False
	while now < leng:
		if s[now] == 'A':
			if now == leng - 1:
				return flag
			elif s[now + 1] == 'P':
				flag = True
				s[now + 1] = 'A'
				now += 2
			else:
				now += 1
		else:
			now += 1
	return flag
for i in range(t):
	k = int(input())
	s = list(input())
	ans = int(0)
	while cal(s):
		ans += 1
	print(ans)


for xoxox in range(1):
	from sys import stdin
	input = stdin.readline
	for _ in range(int(input())):
		n = int(input())
		s = input()
		ans = 0
		f = 0
		a = [0]
		for i in range(n):
			if s[i] == 'A' and f == 0:
				f = 1
				continue
			if s[i] == 'A' and f == 1:
				a.append(ans)
				ans = 0
			if s[i] == 'P' and f == 1:
				ans += 1
				if i == n - 1:
					a.append(ans)
		print(max(a))


tc = int(input())
for t in range(tc):
	n = int(input())
	s = input()
	ans = 0
	i = 0
	while i < n:
		if s[i] == 'A':
			i = i + 1
			tmp = 0
			while i < n and s[i] == 'P':
				tmp += 1
				ans = max(ans, tmp)
				i = i + 1
			i = i - 1
		i = i + 1
	print(ans)


a = int(input())
for i in range(a):
	z = int(input())
	s = input()
	(b, c, z) = (0, 0, 0)
	for j in s:
		if j == 'A':
			c = 0
			b = 1
		elif b == 1:
			c += 1
		z = max(z, c)
	print(z)


for i in range(int(input())):
	a = int(input())
	s = input()
	x = 0
	flag = 0
	temp = 0
	for j in range(a):
		if s[j] == 'A':
			x = max(x, temp)
			temp = 0
			flag = 1
		elif flag and s[j] == 'P':
			temp += 1
	x = max(x, temp)
	print(x)


import math
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ind = -1
	lis = []
	for i in range(n):
		if s[i] == 'A':
			lis.append(i)
	ans = 0
	for i in range(len(lis) - 1):
		ans = max(ans, lis[i + 1] - lis[i] - 1)
	if len(lis) > 0:
		ans = max(ans, n - 1 - lis[-1])
	print(ans)


t = int(input())
for k in range(t):
	numero = int(input())
	estudantes = input()
	maximo = 0
	indices = []
	if 'A' not in estudantes or 'P' not in estudantes:
		print(0)
	else:
		numero += 1
		estudantes += 'A'
		for i in range(numero):
			if estudantes[i] == 'A':
				indices.append(i)
		for j in range(1, len(indices)):
			if indices[j] - indices[j - 1] > maximo:
				maximo = indices[j] - indices[j - 1]
		if maximo == 0 and indices[0] == numero - 1:
			print(maximo)
		elif maximo == 0 and indices[0] != numero - 1:
			print(1)
		else:
			print(maximo - 1)


for _ in range(int(input())):
	n = int(input())
	s = input()
	(f, c, m) = (0, 0, 0)
	for i in range(1, n):
		if f == 1 and s[i] == 'P':
			c += 1
		elif f == 0 and s[i] == 'P' and (s[i - 1] == 'A'):
			c = 1
			f = 1
		elif s[i] == 'A':
			f = 0
			if c > m:
				m = c
			c = 0
	if c > m:
		m = c
	print(m)


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	s = inp()
	ans = 0
	while True:
		s1 = list(s)
		for i in range(n - 1):
			if s[i] == 'A':
				s1[i + 1] = 'A'
		strS1 = ''.join(s1)
		if strS1 == s:
			print(ans)
			break
		else:
			s = strS1
		ans += 1


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	k = 0
	m = 0
	l = list(s)
	for i in range(n):
		if l[i] == 'A':
			p = i
			break
	else:
		p = n
	for i in range(p, n):
		if l[i] == 'P':
			k = k + 1
		else:
			k = 0
		m = max(k, m)
	print(m)


a = int(input())
for i in range(a):
	x = int(input())
	s = input()
	r = s + 'A'
	ans = []
	dif = []
	for i in range(len(r)):
		if r[i] == 'A':
			ans.append(i)
	for i in range(len(ans) - 1):
		dif.append(ans[i + 1] - ans[i])
	if len(dif) == 0:
		print(0)
	else:
		print(max(dif) - 1)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	s = input()
	m = float('-inf')
	f = 0
	for i in s:
		if i == 'A':
			f = 1
			c = 0
		elif f == 1:
			c += 1
			if c > m:
				m = c
	if m == float('-inf'):
		print(0)
	else:
		print(m)


t = int(input())
res = 0
for j in range(t):
	n = int(input())
	pl = input()[:n]
	curres = 0
	i = 0
	while i < n:
		while i < n and pl[i] != 'A':
			i += 1
		if i < n and pl[i] == 'A':
			i += 1
		cursubres = 0
		while i < n and pl[i] != 'A':
			i += 1
			cursubres += 1
		curres = max(curres, cursubres)
	print(curres)
	res = max(curres, res)


result = []
for m in range(int(input())):
	n_students = int(input())
	students = input()
	angry = 0
	ags = 0
	wil_start = 0
	tem_ags = 0
	for n in range(n_students):
		if students[n] == 'A':
			wil_start = 1
			if tem_ags > ags:
				ags = tem_ags
			tem_ags = 0
		if students[n] == 'P' and wil_start == 1:
			tem_ags += 1
	if tem_ags > ags:
		ags = tem_ags
	result.append(ags)
for m in result:
	print(m)


t = int(input())
for i in range(int(t)):
	n = int(input())
	r = input()
	cc = 0
	for j in range(int(n)):
		if r[j] == 'A':
			hh = 0
			break
	(cp, ca) = (0, 0)
	ans = 0
	qw = 0
	for k in range(j + 1, n):
		if r[k] == 'P':
			cc += 1
		if cc >= qw:
			qw = cc
		if r[k] == 'A':
			cc = 0
			continue
		if cc == 1:
			ans += 1
	print(qw)


for _ in range(int(input())):
	n = int(input())
	s = input()
	i = 0
	while i < len(s) and s[i] != 'A':
		i += 1
	mx = 0
	while i < len(s):
		cmax = 0
		i += 1
		while i < len(s) and s[i] != 'A':
			i += 1
			cmax += 1
		mx = max(mx, cmax)
	print(mx)


t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	lists = [c for c in s]
	cnt = 0
	flag = 0
	while 1:
		flag = 0
		ind_lists = []
		for i in range(len(s) - 1):
			if lists[i] == 'A':
				if lists[i + 1] == 'P':
					ind_lists.append(i + 1)
					flag = 1
		for j in ind_lists:
			lists[j] = 'A'
		if flag == 0:
			break
		cnt += 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	l = list(input())
	ans = 0
	for t in range(n):
		c = 0
		flag = 0
		while c < n - 1:
			if l[c] == 'A' and l[c + 1] == 'P':
				l[c + 1] = 'A'
				c += 1
				flag = 1
			c += 1
		if not flag:
			break
		ans += 1
	print(ans)