import java.util.*;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        try {
            int m = sc.nextInt();
            if (m <= 0) 
            {
                System.out.println("Invalid input");
                return;
            }
            HashMap<String, Integer> map1 = new HashMap<>();
            for (int i = 0; i < m; i++)
            {
                String key = sc.next();
                int value = sc.nextInt();
                map1.put(key, value);
            }
            int n = sc.nextInt();
            if (n <= 0) 
            {
                System.out.println("Invalid input");
                return;
            }
            HashMap<String, Integer> map2 = new HashMap<>();
            for (int i = 0; i < n; i++) 
            {
                String key = sc.next();
                int value = sc.nextInt();
                map2.put(key, value);
            }
            LinkedHashMap<String, Integer> intersection = new LinkedHashMap<>();
            for (String key : map1.keySet())
            {
                if (map2.containsKey(key) && map1.get(key).equals(map2.get(key)))
                {
                    intersection.put(key, map1.get(key));
                }
            }
            if (intersection.isEmpty()) 
            {
                System.out.println("-1");
            }
            else
            {
                for (Map.Entry<String, Integer> entry : intersection.entrySet())
                {
                    System.out.println(entry.getKey() + " " + entry.getValue());
                }
            }
        } 
        catch (Exception e) 
        {
            System.out.println("Invalid input");
}
}
}
