Under construction

Still being built. Things may change

Cyclical Periods

Time limit1sMemory limit1024 MB

Summary
Given letter strings at sparse positions on a number line, find the letter whose periodic occurrences have the longest period.
Level

Easy3 of 10

Topics
Hash map, Math, Implementation
Solved
No attempts yet

Problem

The image illustrates the superposition of four cycles aa, bb, cc, and dd with different offsets and periods. aa appears every 3 steps with an offset of 3, bb every 4 steps with an offset of 2, cc every 7 steps with an offset of 5, and dd every 11 steps with an offset of 4. Cycle dd has the longest period.

Each position on the number line has a string of lowercase letters, and the letters in each string can be in any order. Given these strings and their positions, find the letter that represents the cycle with the longest period. The number line starts at 1 and is at most 65,536 long. Every letter that appears shows up at least twice, and it appears at every position on the number line that matches its offset and period.

Input

The first line contains an integer NN (2≤N≤65,5362 \leq N \leq 65{,}536), the number of strings on the number line. Each following line describes a position on the number line that has at least one letter. Each such line starts with a positive integer PP (1≤P≤65,5361 \leq P \leq 65{,}536), the position, followed by a space and the string of lowercase letters at that position. No letter is repeated within a string, and the positions increase strictly from one line to the next.

Output

Print the single letter that represents the cycle with the longest length. If two cycles tie, choose the one that appears at the earliest position. If they still tie, choose the letter that appears first in the string where the cycle is first discovered.

Examples2

  1. Example 1

    Input
    18
    2 b
    3 a
    4 d
    5 c
    6 ab
    9 a
    10 b
    12 ac
    14 b
    15 ad
    18 ab
    19 c
    21 a
    22 b
    24 a
    26 bcd
    27 a
    30 ab
    
    Expected output
    d
    
  2. Example 2

    Input
    4
    1 pz
    2 d
    4 zp
    5 d
    
    Expected output
    z