Under construction

Still being built. Things may change

Expiration Date

Interview

Time limit1sMemory limit1024 MB

Summary
For each food's expiration date, try every valid year/month/day reading and print invalid, safe, or unsafe by comparing with today's date.
Level

Easy3 of 10

Topics
Implementation, Brute force
Solved
No attempts yet

Problem

Hanbyeol has lived alone for one year and is starting to worry whether the food in her refrigerator is still within its expiration date. However, she cannot tell the order of year, month, and day in the dates printed on the packages.

Date formats differ by country. There are three main formats.

  • Year/Month/Day: East Asian countries such as South Korea, Japan, China, Lithuania, Mongolia, Bhutan, and Hungary
  • Day/Month/Year: most other countries, including Greece, the Netherlands, Denmark, Germany, Russia, Vietnam, Egypt, and Italy
  • Month/Day/Year: some English-speaking countries such as the United States, Guam, and the Philippines

For example, the date 10/12/32 can be either

  • December 10, 2032 when read as Day/Month/Year
  • October 12, 2032 when read as Month/Day/Year

December 32 does not exist, so the date cannot be in Year/Month/Day format (for example, year 20102010, month 1212, day 3232).

Hanbyeol wants to know whether the food is safe under every format that reads the date as a valid date. Write a program that helps her decide.

Input

The first line contains integers YY, MM, and DD separated by spaces. Today is (2000+Y)(2000+Y) year MM month DD day. (0≤Y≤990 \le Y \le 99, and (2000+Y)(2000+Y) year MM month DD day is a valid date)

The next line contains the number of food items NN. (1≤N≤300 0001 \le N \le 300\,000)

Each of the next NN lines contains integers AA, BB, and CC separated by spaces, which is the expiration date of one food item. The expiration date is written in the form AA/BB/CC. (0≤A,B,C≤990 \le A, B, C \le 99)

All dates are between the years 20002000 and 20992099, and only the remainder of the year divided by 100100 is given.

Output

For each date, print one of the following on its own line.

  • invalid: the date is not valid under any format
  • safe: under every format that reads the date as a valid date, today is before or on the expiration date
  • unsafe: otherwise

Hint

11, 33, 55, 77, 88, 1010, and 1212 months each have 3131 days.

44, 66, 99, and 1111 months each have 3030 days.

February has 2929 days in a leap year and 2828 days otherwise. Since the years in this problem are between 20002000 and 20992099, every year divisible by 44 is a leap year.

Examples1

  1. Example 1

    Input
    22 11 26
    4
    22 11 30
    22 11 31
    22 12 1
    22 22 22
    
    Expected output
    safe
    safe
    unsafe
    invalid