(*
 * Program:  friend_bram
 * Author:   Bram Kuijvenhoven (bram at kuijvenhoven.net)
 * Language: Pascal (FreePascal 2.x or higher recommended)
 * Notes:    Example/test solution to 'Digital Friends' problem of BAPC 2006
 *)

program friend_bram;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils;

type
  TFreq = array['0'..'9'] of integer;
  TDigits = string[100];

function Friends(const xfreq,yfreq:TFreq):boolean;
var
  c:char;
begin
  for c:='0' to '9' do
    if (xfreq[c]>0)<>(yfreq[c]>0) then
      Exit(false);
  Exit(true);
end;

function AlmostFriends(const x,y:TDigits; var xfreq,yfreq:TFreq):boolean;
var
  i:integer;
begin
  for i:=1 to Length(x)-1 do
  begin
    if (x[i]>'1') or ((x[i]='1') and (i>1)) then
      if x[i+1]<'9' then
      begin
        Dec(xfreq[x[i]]);
        Dec(xfreq[x[i+1]]);
        Inc(xfreq[Chr(Ord(x[i])  -1)]);
        Inc(xfreq[Chr(Ord(x[i+1])+1)]);
        if Friends(xfreq,yfreq) then
          Exit(true);
        Dec(xfreq[Chr(Ord(x[i+1])+1)]);
        Dec(xfreq[Chr(Ord(x[i])  -1)]);
        Inc(xfreq[x[i+1]]);
        Inc(xfreq[x[i]]);
      end;
    if (x[i]<'9') then
      if x[i+1]>'0' then
      begin
        Dec(xfreq[x[i]]);
        Dec(xfreq[x[i+1]]);
        Inc(xfreq[Chr(Ord(x[i])  +1)]);
        Inc(xfreq[Chr(Ord(x[i+1])-1)]);
        if Friends(xfreq,yfreq) then
          Exit(true);
        Dec(xfreq[Chr(Ord(x[i+1])-1)]);
        Dec(xfreq[Chr(Ord(x[i])  +1)]);
        Inc(xfreq[x[i+1]]);
        Inc(xfreq[x[i]]);
      end;
  end;
  Exit(false);
end;
  
var
  numTestCases, testCase: integer;
  x,y:TDigits;
  xfreq,yfreq:TFreq;
  i:integer;
  c:char;

begin

  readln(numTestCases);
  for testCase:=1 to numTestCases do
  begin
    x:='';
    repeat
      read(c);
      case c of
        '0'..'9':x+=c;
        ' ':break;
      else
        raise Exception.CreateFmt('Invalid character #%d (%c) found in input',[Ord(c),c]);
      end
    until false;
    y:='';
    while not EOLn do
    begin
      read(c);
      case c of
        '0'..'9':y+=c;
      else
        raise Exception.CreateFmt('Invalid character #%d (%c) found in input',[Ord(c),c]);
      end;
    end;
    readln;
    for c:='0' to '9' do begin xfreq[c]:=0; yfreq[c]:=0; end;
    for i:=1 to Length(x) do Inc(xfreq[x[i]]);
    for i:=1 to Length(y) do Inc(yfreq[y[i]]);
    if Friends(xfreq,yfreq) then
      writeln('friends')
    else if AlmostFriends(x,y,xfreq,yfreq) or AlmostFriends(y,x,yfreq,xfreq) then
      writeln('almost friends')
    else
      writeln('nothing');
  end;

end.

