// Arup Guha
// 2/18/2024
// Solution to Kattis Problem: Dreamer
// https://open.kattis.com/problems/dreamer

import java.util.*;

public class dreamer {

	final public static int SIZE = 8;
	public static int[] digits;
	public static TreeSet<data> set;
	
	public static void main(String[] args) {

		// Get # of cases.
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// Get the digits.
			String tmp = stdin.next();
			tmp = tmp + stdin.next();
			tmp = tmp + stdin.next();
			
			// Move into the digits array.
			digits = new int[SIZE];
			for (int i=0; i<SIZE; i++)
				digits[i] = tmp.charAt(i) - '0';
			
			set = new TreeSet<data>();
			int[] perm = new int[SIZE];
			boolean[] used = new boolean[SIZE];
			go(perm, used, 0);
			
			// First the set size.
			System.out.print(set.size());
			
			// Get earliest date.
			if (set.size() > 0) {
				System.out.print(" ");
				System.out.println(set.first());
			}
			
			// Still have to go to the next line here.
			else
				System.out.println();
		}
	}
	
	public static void go(int[] perm, boolean[] used, int k) {
	
		// Filled it.
		if (k == perm.length) {
		
			// Create this set of digits.
			int[] res = new int[perm.length];
			for (int i=0; i<res.length; i++)
				res[i] = digits[perm[i]];
				
			// Make it a date and add it if it works.
			data tmp = new data(res);
			if (tmp.valid())
				set.add(tmp);
				
			return;
		}
		
		// Run usual permutation here.
		for (int i=0; i<perm.length; i++) {
			if (used[i]) continue;
			perm[k] = i;
			used[i] = true;
			go(perm, used, k+1);
			used[i] = false;
		}
	}
}

class data implements Comparable<data> {

	final public static int[] REG = {31,28,31,30,31,30,31,31,30,31,30,31};
	final public static int[] LEAP = {31,29,31,30,31,30,31,31,30,31,30,31};

	public int[] digits;
	public int month;
	public int day;
	public int year;
	
	// Annoying.
	public data(int[] arr) {
		digits = new int[arr.length];
		for (int i=0; i<arr.length; i++)
			digits[i] = arr[i];
		day = 10*arr[0] + arr[1];
		month = 10*arr[2] + arr[3];
		year = 1000*arr[4] + 100*arr[5] + 10*arr[6] + arr[7];
	}
	
	// Regular way to compare dates.
	public int compareTo(data other) {
		if (this.year != other.year) return this.year-other.year;
		if (this.month != other.month) return this.month-other.month;
		return this.day - other.day;
	}
	
	// Returns true iff this object is a leap year date.
	public boolean isleap() {
		if (year%4 != 0) return false;
		if (year%100 == 0 && year%400 != 0) return false;
		return true;
	}
	
	// Returns true iff this date is valid according to the problem.
	public boolean valid() {
	
		// These don't work.
		if (year < 2000) return false;
		
		// This doesn't work.
		if (month < 1 || month > 12) return false;
		
		// Otherwise, it's down to the day.
		if (isleap())
			return day >= 1 && day <= LEAP[month-1];
		return day>=1 && day <= REG[month-1];
	}
	
	// String representation for the problem.
	public String toString() {
		String res = "";
		for (int i=0; i<digits.length; i++) {
			res = res + digits[i];
			
			// This is goofy.
			if (i == 1 || i == 3) res = res + " ";
		}
		
		return res;
	}

}