#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef struct aa
{
string s;
int d,c;
}Node;
Node a[20];
int binary[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int flag[65536];
bool mark[16];
string str[16],outs[16];
int n;
void Dfs(int days,int cost,int sum,int num)//days表示写作业的开始日期,cost表示前面的花费,sum记录作业是否完成情况,num表示选择的作业数
{
int i,temp;
if(num == n)
{
if(flag[sum] == cost)
{
// flag[sum] = cost;
for(i = 0;i < num;i++)
outs[i] = str[i];
}
return ;
}
for(i = 1;i <= n;i++)
{
if(mark[i] == false)
{
mark[i] = true;
sum += binary[i];//要写第i门课
temp = days + a[i].c - a[i].d;
if(temp < 0)
temp = cost;
else
{
temp = temp + cost;
}
//第i门作业完成后的代价temp
if(flag[sum] > temp)
{
flag[sum] = temp;//记录状态
str[num++] = a[i].s;
Dfs(a[i].c+days,temp,sum,num);
num--;
}
sum = sum - binary[i];
mark[i] = false;
}
}
}
int main()
{
int test;
cin>>test;
while(test--)
{
cin>>n;
int i;
int st = 0;
for(i = 1;i <= n;i++)
{
cin>>a[i].s>>a[i].d>>a[i].c;
}
for(i = 0;i < 65536;i++)
{
flag[i] = 1000000;
}
memset(mark,false,sizeof(mark));//标记状态
Dfs(0,0,0,0);
for(i = 1;i <= n;i++)
{
st += binary[i];//结果存放在下标为st的flag[st]中
}
cout<<flag[st]<<endl;
for(i = 0;i < n;i++)
cout<<outs[i]<<endl;
}
return 0;
}