Chef's friend Alex runs a movie theatre. Due to the increasing number of platforms for watching movies online, his business is not running well. As a friend, Alex asked Chef to help him maximise his profits. Since Chef is a busy person, he needs your help to support his friend Alex.  
Alex's theatre has four showtimes: 12 PM, 3 PM, 6 PM and 9 PM. He has four movies which he would like to play ― let's call them A, B, C and D. Each of these movies must be played exactly once and all four must be played at different showtimes. For each showtime, the price of a ticket must be one of the following: Rs 25, Rs 50, Rs 75 or Rs 100. The prices of tickets for different showtimes must also be different.
Through his app, Alex receives various requests from his customers. Each request has the form "I want to watch this movie at this showtime". Let's assume that the number of people who come to watch a movie at a given showtime is the same as the number of requests for that movie at that showtime.
It is not necessary to accommodate everyone's requests ― Alex just wants to earn the maximum amount of money. There is no restriction on the capacity of the theatre. However, for each movie that is not watched by anyone, Alex would suffer a loss of Rs 100 (deducted from the profit).
You are given $N$ requests Alex received during one day. Find the maximum amount of money he can earn on that day by choosing when to play which movies and with which prices. 

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- $N$ lines follow. Each of these lines contains a character $m$, followed by a space and an integer $t$, describing a request to see the movie $m$ at the showtime $t$.

-----Output-----
For each test case, print a single line containing one integer ― the maximum profit Alex can earn (possibly negative).
Finally, print a line containing one integer ― the total profit over all test cases, i.e. over $T$ days.

-----Constraints-----
- $1 \le T \le 150$
- $0 \le N \le 100$
- $m$ is 'A', 'B', 'C' or 'D'
- $t$ is $12$, $3$, $6$ or $9$

-----Subtasks-----
Subtask #1 (30 points): it is possible to fulfill all requests
Subtask #2 (70 points): original constraints

-----Example Input-----
5
12
A 3
B 12
C 6
A 9
B 12
C 12
D 3
B 9
D 3
B 12
B 9
C 6
7
A 9
A 9
B 6
C 3
D 12
A 9
B 6
2
A 9
B 6
1
D 12
0 

-----Example Output-----
575
525
-25 
-200 
-400
475

-----Explanation-----
Example case 1: The following table shows the number of people that want to watch the movies at the given showtimes:  12  3  6  9   A   0  1  0 1  B   3  0  0  2   C   1  0  2  0   D   0  2  0  0 
The maximum number of requests was sent for movie B at 12 PM. Therefore, we play this movie at this time and the tickets cost Rs 100. Next, we play movie D at 3 PM with ticket price Rs 75 and movie C at 6 PM with ticket price Rs 50. Finally, we have a slot for 9 PM and the only movie we can play at that time now is movie A, with ticket price Rs 25. The total profit is $3 \cdot 100 + 2 \cdot 75 + 2 \cdot 50 + 1 \cdot 25 = 300 + 150 + 100 + 25 = 575$. Since each movie was watched by at least one person, there is no additional loss.
Example case 2: Just like above, we show the requests in a table:  12  3  6  9   A   0  0  0 3  B   0  0  2  0   C   0  1  0  0   D   1  0  0  0 
The optimal solution is to play movie A at 9 PM, movie B at 6 PM, movie C at 3 PM and movie D at 12 PM, with decreasing ticket prices in this order. The profit is $3 \cdot 100 + 2 \cdot 75 + 1 \cdot 50 + 1 \cdot 25 = 300+150+50+25 = 525$.
Example case 3: Again, we show the requests in a table:  12  3  6  9   A   0  0  0 1  B   0  0  1  0   C   0  0  0  0   D   0  0  0  0 
The optimal solution is to play movie A at 9 PM with ticket price Rs 100, movie B at 6 PM with ticket price Rs 75 and the remaining two movies in any order at 12 PM and 3 PM ― either way, there will be nobody watching them. We earn $1 \cdot 100 + 1 \cdot 75 = 175$, but we have to deduct Rs 200, so the resulting profit is $175 - 200 = -25$.
Example case 4: The optimal solution is to play movie D at 12 PM; the other three movies go unattended. We have to deduct Rs 300, so the profit is $1 \cdot 100 - 300 = -200$.
Example case 5: Since there are no requests for any movie at any time, all movies go unattended and Alex just suffers a loss of Rs 400.
The total profit for all 5 days is $575+525-25-200-400 = 475$.

