import java.util.Scanner;

/**
 * Created by sorin on 23.03.2017.
 */
public class prog {

    public static void main(final String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] s = scan.nextLine().split(" ");
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(s[0]);
        for (int i = 0; i < n; i++) {

            String[] hexes = scan.nextLine().split(" ");
            for (int j = 0; j < hexes.length; j++) {
                sb.append(hexToShort(hexes[j])).append(" ");
            }
            sb.append(System.lineSeparator());

        }
        System.out.println(sb.toString());
    }

    private static String hexToShort(String hex) {

        if ( hex.length() == 4 ){
            return hex;
        }


        if ( hex.charAt(0) == '#' ) {
            hex = hex.substring(1);
        }

        if ( hex.length() != 6 ) {
            return "";
        }

        String r = hex.substring(0,2);
        String g = hex.substring(2,4);
        String b = hex.substring(4,6);

        return "#" + shortVal(r) + shortVal(g) + shortVal(b);

    }

    private static String shortVal(String c) {
        int ci = Integer.parseInt(c, 16);
        return Integer.toString((ci%17 > 7) ? (17+ci-ci%17) : (ci-ci%17), 16).substring(0,1).toUpperCase();
    }
}