#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=99999;
struct p{
int l,r;string s;
}a[maxn];int dp[1<<15],pre[1<<15],time[1<<15];
void print(int x)
{
if(x==0) return;
int k=pre[x]-1;
print(x-(1<<k));
cout<<a[pre[x]].s<<endl;
}
int main()
{
int n,t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i].s>>a[i].l>>a[i].r;
memset(pre,0,sizeof(pre));memset(dp,20,sizeof(dp));
memset(time,0,sizeof(time));
dp[0]=0;
for(int i=1;i<(1<<n);i++)
{
for(int j=n-1;j>=0;j--)//刚做完第j+1件事
{
int k=1<<j;
if(i&k)
{
int t=time[i-k]+a[j+1].r-a[j+1].l;
if(t<0) t=0;//如果提前完成,不用扣分
if(dp[i]>dp[i-k]+t)
{
dp[i]=dp[i-k]+t;
time[i]=time[i-k]+a[j+1].r;
pre[i]=j+1;
}
}
}
}
cout<<dp[(1<<n)-1]<<endl;
print((1<<n)-1);
}
}