#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int map[21][4];
int No;
struct node
{
int po[21];
int num;
}ans[1000];
bool search(node a,int x)
{
for(int i=0;i<a.num;i++)
if( a.po[i]==x ) return 1;
return 0;
}
bool cmp(const int a, const int b)
{
if(a>b) return 1;
else return 0;
}
void dfs(int m)
{
int p[3],r,i,x;
stack<node> v;
node a,b;
memset(a.po,0,sizeof(a.po));
a.po[0]=m; a.num=1;
v.push(a);
while( !v.empty() )
{
a=v.top(); v.pop();
x=a.po[a.num-1];
p[0]=map[x][1], p[1]=map[x][2], p[2]=map[x][3];
if( a.num==20 )
{
if( p[0]==m || p[1]==m || p[2]==m )
ans[No++]=a;
continue;
}
sort(p,p+3,cmp);
for(i=0;i<3;i++)
{
if( search(a,p[i])==0 )
{
b=a;
b.po[b.num]=p[i];
b.num++;
v.push(b);
}
}
}
}
int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int i,r,a,b,c,m;
memset(map,0,sizeof(map));
for(i=1;i<=20;i++)
{
scanf("%d%d%d",&a,&b,&c);
map[i][1]=a;
map[i][2]=b;
map[i][3]=c;
}
while( scanf("%d",&m) && m )
{
No=0;
dfs(m);
for(i=1;i<=No;i++)
{
printf("%d: ",i);
for(r=0;r<20;r++)
printf("%d ",ans[i-1].po[r]);
printf("%d\n",m);
}
}
return 0;
}