Problem:
Problem A: Era Name

As many of you know, we have two major calendar systems used in Japan today. One of them
is Gregorian calendar which is widely used across the world. It is also known as “Western
calendar” in Japan.

The other calendar system is era-based calendar, or so-called “Japanese calendar.” This system
comes from ancient Chinese systems. Recently in Japan it has been a common way to associate
dates with the Emperors. In the era-based system, we represent a year with an era name given
at the time a new Emperor assumes the throne. If the era name is “A”, the first regnal year
will be “A 1”, the second year will be “A 2”, and so forth.

Since we have two different calendar systems, it is often needed to convert the date in one
calendar system to the other. In this problem, you are asked to write a program that converts
western year to era-based year, given a database that contains association between several
western years and era-based years.

For the simplicity, you can assume the following:

A new era always begins on January 1st of the corresponding Gregorian year.

The first year of an era is described as 1.

There is no year in which more than one era switch takes place.

Please note that, however, the database you will see may be incomplete. In other words, some
era that existed in the history may be missing from your data. So you will also have to detect
the cases where you cannot determine exactly which era the given year belongs to.

Input

The input contains multiple test cases. Each test case has the following format:

N Q

EraName1 EraBasedYear1 WesternYear1

.

.

.

EraNameN EraBasedYearN WesternYearN

Query1
.

.

.

QueryQ

The first line of the input contains two positive integers N and Q (1 ≤ N ≤ 1000, 1 ≤ Q ≤ 1000).
N is the number of database entries, and Q is the number of queries.

Each of the following N lines has three components: era name, era-based year number and the
corresponding western year (1 ≤ EraBasedYeari ≤ WesternYeari ≤ 109 ). Each of era names
consist of at most 16 Roman alphabet characters. Then the last Q lines of the input specifies
queries (1 ≤ Queryi ≤ 109 ), each of which is a western year to compute era-based representation.

The end of input is indicated by a line containing two zeros. This line is not part of any dataset
and hence should not be processed.

You can assume that all the western year in the input is positive integers, and that there is no
two entries that share the same era name.

Output

For each query, output in a line the era name and the era-based year number corresponding to
the western year given, separated with a single whitespace. In case you cannot determine the
era, output “Unknown” without quotes.

Sample Input

4 3
meiji 10 1877
taisho 6 1917
showa 62 1987
heisei 22 2010
1868
1917
1988
1 1
universalcentury 123 2168
2010
0 0

Output for the Sample Input

meiji 1
taisho 6
Unknown
Unknown

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
internal class Data
{
    internal List<List<string>> Era { get; set; }
    internal List<int> Year { get; set; }
}
public class Pg
{
    static void Main()
    {
        var rd = new Reader();
        var op = new Operate();
        var lst = new List<string>();
        var dt = rd.Read().ToList();
        dt.ForEach(x => x.Year.ForEach(yr => Console.WriteLine(op.YearConverter(yr, x.Era))));
    }
}
class Operate
{
    internal string YearConverter(int yr, IEnumerable<List<string>> Era)
    {
        var Er = "";
        var tmp = 0;
        foreach (var v in Era)
        {
            tmp = 0;
            var delta = 0;
            var n1 = int.Parse(v[1]);
            var n2 = int.Parse(v[2]);
            delta = n2 - yr;
            Er = v[0];
            if (n2 >= yr && delta < n1 && n1 - delta > 0)
            {
                tmp = n1 - delta;
                break;
            }
        }
        return tmp != 0 ? Er + " " + tmp : "Unknown";
    }
}
class Reader
{
    internal IEnumerable<Data> Read()
    {
        int[] n;
        while (!(n = Console.ReadLine().Split().Select(int.Parse).ToArray()).All(x => x == 0))
        {
            var dt = new Data { Era = new List<List<string>>(), Year = new List<int>() };
            while (n[0]-- > 0)
                dt.Era.Add(Console.ReadLine().Split().ToList());
            while (n[1]-- > 0)
                dt.Year.Add(int.Parse(Console.ReadLine()));
            yield return dt;
        }
    }
}