(*------------------------------------------------------------
TEAM54
A
210
Target: BDS2006; Platform: Win32; Comment: Borland Developer Studio 2006
------------------------------------------------------------*)
{Выигрывает в 1 ход, 
не проигрывает в 1 ход, 
если можно срубить рубит рандом, 
если можно сходить не под бой, ходит рандом из верхних
(это позволяет выигрывать в 2 хода)}
{$apptype console}
const BOARD_SIZE=8;
type TCell=(EMPTY,WHITE,BLACK);
     GameMove=record
       sx,sy,ex,ey:integer;
     end;

var board:array[0 .. BOARD_SIZE - 1,0 .. BOARD_SIZE - 1] of TCell;
    currentColor:TCell;
function CreateGameMove(sx,sy,ex,ey:integer):GameMove;
var res:GameMove;
begin
  res.sx:=sx;
  res.sy:=sy;
  res.ex:=ex;
  res.ey:=ey;
  CreateGameMove:=res;
end;
function ParseGameMove(move:string):GameMove;
begin
  ParseGameMove := CreateGameMove(ord(move[2]) - ord('1'), ord(move[1]) - ord('a'), ord(move[4]) - ord('1'), ord(move[3]) - ord('a'));
end;
function GameMoveToString (move : GameMove) : string;
begin
  GameMoveToString := char(ord('a') + move.sy) + char(ord('1') + move.sx) + char(ord('a') + move.ey) + char(ord('1') + move.ex);
end;
procedure Init;
var cell:TCell;
    x,y:integer;
begin
  for x := 0 to BOARD_SIZE - 1 do
    begin
      if (x <= 1) then
        cell := WHITE 
        else
	if (x >= BOARD_SIZE - 2) then
	cell:=BLACK
	else
	cell := EMPTY;
	for y:=0 to BOARD_SIZE - 1 do
	board[x,y]:=cell;
    end;
  currentColor := WHITE;
  RandSeed := 314159265;
end;
procedure MakeMove(move : GameMove);
begin
  board[move.ex, move.ey] := board[move.sx, move.sy];
  board[move.sx, move.sy] := EMPTY;
  if (currentColor = WHITE) then
    currentColor := BLACK 
  else
  currentColor := WHITE;
end;
function GetRandomMove : GameMove;
var moves,rubi,na,win2:array [0 .. 3 * BOARD_SIZE * BOARD_SIZE - 1] of GameMove;
    moves_count,rubi_count,na_count,win2_count : integer;
    sx, sy, dy, ex, ey,i,tt,qw,qb : integer;
    ch:boolean;
