import java.io.PrintStream; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Scanner; public class A { public static void main(String[] args) throws ParseException { Scanner input = new Scanner(System.in); PrintStream output = System.out; DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM); int number = Integer.parseInt(input.nextLine()); for (int i = 0; i < number; i++) { List dates = new ArrayList(); boolean goOn = true; do { Date date = formatter.parse(input.nextLine()); dates.add(date); goOn = date.after(new Date()); } while (goOn); Collections.sort(dates); output.println(formatter.format(dates.get(1))); output.println(formatter.format(dates.get(dates.size() - 1))); } } }