import java.util.StringTokenizer;

import ru.ifmo.testlib.*;
import ru.ifmo.testlib.Outcome.Type;

public class Check implements Checker {

	class Flight {
		int sTime, fTime;
		int cost;
		String name;
		
		public Flight(String name, int sTime, int fTime, int cost) {
			super();
			this.sTime = sTime;
			this.fTime = fTime;
			this.cost = cost;
			this.name = name;
		}
		
	}
	
	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'));
		}
	}
	
	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;
	}
	
	
	@Override
	public Outcome test(InStream inf, InStream ouf, InStream ans) {
		String[] names = new String[4];
		names[0] = ouf.nextToken();
		names[1] = ouf.nextToken();
		names[2] = ans.nextToken();
		names[3] = ans.nextToken();
		Flight[] f = new Flight[4];
		
		int n = inf.nextInt();
		for (int i = 0; i < n; i++) {
			Flight a = new Flight(inf.nextToken(), parseTime(inf.nextToken()), parseTime(inf.nextToken()), inf.nextInt());
			if (a.name.equals(names[0])) {
				f[0] = a;
			}
			if (a.name.equals(names[2])) {
				f[2] = a;
			}
		}
		
		n = inf.nextInt();
		for (int i = 0; i < n; i++) {
			Flight a = new Flight(inf.nextToken(), parseTime(inf.nextToken()), parseTime(inf.nextToken()), inf.nextInt());
			if (a.name.equals(names[1])) {
				f[1] = a;
			}
			if (a.name.equals(names[3])) {
				f[3] = a;
			}
		}
		
		if ((f[2] == null) || (f[3] == null)) {
			return new Outcome(Type.FAIL, "One of jury's flights hasn't been found");
		}
		
		if ((f[0] == null) || (f[1] == null)) {
			return new Outcome(Type.WA, "One of contestant's flights hasn't been found");
		}
		
		if (f[2].fTime > f[3].sTime - 900) {
			return new Outcome(Type.FAIL, "Jury's answer has less, than 15 minutes on docking");
		}
		
		if (f[0].fTime > f[1].sTime - 900) {
			return new Outcome(Type.WA, "Contestant's answer has less, than 15 minutes on docking");
		}
		
		if (f[0].cost + f[1].cost > f[2].cost + f[3].cost) {
			return new Outcome(Type.WA, "Contestant has spent " + (f[0].cost + f[1].cost) + " roubles, but jury has spent " + (f[2].cost + f[3].cost) + " roubles."); 
		}
		
		if (f[0].cost + f[1].cost < f[2].cost + f[3].cost) {
			return new Outcome(Type.FAIL, "Contestant has spent " + (f[0].cost + f[1].cost) + " roubles, but jury has spent " + (f[2].cost + f[3].cost) + " roubles."); 
		}
		
		if (f[1].fTime < f[3].fTime) {
			return new Outcome(Type.FAIL, "Contestant has arrived at " + f[1].fTime + " ,but jury has arrived at " + f[3].fTime);
		}
		
		if (f[1].fTime > f[3].fTime) {
			return new Outcome(Type.WA, "Contestant has arrived at " + f[1].fTime + " ,but jury has arrived at " + f[3].fTime);
		}
		
		return new Outcome(Type.OK, "Solution is correct");
		
	}
	
}
