// Straightforward solution for problem Digital Friends, BAPC 2006
// Rudy van Vliet              1 September 2006

#include <iostream>
#include <string>  // for strlen
using namespace std;

const int MaxLength = 100;
const int Base = 10;
typedef char Integer[MaxLength+1];
typedef int IntPerDigit[Base];

//***************************************************************************

void CountOccurrences (Integer Number, IntPerDigit Occur)
 // count the occurrences of each digit in the number Number
{ int Digit,
      Length,
      i;

  for (Digit=0;Digit<Base;Digit++)
    Occur[Digit] = 0;

  Length = strlen (Number);
  for (i=0;i<Length;i++)
  { Digit = Number[i] - '0';
    Occur[Digit] ++;
  }

}  // CountOccurrences

//***************************************************************************

bool CheckDigits (IntPerDigit Occur1, IntPerDigit Occur2)
 // check if the same digits occur in Number1 as in Number2
{ bool OK;
  int Digit;

  OK = true;
  for (Digit=0;(Digit<Base)&&OK;Digit++)
  { if ( (Occur1[Digit]==0) && (Occur2[Digit]>0) )
      OK = false;
    if ( (Occur1[Digit]>0) && (Occur2[Digit]==0) )
      OK = false;
  }  // for

  return OK;

}  // CheckDigits

//***************************************************************************

bool CheckExchange (IntPerDigit Occur1, IntPerDigit Occur2, 
                    int DigitI, int DigitJ)
 // check if the exchange of one unit from DigitI to DigitJ in Number1
 // makes Number1 a friend of Number2
 // pre: the proposed exchange is a valid exchange
{ bool OK;
  int NewDigitI, NewDigitJ;

  NewDigitI = DigitI - 1;
  NewDigitJ = DigitJ + 1;
  Occur1[DigitI] --;
  Occur1[NewDigitI] ++;
  Occur1[DigitJ] --;
  Occur1[NewDigitJ] ++;
      
  OK = CheckDigits (Occur1, Occur2);

   // restore Occur1
  Occur1[DigitI] ++;
  Occur1[NewDigitI] --;
  Occur1[DigitJ] ++;
  Occur1[NewDigitJ] --;

  return OK;

}  // CheckExchange

//***************************************************************************

bool TryExchanges (Integer Number1, IntPerDigit Occur1, IntPerDigit Occur2)
 // try all possible (single) neighbour exchanges in Number1 to make it 
 // a friend of Number2
{ int Length, i;
  bool Possible;
  char cMinDigit, cMinDigitI, cMaxDigit;

  Possible = false;
  cMinDigit = '0';  // minimal digit
  cMaxDigit = '0' + Base-1;  // maximal digit
  Length = strlen (Number1);

  for (i=0;(i<Length-1)&&(!Possible);i++)
   // try a neighbour exchange at positions i and i+1 in Number1
  { if (i==0)
      cMinDigitI = '1';
    else
      cMinDigitI = '0';
    if ((Number1[i]>cMinDigitI) && (Number1[i+1]<cMaxDigit))
      // it is possible to exchange one unit from Number1[i] to Number1[i+1]
      Possible = CheckExchange (Occur1, Occur2,
                                Number1[i]-'0', Number1[i+1]-'0');

    if (!Possible)
      if ((Number1[i]<cMaxDigit) && (Number1[i+1]>cMinDigit))
        // it is possible to exchange one unit from Number1[i+1] to Number1[i]
        Possible = CheckExchange (Occur1, Occur2,
                                  Number1[i+1]-'0', Number1[i]-'0');

  }

  return Possible;

}  // TryExchanges

//***************************************************************************

int main ()
{ int Ninst, inst;
  bool Possible;
  Integer Number1, Number2;
  IntPerDigit Occur1, Occur2;

  cin >> Ninst;
  for (inst=1;inst<=Ninst;inst++)
  { cin >> Number1 >> Number2;
    CountOccurrences (Number1, Occur1);
    CountOccurrences (Number2, Occur2);

    if (CheckDigits (Occur1, Occur2))
      cout << "friends\n";
    else  // Number1 and Number2 are not friends, but they may be
          // almost friends; check this by trying all possible (single)
          // neighbour exchanges
    {   // first try all (single) neighbour exchanges in Number1
      Possible = TryExchanges (Number1, Occur1, Occur2);
      if (!Possible)
        // a single neighbour exchange in Number1 did not work;
        // try all (single) neighbour exchanges in Number2
        Possible = TryExchanges (Number2, Occur2, Occur1);

      if (Possible)
        cout << "almost friends\n";
      else
        cout << "nothing\n";
    }
  }  // for inst

  return 0;

}

