#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
int a[1000]={0},b[1000]={0},c[1000]={0}, count1 = 0, count2 = 0, count3 = 0,j;
int n1,n2;
while(cin>>n1>>n2&&n1!=0&&n2!=0)
{
a[count1++] = n1;
b[count2++] = n2;
}
for( j= 0;j <count1; j++)
{
bool flag=1;
for(int t =0;t <count2;t++)
{
if(a[j]==b[t])
{
flag=0;break;
}
}
if(flag)
{
c[count3++]=a[j];
}
}
for( j= 0;j < count2; j++)
{
bool flag=1;
for(int t =0;t < count1;t++)
{
if(b[j]==a[t])
{
flag=0;break;
}
}
if(flag)
{
c[count3++]=b[j];
}
}
sort(c,c+count3);
cout<<"{";
for(int w = 0;w < count3;w++)
{
if(w==0)
{
cout<<c[w];
}
else
{
cout<<","<<c[w];
}
}
cout<<"}";
cout<<endl;
}
}
return 0;
}