begin
  moves_count := 0;
  rubi_count:=0;
  na_count:=0;
  win2_count:=0;
  for sx := 0 to BOARD_SIZE - 1 do
  for sy := 0 to BOARD_SIZE - 1 do
  if(board[sx, sy] = currentColor) then
  for dy := -1 to 1 do
    begin
      if (currentColor = WHITE) then
        ex := sx + 1
        else
        ex := sx - 1;
      ey := sy + dy;
      if (ex >= 0) and (ex < BOARD_SIZE) and (ey >= 0) and (ey < BOARD_SIZE) and
	 (((dy <> 0) and (board[ex, ey] <> currentColor)) or (board[ex, ey] = EMPTY)) then
	begin
	  moves[moves_count] := CreateGameMove(sx, sy, ex, ey);
	  inc(moves_count);
	  if board[ex,ey]<>EMPTY then
	    begin
              rubi[rubi_count] := CreateGameMove(sx, sy, ex, ey);	      
              inc(rubi_count);
	    end;
	  ch:=true;
	  if (currentColor=WHITE) and (ex=(BOARD_SIZE-2)) then
	    begin
	      qw:=0;
	      qb:=0;
	      if (ex<(BOARD_SIZE-1)) and (ey>0) and (board[ex+1,ey-1]=BLACK) then
	        inc(qb);
	      if (ex<(BOARD_SIZE-1)) and (ey<(BOARD_SIZE-1)) and (board[ex+1,ey+1]=BLACK) then
	        inc(qb);
	      if (ex>0) and (ey>0) and (board[ex-1,ey-1]=WHITE) and (((ex-1)<>sx) or ((ey-1)<>sy)) then
	        inc(qw);
	      if (ex>0) and (ey<(BOARD_SIZE-1)) and (board[ex-1,ey+1]=WHITE) and (((ex-1)<>sx) or ((ey+1)<>sy))then
	        inc(qw);
	      if qw>=qb then
	        begin
	          win2[win2_count]:=CreateGameMove(sx,sy,ex,ey);
	          inc(win2_count);
	        end;
	    end;
	  if (currentColor=BLACK) and (ex=1) then
	    begin
	      qw:=0;
	      qb:=0;
	      if (ex<(BOARD_SIZE-1)) and (ey>0) and (board[ex+1,ey-1]=BLACK) and (((ex+1)<>sx) or ((ey-1)<>sy)) then
	        inc(qb);
	      if (ex<(BOARD_SIZE-1)) and (ey<(BOARD_SIZE-1)) and (board[ex+1,ey+1]=BLACK) and (((ex+1)<>sx) or ((ey+1)<>sy)) then
	        inc(qb);
	      if (ex>0) and (ey>0) and (board[ex-1,ey-1]=WHITE) then
	        inc(qw);
	      if (ex>0) and (ey<(BOARD_SIZE-1)) and (board[ex-1,ey+1]=WHITE) then
	        inc(qw);
	      if qw<=qb then
	        begin
	          win2[win2_count]:=CreateGameMove(sx,sy,ex,ey);
	          inc(win2_count);
	        end;
	    end;
	  if (currentColor=WHITE) then
	    begin
	      if (ex<(BOARD_SIZE-1)) and (ey>0) and (board[ex+1,ey-1]=BLACK) then
	        ch:=false;
	      if (ex<(BOARD_SIZE-1)) and (ey<(BOARD_SIZE-1)) and (board[ex+1,ey+1]=BLACK) then
	        ch:=false;
	    end;
	  if (currentColor=BLACK) then
	    begin
	      if (ex>0) and (ey>0) and (board[ex-1,ey-1]=WHITE) then
	        ch:=false;
	      if (ex>0) and (ey<(BOARD_SIZE-1)) and (board[ex-1,ey+1]=WHITE) then
	        ch:=false;
	    end;
	  if ch then
	    begin
	      na[na_count]:=CreateGameMove(sx,sy,ex,ey);
	      inc(na_count);
	    end;
	end;
    end;
  if (currentColor=WHITE) then
    begin
      if (moves[moves_count-1].ex=BOARD_SIZE-1) then
        GetRandomMove:=moves[moves_count-1] else
          begin
            if rubi_count>0 then
              begin
                if rubi[0].ex=1 then
                  GetRandomMove:=rubi[0] else
                begin
                  if win2_count>0 then
                    GetRandomMove:=win2[0] else
                GetRandomMove:=rubi[0];
                end;
              end else
            begin
              if win2_count>0 then
                GetRandomMove:=win2[0] else
                begin
              if na_count>0 then
                begin
                  tt:=0;
                  for i:=0 to na_count-1 do
                    if na[i].ex=na[na_count-1].ex then
                      inc(tt);
                  GetRandomMove:=na[na_count-1-random(tt)]; 
                end  else
              GetRandomMove := moves[Random(moves_count)];
            end;
            end;
          end;
    end;
  if (currentColor=BLACK) then
    begin
      if (moves[0].ex=0) then
        GetRandomMove:=moves[0] else
            if rubi_count>0 then
              begin
                if rubi[rubi_count-1].ex=BOARD_SIZE-1 then
                  GetRandomMove:=rubi[rubi_count-1] else
                begin
                  if win2_count>0 then
                    GetRandomMove:=win2[0] else
                GetRandomMove:=rubi[rubi_count-1];
                end;
              end else
            begin
              if win2_count>0 then
                GetRandomMove:=win2[0] else
                begin
              if na_count>0 then  
                begin
                  tt:=0;
                  for i:=0 to na_count-1 do
                    if na[i].ex=na[0].ex then
                      inc(tt);
                  GetRandomMove:=na[random(tt)]; 
                end  else
              GetRandomMove := moves[Random(moves_count)];
            end;
            end;
    end;

end;
var command : string;
    move : GameMove;
begin
Init;
while (true) do
  begin
    readln (command);
    if(command = 'Quit') then
      break;
    if(command = 'Name') then
      begin
        Writeln('Lenyuk');
	Flush(Output);
	continue;
      end;
    if(command <> 'Start') then
      MakeMove(ParseGameMove(command));
    move := GetRandomMove;
    MakeMove(move);
    Writeln(GameMoveToString(move));
    Flush(Output);
   end;
end.
