#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
using namespace std;
const int max1 = 1e5;
const int inf = 0x3f3f3f3f;
int T, n, m;
string c[max1 + 5], p[max1 + 5], s[max1 + 5];
int t[max1 + 5];
map <string, int> Map_name;
string name[max1 + 5];
int cnt;
struct Node
{
int t, num;
Node () {}
Node ( int __t, int __num )
{ t = __t, num = __num; }
bool operator < ( const Node &A ) const
{
if ( t == A.t )
return num < A.num;
return t < A.t;
}
bool operator == ( const Node &A ) const
{
return t == A.t && num == A.num;
}
};
multiset <Node> tim[max1 + 5][26][2];
vector <Node> seq[max1 + 5][26];
struct Data
{
int cntpro, sumtime;
Data () {}
Data ( int __cntpro, int __sumtime )
{ cntpro = __cntpro, sumtime = __sumtime; }
bool operator < ( const Data &A ) const
{
if ( cntpro == A.cntpro )
return sumtime < A.sumtime;
return cntpro > A.cntpro;
}
bool operator == ( const Data &A ) const
{
return cntpro == A.cntpro && sumtime == A.sumtime;
}
bool operator <= ( const Data &A ) const
{
return *this < A || *this == A;
}
};
map <Data, int> bin;
Data val[max1 + 5], d[max1 + 5];
int len;
Data Calc ()
{
int lim = min((n + 9) / 10, 35);
int precnt = 0;
if ( precnt >= lim )
return Data(inf, 0);
for ( auto v : bin )
{
precnt += v.second;
if ( precnt >= lim )
return v.first;
}
return (*--bin.end()).first;
}
int sum, ans[max1 + 5];
void Work ()
{
scanf("%d", &m);
Map_name.clear(); cnt = 0;
for ( int i = 1; i <= m; i ++ )
{
cin >> c[i] >> p[i] >> t[i] >> s[i];
if ( Map_name.find(c[i]) == Map_name.end() )
{
Map_name[c[i]] = ++cnt, name[cnt] = c[i];
for ( int j = 0; j < 26; j ++ )
{
seq[cnt][j].clear();
for ( int k = 0; k < 2; k ++ )
tim[cnt][j][k].clear();
}
}
if ( s[i][0] == 'a' )
tim[Map_name[c[i]]][p[i][0] - 'A'][0].insert(Node(t[i], i));
else
tim[Map_name[c[i]]][p[i][0] - 'A'][1].insert(Node(t[i], i));
seq[Map_name[c[i]]]->push_back(Node(t[i], i));
}
n = 0; bin.clear(); len = 0;
for ( int i = 1; i <= cnt; i ++ )
{
val[i].cntpro = val[i].sumtime = ans[i] = 0;
for ( int j = 0; j < 26; j ++ )
{
if ( !tim[i][j][0].empty() )
{
++val[i].cntpro;
val[i].sumtime += (*tim[i][j][0].begin()).t;
val[i].sumtime += lower_bound(seq[i][j].begin(), seq[i][j].end(), (*tim[i][j][0].begin())) - seq[i][j].begin();
}
}
if ( val[i].cntpro )
++n;
++bin[val[i]];
// printf("i = %d val = %d %d\n", i, val[i].cntpro, val[i].sumtime);
}
d[++len] = Calc();
// printf("n = %d d : cntpro = %d sumtime = %d\n", n, d[len].cntpro, d[len].sumtime);
for ( int i = 1; i <= m; i ++ )
{
int id = Map_name[c[i]];
Data preval = val[id];
--bin[val[id]];
if ( val[id].cntpro )
--n;
if ( !tim[id][p[i][0] - 'A'][0].empty() )
{
--val[id].cntpro;
val[id].sumtime -= (*tim[id][p[i][0] - 'A'][0].begin()).t;
int rk = lower_bound(seq[id][p[i][0] - 'A'].begin(), seq[id][p[i][0] - 'A'].end(), (*tim[id][p[i][0] - 'A'][0].begin())) - seq[id][p[i][0] - 'A'].begin();
val[id].sumtime -= rk * 20;
}
if ( s[i][0] == 'a' )
{
tim[id][p[i][0] - 'A'][0].erase(tim[id][p[i][0] - 'A'][0].find(Node(t[i], i)));
tim[id][p[i][0] - 'A'][1].insert(Node(t[i], i));
}
else
{
tim[id][p[i][0] - 'A'][1].erase(tim[id][p[i][0] - 'A'][1].find(Node(t[i], i)));
tim[id][p[i][0] - 'A'][0].insert(Node(t[i], i));
}
if ( !tim[id][p[i][0] - 'A'][0].empty() )
{
++val[id].cntpro;
val[id].sumtime += (*tim[id][p[i][0] - 'A'][0].begin()).t;
int rk = lower_bound(seq[id][p[i][0] - 'A'].begin(), seq[id][p[i][0] - 'A'].end(), (*tim[id][p[i][0] - 'A'][0].begin())) - seq[id][p[i][0] - 'A'].begin();
val[id].sumtime += rk * 20;
}
if ( val[id].cntpro )
++n;
++bin[val[id]];
d[++len] = Calc();
// printf("n = %d d : cntpro = %d sumtime = %d\n", n, d[len].cntpro, d[len].sumtime);
if ( preval <= d[len] )
--ans[id];
if ( val[id] <= d[len] )
++ans[id];
// printf("id = %d ans = %d\n", id, ans[id]);
--bin[val[id]];
if ( val[id].cntpro )
--n;
if ( !tim[id][p[i][0] - 'A'][0].empty() )
{
--val[id].cntpro;
val[id].sumtime -= (*tim[id][p[i][0] - 'A'][0].begin()).t;
int rk = lower_bound(seq[id][p[i][0] - 'A'].begin(), seq[id][p[i][0] - 'A'].end(), (*tim[id][p[i][0] - 'A'][0].begin())) - seq[id][p[i][0] - 'A'].begin();
val[id].sumtime -= rk * 20;
}
if ( s[i][0] == 'a' )
{
tim[id][p[i][0] - 'A'][1].erase(tim[id][p[i][0] - 'A'][1].find(Node(t[i], i)));
tim[id][p[i][0] - 'A'][0].insert(Node(t[i], i));
}
else
{
tim[id][p[i][0] - 'A'][0].erase(tim[id][p[i][0] - 'A'][0].find(Node(t[i], i)));
tim[id][p[i][0] - 'A'][1].insert(Node(t[i], i));
}
if ( !tim[id][p[i][0] - 'A'][0].empty() )
{
++val[id].cntpro;
val[id].sumtime += (*tim[id][p[i][0] - 'A'][0].begin()).t;
int rk = lower_bound(seq[id][p[i][0] - 'A'].begin(), seq[id][p[i][0] - 'A'].end(), (*tim[id][p[i][0] - 'A'][0].begin())) - seq[id][p[i][0] - 'A'].begin();
val[id].sumtime += rk * 20;
}
if ( val[id].cntpro )
++n;
++bin[val[id]];
}
sort(d + 1, d + 1 + len);
sum = 0;
for ( int i = 1; i <= cnt; i ++ )
{
int pos = lower_bound(d + 1, d + 1 + len, val[i]) - d;
ans[i] += len - pos + 1;
if ( ans[i] )
++sum;
// cout << name[i] << ' ';
// printf("ans = %d\n", ans[i]);
}
printf("%d\n", sum);
for ( int i = 1; i <= cnt; i ++ )
if ( ans[i] )
cout << name[i] << ' ';
cout << endl;
return;
}
int main ()
{
scanf("%d", &T);
while ( T -- )
Work();
return 0;
}