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.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class P : IComparable
{
    public int to { get; set; }
    public int from { get; set; }
    public string era { get; set; }
    public int CompareTo(object obj)
    {
        var x = (P)obj;
        if (this.from > x.from) return 1;
        else if (this.from == x.from) return 0;
        else return -1;
    }
}


public class hello
{
    public static void Main()
    {
        while (true)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            var n = int.Parse(line[0]);
            var q = int.Parse(line[1]);
            if (n == 0 && q == 0) break;
            var ps = new P[n];
            for (int i = 0; i < n; i++)
            {
                line = Console.ReadLine().Trim().Split(' ');
                var a = int.Parse(line[1]);
                var b = int.Parse(line[2]);
                ps[i] = new P { from = b - a + 1, to = b, era = line[0] };
            }
            ps = ps.OrderBy(x => x.from).ToArray();
            getAns(n, q, ps);
        }
    }
    static void getAns(int n, int q, P[] ps)
    {
        var sb = new StringBuilder();
        var w2 = new P();
        for (int i = 0; i < q; i++)
        {
            var w = int.Parse(Console.ReadLine().Trim());
            w2.from = w;
            var t = LowerBound<P>(ps, w2);
            if (t >= n)
            {
                if (w > ps[n - 1].to) sb.Append("Unknown\n");
                else sb.Append(string.Format(   "{0} {1}\n", ps[n - 1].era, w - ps[n - 1].from + 1));
            }
            else if (t == 0)
            {
                if (w == ps[0].from) sb.Append(string.Format("{0} {1}\n", ps[0].era, w - ps[0].from + 1));
                else sb.Append("Unknown\n");
            }
            else
            {
                if (ps[t].from == w) { sb.Append(string.Format("{0} 1\n", ps[t].era)); }
                else if (ps[t - 1].from < w)
                {
                    if (ps[t - 1].to >= w) { sb.Append(string.Format("{0} {1}\n", ps[t - 1].era, w - ps[t - 1].from + 1)); }
                    else sb.Append("Unknown\n");
                }
            }
        }
        Console.Write(sb);
    }
    public static int LowerBound<T>(T[] arr, int start, int end, T value, IComparer<T> comparer)
    {
        int low = start;
        int high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) < 0)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    public static int LowerBound<T>(T[] arr, T value) where T : IComparable
    {
        return LowerBound(arr, 0, arr.Length, value, Comparer<T>.Default);
    }
}