N = int(eval(input()))

S = input().strip()

cmax = 0

cnt = 0

for i in range(N):

    if S[i]=="I":

        cnt += 1

    else:

        cnt -= 1

    cmax = max(cmax,cnt)

print(cmax)
N = int(eval(input()))

S = input().strip()

x = 0

xmax = 0

for i in range(N):

    if S[i]=="I":

        x += 1

        xmax = max(xmax,x)

    else:

        x -= 1

print(xmax)
N = int(eval(input()))
S = input().strip()
cmax = 0
cnt = 0
for i in range(N):
    if S[i] == "I":
        cnt += 1
    else:
        cnt -= 1
    cmax = max(cmax, cnt)
print(cmax)

N = int(eval(input()))
S = input().strip()
x = 0
xmax = 0
for i in range(N):
    if S[i] == "I":
        x += 1
        xmax = max(xmax, x)
    else:
        x -= 1
print(xmax)

-cmax = 0 -cnt = 0 +x = 0 +xmax = 0 -        cnt += 1 +        x += 1 +        xmax = max(xmax, x) -        cnt -= 1 -    cmax = max(cmax, cnt) -print(cmax) +        x -= 1 +print(xmax)