import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main {

    public static void main(String[] args) {
        try {
            Scanner in = new Scanner(new File("mnemonic.in"));
            PrintWriter out = new PrintWriter(new File("mnemonic.out"));
            int n = Integer.parseInt(in.next());
            boolean nums[][] = new boolean[10][10];
            int countm[][] = new int[2][10];
            int countn[][] = new int[2][10];
            for (int i = 0; i <= n; i++) {
                nums[i / 10][i % 10] = true;
            }
            for (int i = 0; i < 10; i++) {
                countm[0][i] = countm[1][i] = 0;
                countn[0][i] = countn[1][i] = 0;
            }
            for (int i = 0; i < n; i++) {
                String s = in.next();
                if (s.contains("*")) {
                    if (s.compareTo("**") != 0) {
                        if (s.charAt(0) == '*') {
                            countm[0][(int) s.charAt(1) - (int) '0']++;
                        } else {
                            countm[1][(int) s.charAt(0) - (int) '0']++;
                        }
                    }
                } else {
                    nums[(int) s.charAt(0) - (int) '0'][(int) s.charAt(1) - (int) '0'] = false;
                }
            }
            for (int i = 0; i <= n; i++) {
                if (nums[i / 10][i % 10]) {
                    //System.out.print(i+" ");
                    countn[1][i / 10]++;
                    countn[0][i % 10]++;
                }
            }
            /*  for (int i = 0; i < 10; i++) {
            System.out.print(countm[0][i] + " " + countn[0][i] + " " + countm[1][i] + " " + countn[0][i] + "\n");
            }*/
            boolean b = false;
            do {
                b = false;
                for (int i = 0; i < 10; i++) {
                    if (countn[0][i] != 0 && countn[0][i] == countm[0][i]) {
                        b = true;
                        countn[0][i] = 0;
                        countm[0][i] = 0;
                        for (int j = 0; j < 10; j++) {
                            if (nums[j][i]) {
                                countn[1][j]--;
                                nums[j][i] = false;
                            }
                        }
                    }
                    if (countn[1][i] != 0 && countn[1][i] == countm[1][i]) {
                        b = true;
                        countn[1][i] = 0;
                        countm[1][i] = 0;
                        for (int j = 0; j < 10; j++) {
                            if (nums[i][j]) {
                                countn[0][j]--;
                                nums[i][j] = false;
                            }
                        }
                    }
                }
            } while (b);
            int i = 0;
            while (!nums[i / 10][i % 10]) {
                i++;
            }
            if (i < 10) {
                out.print(0);
            }
            out.print(i + " ");
            i = n;
            while (!nums[i / 10][i % 10]) {
                i--;
            }
            if (i < 10) {
                out.print(0);
            }
            out.print(i);
            in.close();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}