This page is still under construction.

Parts of this page are still being built. What you see may change.

Coding Biorhythm

Time limit1sMemory limit512 MB

Summary
For each of N dates, compute a biorhythm value from the squared digit differences of the birth date and that date, then print the earliest date with the maximum value.
Level

Easy2 of 10

Topics
Implementation, Math, Brute force
Solved
No attempts yet

Problem

Have you heard of a theory called biorhythm? Biorhythm claims that three cycles in the human body, physical, emotional, and intellectual, appear in a certain pattern determined by the date of birth, and that the combination of these patterns causes differences in ability and work efficiency.

Juheon suspected that his own coding skill, which changed from day to day, might also be affected by biorhythm, so he conducted research for several years. As a result, he found that coding and biorhythm are closely related, and he decided to define a new concept called coding biorhythm. The value of coding biorhythm Juheon found is computed from the date of birth and a date as follows.

When the format of 'year' is Y(y)Y(y), 'month' is M(m)M(m), and 'day' is D(d)D(d), define your birthday as "YYYY:MM:DDYYYY : MM : DD" and the date you will code as "yyyy:mm:ddyyyy : mm : dd", and for convenience write them as "Y1Y2Y3Y4:M1M2:D1D2Y_1Y_2Y_3Y_4 : M_1M_2 : D_1D_2" and "y1y2y3y4:m1m2:d1d2y_1y_2y_3y_4 : m_1m_2 : d_1d_2". Then the value of coding biorhythm is obtained by the formula below.

∑i=14(Yi−yi)2×∑j=12(Mj−mj)2×∑k=12(Dk−dk)2\displaystyle \sum_{i=1}^4 {(Y_i-y_i})^2 \times \sum_{j=1}^2 {(M_j-m_j})^2 \times \sum_{k=1}^2 {(D_k-d_k})^2

For example, if your birthday is January 1, 2000 and the date you will code is October 30, 2021, the value of coding biorhythm is computed by the formula below.

Y1Y2Y3Y4:M1M2:D1D2=2000:01:01Y_1Y_2Y_3Y_4 : M_1M_2 : D_1D_2 = 2000 : 01 : 01

y1y2y3y4:m1m2:d1d2=2021:10:30y_1y_2y_3y_4 : m_1m_2 : d_1d_2 = 2021 : 10 : 30

Coding biorhythm ={(2−2)2+(0−0)2+(0−2)2+(0−1)2}×{(0−1)2+(1−0)2}×{(0−3)2+(1−0)2}= \{(2-2)^2+(0-0)^2+(0-2)^2+(0-1)^2\} \times \{(0-1)^2+(1-0)^2\} \times \{(0-3)^2+(1-0)^2\}

=5×2×10=100= 5 \times 2 \times 10 = 100

Given someone's date of birth and the dates of the N days on which that person will code, print the date on which that person's coding biorhythm is highest among those dates. If several dates have the highest value, print the earliest one among them.

Input

The first line gives the person's date of birth.

The second line gives the number of days N on which the person will code. ( 2 ≤ N ≤ 1000 )

From the third line, the next N lines give the dates on which the person will code. Each coding date is guaranteed to be after the date of birth, and no date is given more than once.

Every date is given in the form YYYYMMDDYYYYMMDD, and only dates that actually exist in the Gregorian calendar from January 1, 1000 to December 31, 9999 are given as input.

Output

Among the coding dates given as input, print the date on which the coding biorhythm is highest in the form YYYYMMDDYYYYMMDD.

If several days have the highest value, print the earliest date among them.

Examples2

  1. Example 1

    Input
    20000101
    3
    20211030
    20211031
    20131114
    
    Expected output
    20131114
    
  2. Example 2

    Input
    20301030
    2
    99991231
    20301225
    
    Expected output
    99991231