// Arup Guha
// 1/24/2024
// Solution to Kattis Problem: Breaking Bad
// https://open.kattis.com/problems/breakingbad
// This is also a solution for Spring 2024 COP 3503 RP4 to illustrate two coloring.

import java.util.*;
import java.io.*;

public class breakingbad2 {

	public static int n;
	public static HashMap<String,Integer> stuffMap;
	public static String[] stuff;
	public static ArrayList<Integer>[] g;

	public static void main(String[] args) throws Exception {
		
		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		
		// Read ingredients. Store both ways.
		stuffMap = new HashMap<String,Integer>();
		stuff = new String[n];
		for (int i=0; i<n; i++) {
			stuff[i] = stdin.next();
			stuffMap.put(stuff[i], i);
		}
		
		// Set up graph.
		g = new ArrayList[n];
		for (int i=0; i<n; i++)
			g[i] = new ArrayList<Integer>();
		
		// Add edges.
		int numE = stdin.nextInt();
		for (int i=0; i<numE; i++) {
			int u = stuffMap.get(stdin.next());
			int v = stuffMap.get(stdin.next());
			g[u].add(v);
			g[v].add(u);
		}
		
		// Will store colors 0 = unassigned, 1 = red, 2 = blue
		int[] color = new int[n];
		boolean canDo = twocolor(color);
		
		// We can't do it.
		if (!canDo)
			System.out.println("impossible");
		
		// Output result.
		else {
		
			// Print first.
			boolean addSp = false;
			for (int i=0; i<n; i++) {
				if (color[i] == 1) {
					if (addSp) System.out.print(" ");
					addSp = true;
					System.out.print(stuff[i]);
				}	
			}
			System.out.println();
			
			// Print second
			addSp = false;
			for (int i=0; i<n; i++) {
				if (color[i] == 2) {
					if (addSp) System.out.print(" ");
					addSp = true;
					System.out.print(stuff[i]);
				}	
			}
			System.out.println();
		}
	}
	
	// Wrapper function for dfs.
	public static boolean twocolor(int[] color) {
		
		// Go to each node.
		for (int i=0; i<n; i++) {
		
			// This one's been colored.
			if (color[i] != 0) continue;
			
			// Start coloring this component.
			boolean res = dfs(color, i, i%2+1);
			
			// Oops, it didn't work.
			if (!res) return false;
		}
		
		// Ta da!
		return true;
	}
	
	public static boolean dfs(int[] color, int v, int value) {
	
		// Color this node.
		color[v] = value;
		
		// Check if this violated something.
		for (Integer next: g[v]) {
			if (color[next] == color[v])
				return false;
		}
		
		// Now recursively color all uncolored neighbors with the opposite color.
		for (Integer next: g[v]) {
			if (color[next] == 0) {
				boolean res = dfs(color, next, 3-value);
				if (!res) return false;
			}
		}
		
		// Good if we get here.
		return true;
	}
}
