
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class clock {

    private static final List<Integer> set = Arrays.asList(new Integer[]{
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    });
    private static final List<Integer> setMF = Arrays.asList(new Integer[]{
        0, 1, 2, 3, 4, 5
    });
    private static final List<Integer> setHF = Arrays.asList(new Integer[]{
        0, 1, 2
    });
    static String[][] x = new String[10][];

    static int lastTime = -1;
    static final int day = 24*60;
    
    static {
        x[0] = new String[]{
            " - ",
            "| |",
            " . ",
            "| |",
            " - "
        };

        x[8] = new String[]{
            " - ",
            "| |",
            " - ",
            "| |",
            " - "
        };
        x[0] = new String[]{
            " - ",
            "| |",
            " . ",
            "| |",
            " - "
        };

        x[8] = new String[]{
            " - ",
            "| |",
            " - ",
            "| |",
            " - "
        };
        x[2] = new String[]{
            " - ",
            ". |",
            " - ",
            "| .",
            " - "
        };
        x[3] = new String[]{
            " - ",
            ". |",
            " - ",
            ". |",
            " - "
        };
        x[5] = new String[]{
            " - ",
            "| .",
            " - ",
            ". |",
            " - "
        };
        x[9] = new String[]{
            " - ",
            "| |",
            " - ",
            ". |",
            " - "
        };
        x[6] = new String[]{
            " - ",
            "| .",
            " - ",
            "| |",
            " - "
        };
        x[7] = new String[]{
            " - ",
            ". |",
            " . ",
            ". |",
            " . "
        };
        x[1] = new String[]{
            " . ",
            ". |",
            " . ",
            ". |",
            " . "
        };
        x[4] = new String[]{
            " . ",
            "| |",
            " - ",
            ". |",
            " . "
        };
        x[2] = new String[]{
            " - ",
            ". |",
            " - ",
            "| .",
            " - "
        };
        x[3] = new String[]{
            " - ",
            ". |",
            " - ",
            ". |",
            " - "
        };
        x[5] = new String[]{
            " - ",
            "| .",
            " - ",
            ". |",
            " - "
        };
        x[9] = new String[]{
            " - ",
            "| |",
            " - ",
            ". |",
            " - "
        };
        x[6] = new String[]{
            " - ",
            "| .",
            " - ",
            "| |",
            " - "
        };
        x[7] = new String[]{
            " - ",
            ". |",
            " . ",
            ". |",
            " . "
        };
        x[1] = new String[]{
            " . ",
            ". |",
            " . ",
            ". |",
            " . "
        };
        x[4] = new String[]{
            " . ",
            "| |",
            " - ",
            ". |",
            " . "
        };
    }

    public static void main(String[] args) throws Exception {



        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        int last = -1;
        boolean integ = true;
        boolean firstL = true;
        String[][] rums = null;
        int pos = 0;
        int len;
        
        int minTime = -1;
        int maxTime = -1;

        int ch[][] = new int[2][4];

        while ((line = br.readLine()) != null) {
            if (integ) {
                if (line.equals("")) {
                    break;
                }
                if (last == -1) {
                    last = Integer.parseInt(line);
                    if (last == 0) {
                        break;
                    }
                    lastTime = -1;
                    last--;
                } else {
                    String[] p = line.split("\\s");
                    minTime = Integer.parseInt(p[0]);
                    maxTime = Integer.parseInt(p[1]); 
                }
                integ = false;
                last--;
                pos = 0;
                firstL = true;
                

            } else {
                if (firstL) {
                    firstL = false;
                    char[] lineC = line.toCharArray();
                    len = lineC.length;
                    int p = 3;
                    for (int i = len - 1; i >= 0; i--) {
                        if (lineC[i] != ' ') {
                            ch[0][p] = i - 1;
                            ch[1][p--] = i + 2;
                        }

                        if (p == 0) {
                            ch[0][0] = ch[0][1] - (ch[0][3] - ch[0][2]);
                            ch[1][0] = ch[1][1] - (ch[1][3] - ch[1][2]);
                        }
                    }
                    rums = new String[4][5];
                    pos = 0;
                    //continue;
                }

                rums[0][pos] = line.substring(ch[0][0], ch[1][0]);
                rums[1][pos] = line.substring(ch[0][1], ch[1][1]);
                rums[2][pos] = line.substring(ch[0][2], ch[1][2]);
                rums[3][pos] = line.substring(ch[0][3], ch[1][3]);
                pos++;
                if (pos == 5) {
                    integ = true;
                    eval(minTime, maxTime, rums);
                if (last == -1) {
                    System.out.println();
                }
                }
            }


        }
    }

    private static void eval(int min, int max, String[][] rums) {
        Set<Integer> dg1 = new HashSet<Integer>(setHF);
        Set<Integer> dg2 = new HashSet<Integer>(set);
        Set<Integer> dg3 = new HashSet<Integer>(setMF);
        Set<Integer> dg4 = new HashSet<Integer>(set);

        doClean(dg1, rums[0]);
        doClean(dg2, rums[1]);
        doClean(dg3, rums[2]);
        doClean(dg4, rums[3]);

        Set<Integer> times = new HashSet<Integer>();
        int minT;
        int hT;
        int mT;
        
        for (Integer h1 : dg1) {
                for (Integer h2 : dg2) {
                    for (Integer m1 : dg3) {
                        for (Integer m2 : dg4) {
                            hT = h1*10+h2;
                            if (hT > 23) {
                                continue;
                            }
                            
                            mT = m1*10+m2;
                            if (mT > 59) {
                                continue;
                            }
                            
                            minT = hT*60+mT;
                            times.add(minT);
                        }
                    }
                }

            }
        
        if (lastTime == -1) {
            if (times.size() == 1) {
                int a = times.iterator().next();
                printTime(a);
                lastTime = a;
            } else {
                System.out.println("ambiguous, " + times.size() + " possibilities");
            }
        } else {
            int mit = lastTime + min;
            if (mit >= day) {
                mit -= day;
            }
            int mat = lastTime + max;
            if (mat >= day) {
                mat -= day;
            }
            
            boolean opacne = mat < mit;
            
            Integer tx;
            Iterator<Integer> iter = times.iterator();
            while (iter.hasNext()) {
                tx = iter.next();
                if (!opacne && (tx < mit || tx > mat)) {
                    iter.remove();
                } else if (opacne && (tx > mat && tx < mit)) {
                    iter.remove();
                }
            }
            
            if (times.size() == 1) {
                int a = times.iterator().next();
                printTime(a);
                lastTime = a;
            } else {
                System.out.println("ambiguous, " + times.size() + " possibilities");
            }
        }

    }

    private static void doClean(Set<Integer> dg1, String[] line) {
        for (int i = 0; i < 5; i++) {
            //System.err.println(line[i]);
            if (i == 0 && line[i].charAt(1) == ' ') {
                dg1.clear();
                dg1.add(0);
                break;
            }

            for (int cislo = 0; cislo < 10; cislo++) {
                if (!line[i].equals(x[cislo][i])) {
                    if (i == 1 || i == 3) {
                        if (line[i].charAt(0) != '?' && line[i].charAt(0) != x[cislo][i].charAt(0)) {
                            dg1.remove(cislo);
                            continue;
                        } else if (line[i].charAt(2) != '?' && line[i].charAt(2) != x[cislo][i].charAt(2)) {
                            dg1.remove(cislo);
                            continue;
                        }
                    } else {
                        if (line[i].charAt(1) != '?' && line[i].charAt(1) != x[cislo][i].charAt(1)) {
                            dg1.remove(cislo);
                            continue;
                        }
                    }
                }
            }
        }
    }
    
    private static void printTime(int t) {
        System.out.printf("%d:%02d\n", (t / 60) , (t % 60));
    }
}
