import java.io.*;
import java.util.*;

public class flight_pk_wa3 {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;

	public String nextToken() throws IOException {
		while ((st == null) || (!st.hasMoreTokens()))
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	class InFlight implements Comparable<InFlight> {
		int sTime, fTime;
		int cost;
		String name;
		
		public InFlight(String name, int sTime, int fTime, int cost) {
			super();
			this.sTime = sTime;
			this.fTime = fTime;
			this.cost = cost;
			this.name = name;
		}
		
		public int compareTo(InFlight a) {
			return fTime - a.fTime;
		}
	}
	
	void check(String s) {
		for (int i = 0; i < s.length(); i++) {
			assert(('A' <= s.charAt(i)) && (s.charAt(i) <= 'Z') || ('0' <= s.charAt(i)) && (s.charAt(i) <= '9'));
		}
	}
	
	class OutFlight implements Comparable<OutFlight> {
		String name;
		int sTime, fTime;
		int cost;
		
		public OutFlight(String name, int sTime, int fTime, int cost) {
			super();
			this.sTime = sTime;
			this.fTime = fTime;
			assert((1 <= cost) && (cost <= 1000000));
			this.cost = cost;
			this.name = name;
			check(name);
		}
		
		public int compareTo(OutFlight a) {
			return sTime - a.sTime;
		}
	}
	
	public int parseTime(String s) {
		StringTokenizer z = new StringTokenizer(s, ":");
		
		String o = z.nextToken();
		assert(o.length() == 2);
		int a = Integer.parseInt(o);
		assert((0 <= a) && (a <= 23));
		o = z.nextToken();
		assert(o.length() == 2);
		int b = Integer.parseInt(o);
		assert((0 <= b) && (b <= 59));
		o = z.nextToken();
		assert(o.length() == 2);
		int c = Integer.parseInt(o);
		assert((0 <= c) && (c <= 59));
		
		
		return a * 3600 + b * 60 + c;
	}
	
	
	public void solve() throws IOException {
		int n = nextInt();
		assert((1 <= n) && (n <= 100000));
		InFlight[] a = new InFlight[n];
		for (int i = 0; i < n; i++) {
			a[i] = new InFlight(nextToken(), parseTime(nextToken()), parseTime(nextToken()), nextInt());
		}
		int m = nextInt();
		OutFlight[] b = new OutFlight[m];
		for (int i = 0; i < m; i++) {
			b[i] = new OutFlight(nextToken(), parseTime(nextToken()), parseTime(nextToken()), nextInt());
		}
		Arrays.sort(a);
		Arrays.sort(b);
		OutFlight[] min = new OutFlight[m];
		OutFlight tMin = b[m - 1];
		for (int i = m - 1; i >= 0; i--) {
			if ((b[i].cost <= tMin.cost))
				tMin = b[i];
			min[i] = tMin;
		}
		int t = 0;
		InFlight tF = null;
		OutFlight tS = null;
		for (int i = 0; i < n; i++) {
			while ((t < m) && (b[t].sTime < a[i].fTime + 900))
				t++;
			if (t == m)
				continue;
			if ((tF == null) || (tF.cost + tS.cost > a[i].cost + min[t].cost) || ((tF.cost + tS.cost == a[i].cost + min[t].cost) && (min[t].fTime < tS.fTime))) {
				tF = a[i];
				tS = min[t];
			}
		}
		out.println(tF.name);
		out.println(tS.name);
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			br = new BufferedReader(new FileReader("flight.in"));
			out = new PrintWriter("flight.out");

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new flight_pk_wa3().run();
	}
}