## DS-K的博客

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 999999999
#define mod 100000007

int n,nA,nB;
struct node
{
int arrive,run;
}A[209],B[209];

int dp[209][209][2];

void init()
{
cin>>n;
nA = nB = 0;
rep(i,1,n)
{
char c;
getchar();
c = getchar();
if ( c == 'A' )
nA++ , scanf("%d%d",&A[nA].arrive,&A[nA].run);
else
nB++ , scanf("%d%d",&B[nB].arrive,&B[nB].run);
}
Clean(dp,0x3f);
dp[0][0][0] = dp[0][0][1] = 0;
}

void solve()
{
rep(i,0,nA)
rep(j,0,nB)
{
int st = dp[i][j][0],ed = 0;
rep(k,i+1,nA)
{
st = max( st , A[k].arrive );
ed = max( ed , st + A[k].run );
dp[k][j][1] = min( dp[k][j][1] , ed );
st += 10 , ed += 10;
}
st = dp[i][j][1];
ed = 0;
rep(k,j+1,nB)
{
st = max( st , B[k].arrive );
ed = max( ed , st + B[k].run );
dp[i][k][0] = min( dp[i][k][0] , ed );
st += 10 , ed += 10;
}
}
}

int main()
{
int T;
cin>>T;
while(T--)
{
init();
solve();
cout<<min( dp[nA][nB][0] , dp[nA][nB][1] )<<endl;
}
return 0;
}



12-18 409

10-19 45

03-15 1200

03-15 1676

07-22 781

02-04 1244

08-24 201