// 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 breakingbad {

	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 {
		
		FastScanner stdin = new FastScanner(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 = -1, v = -1;
			String ing1 = stdin.next();
			String ing2 = stdin.next();
			
			// Find ingredient numbers.
			if (stuffMap.containsKey(ing1)) u = stuffMap.get(ing1);
			if (stuffMap.containsKey(ing2)) v = stuffMap.get(ing2);
			
			// Only add edge if safe.
			if (u>=0 && v>=0) {
				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 {
		
			StringBuffer sb1 = new StringBuffer();
			boolean addSp = false;
			
			// Build first list.
			for (int i=0; i<n; i++) {
				if (color[i] == 1) {
					if (addSp) sb1.append(" ");
					addSp = true;
					sb1.append(stuff[i]);
				}	
			}
			
			// Build second list.
			StringBuffer sb2 = new StringBuffer();
			addSp = false;
			for (int i=0; i<n; i++) {
				if (color[i] == 2) {
					if (addSp) sb2.append(" ");
					addSp = true;
					sb2.append(stuff[i]);
				}	
			}
			
			// Ta da!
			System.out.println(sb1);
			System.out.println(sb2);
		}
	}
	
	// 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;
	}
}

class FastScanner {
    BufferedReader br;
    StringTokenizer st;
	
    public FastScanner(InputStream i) {
        br = new BufferedReader(new InputStreamReader(i));
        st = new StringTokenizer("");
    }
			
    public String next() throws IOException {
        if(st.hasMoreTokens())
            return st.nextToken();
        else
            st = new StringTokenizer(br.readLine());
        return next();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    //#
    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
    //$
}