Each newborn calf on Farmer John's farm receives a brand new N-digit (1 ≤ N ≤ 100) serial number, a positive integer larger than any other cow's serial number on the farm. Obviously, the larger the serial number, the younger the cow.
Each cow becomes very fond of her number and often boasts about it. The cows are especially proud of serial numbers in which more than half (not just half!) of the digits are the same. For example, in the number 23522, the digit 2 appears in 3 out of the 5 digit places; the number 12342, though, has no digit that appears as a strict majority. Cows with serial numbers that contain a single majority digit are called 'special'; the rest of the cows are 'ordinary'.
Naturally, ordinary cows are quite envious of special cows. Sometimes in the past, they have even resorted to bullying special cows. Of course, given the growth rate of cows, a cow can only bully cows that are younger than it. In fact, an ordinary cow will bully the oldest special cow younger than she is. Unfortunately, cows have a tough time figuring out which cow that would be. Given a cow's serial number, determine which special cow she should bully. If the cow is already special, then output her own serial number.