program ROMLETTERS11(input, output);
type    number = array [1..75] of byte;
var  K, I : word;
     RESMIN, RESMAX, AI : number;
function roman2Decimal(DIGIT : char): byte;
var RESULT : byte;
begin
    case DIGIT of
        'N' : begin RESULT := 0 end;
        'I' : begin RESULT := 1 end;
        'V' : begin RESULT := 5 end;
        'X' : begin RESULT := 10 end;
        'L' : begin RESULT := 50 end;
        else RESULT := 255;
    end;
    roman2Decimal := RESULT
end;
function decimal2Letter(DIGIT : byte): char;
var RESULT : char;
begin
    case DIGIT of
        0..25 : begin RESULT := chr(ord('A') + DIGIT) end;
        26..51 : begin RESULT := chr(ord('a') + DIGIT - 26) end;
        else RESULT := '#';
    end;
    decimal2Letter := RESULT
end;
procedure readRoman(var N : byte);
var BUFFER : array[1..25] of byte;
    I, J : word; CH : char; D : byte;
begin
    for I := 1 to 25 do BUFFER[I] := 255;
    if not (eoln or eof) then begin
        read(CH);
        I := 1;
        D := roman2Decimal(CH);
        while (D < 52) and (I < 26) do begin
            BUFFER[I] := D;
            if not (eoln or eof) then begin
                read(CH);
                D := roman2Decimal(CH);
                I := I + 1;
            end
            else D := 255
        end;
        if (I = 1) and (BUFFER[1] > 51) then N := 255
        else begin
            I := 1;
            N := 0;
            while (I < 26) and (BUFFER[I] < 52) do begin
              J := 1;
              N := N + BUFFER[I];
              if (I > J) and (BUFFER[I] = 50) and (BUFFER[I - J] = 10) then			  
               while (I > J) and (BUFFER[I - J] = 10) do begin
                N := N - 20;
                J := J + 1;
               end
              else if (I > J) and (BUFFER[I] > 1) and (BUFFER[I - J] = 1) then			  
               while (I > J) and (BUFFER[I - J] = 1) do begin
                N := N - 2;
                J := J + 1;
               end;
              I := I + 1;
            end
        end
    end
    else N := 255
end;
procedure writeNumber(var A : number);
var I, J : word;
begin
    I := 1;
    while (I < 75) and (A[I] = 0) do I := I + 1;
    for J := I to 75 do write(decimal2Letter(A[J]))
end;
procedure readNumber(var A : number);
var CH : byte; I, J : word;
begin for I := 1 to 75 do A[I] := 0;
    readRoman(CH);
    I := 1;
    J := 0;
    while (I <= 75) and (CH < 52) do begin
        J := J + 1; A[J] := CH;
        if not (eoln or eof) then begin
            readRoman(CH);
            I := I + 1
        end else I := 76
    end;
    for I := J downto 1 do A[75 - J + I] := A[I];
    for I := 1 to 75 - J do A[I] := 0;
end;

begin readln(K);
    readNumber(RESMIN);
    RESMAX := RESMIN;
    if eoln and not(eof) then readln;
    for I := 2 to K do begin
        readNumber(AI);
        if eoln and not(eof) then readln;
        if (CompareByte(RESMAX, AI, 75) < 0) then RESMAX := AI
        else if (CompareByte(AI, RESMIN, 75) < 0) then RESMIN := AI
    end;
    writeNumber(RESMIN);
    writeln;
    writeNumber(RESMAX)
end.