Difficulty: interview

Starter code:

Solutions:
from itertools import permutations
C = list(permutations(['A','B','C','D']))
V = list(permutations([3,6,9,12]))
P = list(permutations([25,50,75,100]))
R = []

def test():
 d = {}
 n = int(input())
 for i in C[0]:
  for j in V[0]:
   d[i+str(j)] = 0
 for i in range(n):
  x,y = input().split()
  d[x+y] += 1
 ans = -1000000000
 for i in C:
  for j in V:
   for k in P:
    c = 0
    for l in range(4): 
     if d[i[l]+str(j[l])] == 0:
      c -= 100
     else:
      c += (d[i[l]+str(j[l])]*k[l])
    ans = max(ans,c)
 R.append(ans)
 print(ans)

def __starting_point():
 t = int(input())
 for i in range(t):
  test()
 print(sum(R))

__starting_point()

# cook your dish here
f=[]
for ad in range(int(input())):
 n=int(input())
 a=[0]*4;b=[0]*4;c=[0]*4;d=[0]*4
 for i in range(n):
  x=(input().split())
  if x[0]=="A":
   a[int(x[1])//3-1]+=1
  elif x[0]=="B":
   b[int(x[1])//3-1]+=1
  elif x[0]=="C":
   c[int(x[1])//3-1]+=1
  elif x[0]=="D":
   d[int(x[1])//3-1]+=1
 x=[]
 for i in range(4):
  for j in range(4):
   for k in range(4):
    for m in range(4):
     s=0
     l=[i,j,k,m]
     if len(set(l))==4:
      y=[a[i],b[j],c[k],d[m]]
      for t in y:
       if t==0:
        s-=100
      y.sort()
      for t in range(3,-1,-1):
       s+=(25+25*t)*y[t]
      x.append(s)
 
 m=max(x)
 f.append(m)
 print(m)
print(sum(f))


from pprint import pprint
from numpy import where
arr =[]
from sys import stdin,stdout
prices =[100,75,50,25]
for _ in range(int(stdin.readline())):
 profit = -400
 t = int(stdin.readline())
 a = []
 for i in range(4):
  temp = []
  for j in range(4):
   temp.append(0)
  a.append(temp)
 s = 0
 for __ in range(t):
  mov , time = input().split()
  time = int(time)
  if mov == 'A':
   if time == 12:
    a[0][0]+=1
   elif time ==3:
    a[0][1]+=1
   elif time == 6:
    a[0][2]+=1
   else:
    a[0][3]+=1
  elif mov =='B':
   if time == 12:
    a[1][0]+=1
   elif time == 3:
    a[1][1]+=1
   elif time == 6:
    a[1][2]+=1
   else:
    a[1][3]+=1
  elif mov =='C':
   if time == 12:
    a[2][0]+=1
   elif time == 3:
    a[2][1]+=1
   elif time == 6:
    a[2][2]+=1
   else:
    a[2][3]+=1
  else:
   if time == 12:
    a[3][0]+=1
   elif time == 3:
    a[3][1]+=1
   elif time == 6:
    a[3][2]+=1
   else:
    a[3][3]+=1
 ma = -1
 cakes =[]
 for k in range(4):
  mai,maj=-1,-1
  for i in range(4):
   for j in range(4):
    if a[i][j] > ma:
     ma = a[i][j]
     mai = i
     maj = j
  for z in range(4):
   for x in range(4):
    if z==mai or x == maj:
     a[z][x]=0
  cakes.append(ma)
  ma = -1
 cakes = sorted(cakes,reverse=True)
 for i in range(4):
  if cakes[i]==0:
   s-=100
  else:
   s+=prices[i]*cakes[i]
 if s >= profit:
  profit= s
 print(profit)
 arr.append(profit)
 #print(arr)
print(sum(arr))

from pprint import pprint
from numpy import where
arr =[]
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
 t = int(stdin.readline())
 a = []
 for i in range(4):
  temp = []
  for j in range(4):
   temp.append(0)
  a.append(temp)
 s = 0
 for __ in range(t):
  mov , time = input().split()
  time = int(time)
  if mov == 'A':
   if time == 12:
    a[0][0]+=1
   elif time ==3:
    a[0][1]+=1
   elif time == 6:
    a[0][2]+=1
   else:
    a[0][3]+=1
  elif mov =='B':
   if time == 12:
    a[1][0]+=1
   elif time == 3:
    a[1][1]+=1
   elif time == 6:
    a[1][2]+=1
   else:
    a[1][3]+=1
  elif mov =='C':
   if time == 12:
    a[2][0]+=1
   elif time == 3:
    a[2][1]+=1
   elif time == 6:
    a[2][2]+=1
   else:
    a[2][3]+=1
  else:
   if time == 12:
    a[3][0]+=1
   elif time == 3:
    a[3][1]+=1
   elif time == 6:
    a[3][2]+=1
   else:
    a[3][3]+=1
 ma = -1
 cakes =[]
 for k in range(4):
  mai,maj=-1,-1
  for i in range(4):
   for j in range(4):
    if a[i][j] > ma:
     ma = a[i][j]
     mai = i
     maj = j
  for z in range(4):
   for x in range(4):
    if z==mai or x == maj:
     a[z][x]=0
  cakes.append(ma)
  ma = -1
 cakes = sorted(cakes,reverse=True)
 for i in range(4):
  if cakes[i]==0:
   s-=100
  else:
   if i==0:
    s+= cakes[i]*100
   elif i==1:
    s+= cakes[i]*75
   elif i==2:
    s+= cakes[i]*50
   elif i==3:
    s+= cakes[i]*25
 print(s)
 arr.append(s)
 #print(arr)
print(sum(arr))

from pprint import pprint
from numpy import where
arr =[]
from sys import stdin,stdout
for _ in range(int(stdin.readline())):
 t = int(stdin.readline())
 a = []
 for i in range(4):
  temp = []
  for j in range(4):
   temp.append(0)
  a.append(temp)
 s = 0
 for __ in range(t):
  mov , time = input().split()
  time = int(time)
  if mov == 'A':
   if time == 12:
    a[0][0]+=1
   elif time ==3:
    a[0][1]+=1
   elif time == 6:
    a[0][2]+=1
   else:
    a[0][3]+=1
  elif mov =='B':
   if time == 12:
    a[1][0]+=1
   elif time == 3:
    a[1][1]+=1
   elif time == 6:
    a[1][2]+=1
   else:
    a[1][3]+=1
  elif mov =='C':
   if time == 12:
    a[2][0]+=1
   elif time == 3:
    a[2][1]+=1
   elif time == 6:
    a[2][2]+=1
   else:
    a[2][3]+=1
  else:
   if time == 12:
    a[3][0]+=1
   elif time == 3:
    a[3][1]+=1
   elif time == 6:
    a[3][2]+=1
   else:
    a[3][3]+=1
 ma = -1
 cakes =[]
 for k in range(4):
  mai,maj=-1,-1
  for i in range(4):
   for j in range(4):
    if a[i][j] > ma:
     ma = a[i][j]
     mai = i
     maj = j
  for z in range(4):
   for x in range(4):
    if z==mai or x == maj:
     a[z][x]=0
  cakes.append(ma)
  ma = -1
 for i in range(len(cakes)):
  if cakes[i]==0:
   s-=100
  else:
   if i==0:
    s+= cakes[i]*100
   elif i==1:
    s+= cakes[i]*75
   elif i==2:
    s+= cakes[i]*50
   elif i==3:
    s+= cakes[i]*25
 print(s)
 arr.append(s)
 #print(arr)
print(sum(arr))

ans=0
for _ in range(int(input())):
 n=int(input())
 A,B,C,D=[0]*4,[0]*4,[0]*4,[0]*4
 for i in range(n):
  m,t=map(str,input().split())
  if m=='A':
   if t=='12':
    A[0]+=1
   elif t=='3':
    A[1]+=1
   elif t=='6':
    A[2]+=1
   else:
    A[3]+=1
  elif m=='B':
   if t=='12':
    B[0]+=1
   elif t=='3':
    B[1]+=1
   elif t=='6':
    B[2]+=1
   else:
    B[3]+=1
  elif m=='C':
   if t=='12':
    C[0]+=1
   elif t=='3':
    C[1]+=1
   elif t=='6':
    C[2]+=1
   else:
    C[3]+=1
  else:
   if t=='12':
    D[0]+=1
   elif t=='3':
    D[1]+=1
   elif t=='6':
    D[2]+=1
   else:
    D[3]+=1
 ans1=-400
 for i in range(4):
  for j in range(4):
   if j==i:
    continue
   for k in range(4):
    if k==i or k==j:
     continue
    for l in range(4):
     if l==i or l==j or l==k:
      continue
     din=[A[i],B[j],C[k],D[l]]
     din.sort(reverse=True)
     count=(100*din[0])+(75*din[1])+(50*din[2])+(25*din[3])
     count-=(100*(din.count(0)))
     if count>ans1:
      ans1=count
 print(ans1)
 ans+=ans1
print(ans)

import numpy
from itertools import permutations 
totals=0
test=int(input())
while(test!=0):
 n=int(input())
 tw=[0,0,0,0]
 th=[0,0,0,0]
 si=[0,0,0,0]
 ni=[0,0,0,0]
 s=[0,1,2,3]
 for i in range(n):
  m,t = map(str,input().split())
  t=int(t)
  if m=="A":
   ind=0
  elif m=="B":
   ind=1
  elif m=="C":
   ind=2
  elif m=="D":
   ind=3
  if t==12:
   tw[ind]+=1
  elif t==3:
   th[ind]+=1
  elif t==6:
   si[ind]+=1
  else:
   ni[ind]+=1
 co = list(permutations(s,4))
 total=float("-inf")
 for value in co:
  p=[tw[value[0]],th[value[1]],si[value[2]],ni[value[3]]]
  p.sort(reverse=True)
  tot=0
  for i in p:
   if i==0:
     tot-=100
  tot+=p[0]*100
  tot+=p[1]*75
  tot+=p[2]*50
  tot+=p[3]*25
  if tot>total:
   total=tot
 print(total)
 totals+=total
 test-=1
print(totals)

# cook your dish here
from sys import stdin,stdout
I1=lambda:int(stdin.readline())
T=0
for _ in range(I1()):
 m=I1()
 li2=[12,3,6,9]
 d={'A':[0,0,0,0],'B':[0,0,0,0],'C':[0,0,0,0],'D':[0,0,0,0]}
 for i in range(m):
  x,t=list(map(str,stdin.readline().split()))
  t=int(t)
  d[x][li2.index(t)]+=1 
 #print(d)
 n=4
 ans=[]
 cost=[100,75,50,25]
 for i in range(n):
  li1=[]
  li1.append(d['A'][i])
  for j in range(n):
   if j!=i:
    li1.append(d['B'][j])
    for k in range(n):
     if k!=i and k!=j:
      li1.append(d['C'][k])
      for l in range(n):
       if l!=i and l!=j and l!=k:
        li1.append(d['D'][l])
        c=0 
        z=0 
        for s in sorted(li1,reverse=True):
         if s==0:
          c-=100
         else:
          c+=s*cost[z]
         z+=1 
        if c!=0:
         ans.append(c)
        li1.pop()
      li1.pop()
    li1.pop()
  li1.pop()
 #print(ans)
 a=max(ans)
 print(a)
 T+=a 
print(T)
 


#The Theatre Problem
def solve(movie,ind,used):
 if ind >= 4:
  arr = []
  for i in used:
   arr.append(used[i])
  arr.sort(reverse=True)
  profit = 0
  price = 100
  for i in arr:
   if i == 0:
    profit -= 100
    continue
   profit += price*i
   price -= 25
  nonlocal ans
  ans = max(ans,profit)
  return
 
 for i in movie[ind]:
  if i in used:
   continue
  used[i] = movie[ind][i]
  solve(movie,ind+1,used)
  del used[i]

Test = int(input())
total = 0
for __ in range(Test):
 n = int(input())
 a = {3:0, 6:0, 9:0, 12:0}
 b = {3:0, 6:0, 9:0, 12:0}
 c = {3:0, 6:0, 9:0, 12:0}
 d = {3:0, 6:0, 9:0, 12:0}
 for i in range(n):
  arr = input().split()
  val = int(arr[1])

  if arr[0] == 'A':
   a[val] += 1

  elif arr[0] == 'B':
   b[val] += 1

  elif arr[0] == 'C':
   c[val] += 1

  else:
   d[val] += 1

  kdf = 'asdfasd'
  gasdf3 = 90877444

 ans = -2**31
 solve([a,b,c,d],0,{})
 print(ans)
 total += ans
print(total)

def maximizing(array):
 cpy = array[:]
 final_list = []
 for i in range(len(array)):
  new_list = [array[i]]
  for t in range(len(cpy)):
   for j in range(len(new_list)):
    if cpy[t][0] == new_list[j][0] or cpy[t][1] == new_list[j][1]:
     break
   else:
    new_list.append(cpy[t])
  
  cpy.remove(array[i])
  final_list.append(new_list)

 costing = []
 for i in final_list:
  cost = 0
  count_array = []
  if len(i) < 4:
   cost -= (4 - len(i))*100
  for j in i:
   count_array.append(arrays.count(j))

  count_array.sort(reverse=True)

  threshold = 100

  for k in count_array:
   cost += k*threshold
   threshold -= 25

  costing.append(cost)

 return max(costing)


test_cases = int(input())
output_list = []

for _ in range(test_cases):
 n = int(input())
 arrays = []
 if n != 0:
  for _ in range(n):
   arrays.append(list(input().split()))

  output_list.append(maximizing(arrays))
 else:
  output_list.append(-400)

for output in output_list:
 print(output)

print(sum(output_list))


def maximizing(array):
 cpy = array[:]
 final_list = []
 for i in range(len(array)):
  new_list = [array[i]]
  for t in range(len(cpy)):
   for j in range(len(new_list)):
    if cpy[t][0] == new_list[j][0] or cpy[t][1] == new_list[j][1]:
     break
   else:
    new_list.append(cpy[t])
  
  cpy.remove(array[i])
  final_list.append(new_list)

 costing = []
 for i in final_list:
  cost = 0
  count_array = []
  if len(i) < 4:
   cost -= (4 - len(i))*100
  for j in i:
   count_array.append(arrays.count(j))

  count_array.sort(reverse=True)

  threshold = 100

  for k in count_array:
   cost += k*threshold
   threshold -= 25

  costing.append(cost)

 return max(costing)


test_cases = int(input())
output_list = []

for _ in range(test_cases):
 n = int(input())
 arrays = []
 if n != 0:
  for _ in range(n):
   arrays.append(list(input().split()))

  output_list.append(maximizing(arrays))
 else:
  output_list.append(-400)

for output in output_list:
 print(output)

print(sum(output_list))


def maximizing(array):
 cpy = array[:]
 final_list = []
 for i in range(len(array)):
  new_list = [array[i]]
  for t in range(len(cpy)):
   for j in range(len(new_list)):
    if cpy[t][0] == new_list[j][0] or cpy[t][1] == new_list[j][1]:
     break
   else:
    new_list.append(cpy[t])
  for tt in new_list:
   if tt in cpy:
    cpy.remove(tt)
  final_list.append(new_list)

 costing = []
 for i in final_list:
  cost = 0
  count_array = []
  if len(i) < 4:
   cost -= (4 - len(i))*100
  for j in i:
   count_array.append(arrays.count(j))

  count_array.sort(reverse=True)

  threshold = 100

  for k in count_array:
   cost += k*threshold
   threshold -= 25

  costing.append(cost)

 return max(costing)


test_cases = int(input())
output_list = []

for _ in range(test_cases):
 n = int(input())
 arrays = []
 if n != 0:
  for _ in range(n):
   arrays.append(list(input().split()))

  output_list.append(maximizing(arrays))
 else:
  output_list.append(-400)

for output in output_list:
 print(output)

print(sum(output_list))


import sys
import itertools
 
def read_line():
 return sys.stdin.readline().split()
 
s = 0
T = int(read_line()[0])
for i in range(T):
 n = int(read_line()[0])
 
 cnt = {}
 for j in range(n):
  a, b = read_line()
  if cnt.get((a, b)) == None: 
   cnt[(a, b)] = 1
  else:
   cnt[(a, b)] += 1
 
 best = -(10 ** 18)
 for p in list(itertools.permutations(['A', 'B', 'C', 'D'])):
  cand = 0
  li = []
  for t, f in zip(['12', '3', '6', '9'], p):
   li.append(cnt.get((f, t), 0))
   if li[-1] == 0:
    cand -= 100
 
  li = sorted(li)
  
  pr = 25
  for x in li:
   cand += pr * x
   pr += 25
  
  best = max(best, cand)
 
 print(best)
 s += best
print(s)

import numpy
from itertools import permutations 
totals=0
test=int(input())
while(test!=0):
 n=int(input())
 tw=[0,0,0,0]
 th=[0,0,0,0]
 si=[0,0,0,0]
 ni=[0,0,0,0]
 s=[0,1,2,3]
 for i in range(n):
  m,t = map(str,input().split())
  t=int(t)
  if m=="A":
   ind=0
  elif m=="B":
   ind=1
  elif m=="C":
   ind=2
  elif m=="D":
   ind=3
  if t==12:
   tw[ind]+=1
  elif t==3:
   th[ind]+=1
  elif t==6:
   si[ind]+=1
  else:
   ni[ind]+=1
 co = list(permutations(s,4))
 total=float("-inf")
 for value in co:
  p=[tw[value[0]],th[value[1]],si[value[2]],ni[value[3]]]
  p.sort(reverse=True)
  tot=0
  for i in p:
   if i==0:
     tot-=100
  tot+=p[0]*100
  tot+=p[1]*75
  tot+=p[2]*50
  tot+=p[3]*25
  if tot>total:
   total=tot
 print(total)
 totals+=total
 test-=1
print(totals)

# cook your dish here
from collections import Counter
ans=""
sum=0
for _ in range(int(input())):
 n=int(input())
 x={"A":0,"B":0,"C":0,"D":0}
 price = 0
 li = []
 for i in range(0,n):
  a = tuple(map(str, input().split()))
  li.append(a)
 #print(li)
 z=Counter(li)
 #print(z.most_common())
 cost=100
 for y in z.most_common():
  if(x[y[0][0]]==0):
   #print(y[0][0])
   x[y[0][0]]=1
   #print(y[1])
   #print(int(y[1]) * cost)
   price+=int(y[1])*cost
   cost-=25
 for p in list(x.values()):
  if(p==0):
   price-=100
 ans+=str(price)+"\n"
 sum+=price
print(ans,sum)










# cook your dish here
from collections import Counter
ans=""
sum=0
for _ in range(int(input())):
 n=int(input())
 x={"A":0,"B":0,"C":0,"D":0}
 price = 0
 li = []
 for i in range(0,n):
  a = tuple(map(str, input().split()))
  li.append(a)
 #print(li)
 z=Counter(li)
 #print(z.most_common())
 cost=100
 for y in z.most_common():
  if(x[y[0][0]]==0):
   #print(y[0][0])
   x[y[0][0]]=1
   #print(y[1])
   #print(int(y[1]) * cost)
   price+=int(y[1])*cost
   cost-=25
 for p in list(x.values()):
  if(p==0):
   price-=100
 ans+=str(price)+"\n"
 sum+=price
print(ans,sum)










netsum=0
from itertools import permutations as per
try:
 for _ in range(int(input())):
  n=int(input())
  dic={}
  for __ in range(n):
   a,b=input().split()
   if (a,b) not in dic:
    dic[(a,b)]=1
   else:
    dic[(a,b)] += 1
  best = - (10**3)
  for p in list(per(['A','B','C','D'])):
   profit = 0
   lc=[]
   for tup in zip(p,['12','3','6','9']):
    
    
    lc.append(dic.get(tup,0))
    if lc[-1]==0:
     profit-=100
   lc=sorted(lc)
   pr=25
   for x in lc:
    profit+=pr*x
    pr+=25
 
   best=max(best,profit)
  print(best)
  netsum+=best
 print(netsum)
except:
 pass

# cook your dish here
def generateIndex(indexes):
 ans=indexes[:]
 for i in range (1,4):
  for j in indexes:
   temp=[]
   for k in j:
    temp.append((k+i)%4)
   ans.append(temp)
 return ans



def countScore(x,index):
 profit=[]
 for i in range (4):
  profit.append(x[i][index[i]])
 profit.sort()
 ans=0
 m=[25,50,75,100]
 for i in range (4):
  temp=(m[i]*profit[i])
  if temp==0:
   ans-=100
  else:
   ans+=temp
 return ans
   

def ind(t):
 if t==3:
  return 0
 if t==6:
  return 1
 if t==9:
  return 2
 if t==12:
  return 3
 

t=int(input())
total=0
while t>0:
 indexes=[[0,1,2,3],[0,1,3,2],[0,2,1,3],[0,2,3,1],[0,3,1,2],[0,3,2,1]]
 allIndex=generateIndex(indexes)
 t-=1
 n=int(input())
 a=[0,0,0,0]
 b=a[:]
 c=a[:]
 d=a[:]
 for _ in range (n):
  m,t1=input().split()
  t1=int(t1)
  if m=='A':
   a[ind(t1)]+=1
  elif m=='B':
   b[ind(t1)]+=1
  elif m=='C':
   c[ind(t1)]+=1
  else:
   d[ind(t1)]+=1
 x=[a,b,c,d]
 ans=[]
 for i in allIndex:
  ans.append(countScore(x,i))
 profit=max(ans)
 total+=profit
 print(profit)
print(total)