{  ACM International Collegiate Programming Contest
   Central European Regional Contest 2000
   
   Problem G - The Game of Master-Mind
   Martin Kacer
}

Program MasterMind;
Const MAX_PEGS = 64;
	MAX_COLORS = 64;
	MAX_GUESS  = 260;
Type TCode = Array [1..MAX_PEGS] of Integer;

Var TaskCnt, TaskNum		 : Integer;
	Guesses					 : Array [1..MAX_GUESS] of TCode;
	FeedbackB, FeedbackBW	 : Array [1..MAX_GUESS] of Integer;
	SecCode					 : TCode;
	I, J					 : Integer;
	PegNum, ColNum, GuessNum : Integer;
	DebugCnt				 : Integer;


{ compare whole guess with the first `N' pegs of `SecCode' }
Procedure Evaluate (Var Guess : TCode; N : Integer; Var B, BW : Integer);
Var I		   : Integer;
	Num1, Num2 : Array [1..MAX_COLORS] of Integer;
Begin
	If (N = PegNum) then DebugCnt:= DebugCnt + 1;
	B:= 0; BW:= 0;
	For I:= 1 to ColNum do Begin Num1[I]:= 0; Num2[I]:= 0; End;
	For I:= 1 to N do
		If (SecCode[I] = Guess[I]) then B:= B + 1;
	For I:= 1 to N do
		Num1[SecCode[I]]:= Num1[SecCode[I]] + 1;
	For I:= 1 to PegNum do
		Num2[Guess[I]]:= Num2[Guess[I]] + 1;
	For I:= 1 to ColNum do
		If (Num1[I] < Num2[I]) then
			BW:= BW + Num1[I]
		else
			BW:= BW + Num2[I];
End; { Evaluate }


{ check if the first `N' pegs of `SecCode' can be a valid guess }
Function IsPossible (N : Integer) : Boolean;
Var I	   : Integer;
	B, BW  : Integer;
	Possib : Boolean;
Begin
	Possib:= true;
	I:= 1;
	While (I <= GuessNum) and Possib do
	Begin
		Evaluate (Guesses[I], N, B, BW);
		If (B > FeedbackB[I]) or (B+PegNum-N < FeedbackB[I])
			or (BW > FeedBackBW[I]) or (BW+PegNum-N < FeedBackBW[I])
			then Possib:= false;
		I:= I + 1;
	End;
	{Write ('DEBUG: ');
	For I:= 1 to N do Write (SecCode[I], ' ');
	WriteLn (Possib);}
	IsPossible:= Possib;
End; { IsPossible }


{ generate all colors at the specified position }
Function Generate (N : Integer) : Boolean;
Var I	: Integer;
	Brk	: Boolean;
Begin
	Brk:= (N > PegNum);
	I:= 1;
	While (I <= ColNum) and not Brk do
	Begin
		SecCode[N]:= I;
		If IsPossible (N) then
			If Generate (N+1) then
				Brk:= true;
		I:= I + 1;
	End;
	Generate:= Brk;
End; { Generate }


Begin
	Readln (TaskCnt);
	For TaskNum:= 1 to TaskCnt do
	Begin
		DebugCnt:= 0;
		ReadLn (PegNum, ColNum, GuessNum);
		For I:= 1 to GuessNum do Begin
			For J:= 1 to PegNum do Read (Guesses[I,J]);
			ReadLn;
			ReadLn (FeedbackB[I], FeedbackBW[I]);
			FeedbackBW[I]:= FeedbackBW[I] + FeedbackB[I];
		End;
		If Generate (1) then Begin
			Write (SecCode[1]);
			If (PegNum > 1) then For J:= 2 to PegNum do
				Write (' ', SecCode[J]);
			WriteLn;
		End else
			WriteLn ('You are cheating!');
		{WriteLn ('DEBUG: ', DebugCnt);}
	End;
End.
