// Arup Guha
// 1/30/2013
// Solution to 2011 Rocky Mountain Regional Problem H: Citizenship Application.

import java.util.*;

public class h {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		while (stdin.hasNext()) {

			// Get starting and landing dates.
			String a = stdin.next();
			String b = stdin.next();
			date start = new date(a);
			date land = new date(b);

			// Get trip dates.
			int numGaps = stdin.nextInt();
			date[] startTrip = new date[numGaps];
			date[] endTrip = new date[numGaps];

			// Read in all travel dates.
			for (int i=0; i<numGaps; i++) {
				a = stdin.next();
				b = stdin.next();
				startTrip[i] = new date(a);
				endTrip[i] = new date(b);
			}

			// Put in order.
			Arrays.sort(startTrip);
			Arrays.sort(endTrip);

			// Output the solution.
			System.out.println(solve(start, land, startTrip, endTrip));
		}
	}

	// Solve the problem.
	public static date solve(date start, date land, date[] startTrip, date[] endTrip) {

		boolean[] on = new boolean[36500];

		// This is when you got there, make these days true.
		for (int i=start.dayCount; i<on.length; i++)
			on[i] = true;

		// Fill in each trip as not being there.
		for (int i=0; i<startTrip.length; i++)
			for (int j=startTrip[i].dayCount; j<=endTrip[i].dayCount; j++)
				on[j] = false;

		// Count half and full days.
		int halfdays = 0, fulldays = 0;
		for (int i=0; i<1095; i++) {

			// Check this day.
			if (on[i] && i < land.dayCount)
				halfdays++;
			else if (on[i])
				fulldays++;
		}

		// Only 730 of these count.
		int countHalf = halfdays;
		if (halfdays > 730) countHalf = 730;

		// We get it right away!
		if (fulldays + countHalf/2 >= 1095)
			return new date(1095);

		// We get these days in our window additionally.
		for (int i=1095; i<1460; i++) {

			// Check this day.
			if (on[i] && i < land.dayCount)
				halfdays++;
			else if (on[i])
				fulldays++;

			// Adjust our half day count if necessary.
			countHalf = halfdays;
			if (halfdays > 730) countHalf = 730;

			// See if we got it.
			if (fulldays + countHalf/2 >= 1095)
				return new date(i+1);
		}

		// Go through the rest of the days, looking at 1460 day windows.
		for (int i=1460; i<on.length; i++) {

			// Add in the new day.
			if (on[i] && i < land.dayCount)
				halfdays++;
			else if (on[i])
				fulldays++;

			// Subtract out the day 1460 days ago.
			if (on[i-1460] && i-1460 < land.dayCount)
				halfdays--;
			else if (on[i-1460])
				fulldays--;

			// Adjust our half days.
			countHalf = halfdays;
			if (halfdays > 730) countHalf = 730;

			// Return if we got it.
			if (fulldays + countHalf/2 >= 1095)
				return new date(i+1);
		}

		// It should never get here.
		return new date(on.length-1);
	}
}

// Stores number of days after Jan 1, 1980.
class date implements Comparable<date> {

	final public static int START_MONTH = 1;
	final public static int START_DAY = 1;
	final public static int START_YEAR = 1980;
	final public static int[] REGYEAR = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	final public static int[] LEAPYEAR = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

	private int month;
	private int day;
	private int year;
	public int dayCount;
	public String str;

	// Store object from an input string.
	public date(String s) {
		StringTokenizer tok = new StringTokenizer(s,"/");
		month = Integer.parseInt(tok.nextToken());
		day = Integer.parseInt(tok.nextToken());
		year = Integer.parseInt(tok.nextToken());
		setCount(month, day, year);
		str = s;
	}

	// Create it from the day count.
	public date(int myDayCount) {

		dayCount = myDayCount;

		// Beginning start.
		year = START_YEAR;
		month = START_MONTH;
		day = START_DAY;

		int account = dayCount;

		// Get to the correct year.
		while (true) {

			// Subtract the right number of days.
			if (account >= 366 && year%4 == 0) {
				account -= 366;
				year++;
			}
			else if (account >= 365 && year%4 != 0) {
				account -= 365;
				year++;
			}
			else
				break;
		}

		// We are done.
		if (account == 0)
			return;

		// Current year is a leap year
		if (year%4 == 0) {

			// Find the right month.
			int i = 0;
			while (LEAPYEAR[i] <= account) i++;

			account -= LEAPYEAR[i-1];
			month = i;
			day = account+1;

		}
		else {

			// Find the right month.
			int i = 0;
			while (REGYEAR[i] <= account) i++;

			account -= REGYEAR[i-1];
			month = i;
			day = account+1;
		}

		str = month + "/" + day + "/" + year;
	}

	// Set our day count.
	public void setCount(int month, int day, int year) {

		dayCount = 0;

		// Full days in a year.
		dayCount = 365*(year - START_YEAR);

		// Add in leap days.
		dayCount += ((year - START_YEAR+ 3)/4);

		int monthDiff = month - START_MONTH;

		// Adequate leap year check for range.
		if (year%4 == 0)
			dayCount += LEAPYEAR[monthDiff];
		else
			dayCount += REGYEAR[monthDiff];

		// Now, just add days!
		dayCount += (day-1);
	}

	// This is what we want to print out.
	public String toString() {
		return str;
	}

	// Adequate for our sorting purposes, since no vacations overlap.
	public int compareTo(date other) {
		return this.dayCount - other.dayCount;
	}
}