Go: package main

import (
	"fmt"
	"sort"
)

type Datum struct {
	EraName      string
	EraBasedYear int64
	WesternYear  int64
}

const Unknown = "Unknown"

func lowerBound(data []*Datum, westernYearBorder int64) int {
	imin := -1
	imax := len(data)
	for imax-imin > 1 {
		imid := (imax + imin) / 2
		if data[imid].WesternYear >= westernYearBorder {
			imax = imid
		} else {
			imin = imid
		}
	}
	return imax
}

func solve(n, q int) {
	data := make([]*Datum, 0, n)
	for index := 0; index < n; index++ {
		datum := Datum{}
		fmt.Scan(&datum.EraName, &datum.EraBasedYear, &datum.WesternYear)
		data = append(data, &datum)
	}
	sort.Slice(data, func(i, j int) bool {
		return data[i].WesternYear < data[j].WesternYear
	})
	for cnt := 0; cnt < q; cnt++ {
		var query int64
		fmt.Scan(&query)
		var index int
		if index = lowerBound(data, query); index == n {
			fmt.Println(Unknown)
			continue
		}
		possibleDatum := data[index]
		if possibleDatum.WesternYear-query > possibleDatum.EraBasedYear-1 {
			fmt.Println(Unknown)
			continue
		}
		ans := possibleDatum.EraBasedYear - (possibleDatum.WesternYear - query)
		fmt.Println(possibleDatum.EraName, ans)
	}
}

func main() {
	for {
		var n, q int
		if fmt.Scan(&n, &q); n == 0 && q == 0 {
			break
		}
		solve(n, q)
	}
}

C++: #include <iostream>

using namespace std;

const int MAX_N = 1000 + 5;
int era_y[MAX_N], wes_y[MAX_N];
string name[MAX_N];

int main()
{
    int N, Q;

    while (cin >> N >> Q, N || Q) {
        for (int i = 0; i < N; i++) 
            cin >> name[i] >> era_y[i] >> wes_y[i];

        for (int i = 0; i < Q; i++) {
            int q;
            int lb = -1, ub = N;

            cin >> q;
            bool flag = false;

            while (ub - lb > 1) {
                int mid = (lb + ub) / 2;
                if (wes_y[mid] - era_y[mid] + 1 <= q && q <= wes_y[mid]) {
                    flag = true;
                    cout << name[mid] << " " << q - (wes_y[mid] - era_y[mid]) << endl;
                    break;
                }
                else if (wes_y[mid] - era_y[mid] + 1 > q)
                    ub = mid;
                else
                    lb = mid;
            }

            // for (int j = 0; j < N; j++) {
            //     if (wes_y[j] - era_y[j] + 1 <= q && q <= wes_y[j]) {
            //         flag = true;
            //         cout << name[j] << " " << q - (wes_y[j] - era_y[j]) << endl;
            //         break;
            //     }
            // }

            if (!flag) {
                cout << "Unknown" << endl;
            }
        }
    }

    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<ll, ll> P;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  while(cin>>N>>Q,N||Q) {
    map<P, string> MAP;
    FOR(i,0,N) {
      string s;
      ll a, b;
      cin >> s >> a >> b;
      MAP[P(a, b)] = s;
    }
    FOR(i,0,Q) {
      ll in; cin >> in;
      bool ok = false;
      ll year = 1e15;
      string era;
      for(auto m : MAP) {
        ll end = m.first.second;
        ll start = end - m.first.first + 1;
        if(in >= start && year >= in - start + 1) {
          year = in - start + 1;
          era = m.second;
          ok = true;
          if(year > m.first.first) {
            ok = false;
            break;
          }
        }
      }
      if(ok) {
        cout << era << " " << year << endl;
      } else {
        puts("Unknown");
      }
    }
  }
  return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;

string name[1001];
int eby[1001], wy[1001];

int main(){
	name[0] = "Unknown";
	int n, q;
	while(cin >> n >> q, n){
		for(int i = 0; i < n; ++i){
			cin >> name[i] >> eby[i] >> wy[i];
		}
		for(int i = 0; i < q; ++i){
			int ans = -1, x;
			cin >> x;
			for(int j = 0; j < n; ++j){
				if(x <= wy[j] && x > wy[j] - eby[j]){
					ans = j;
					break;
				}
			}
			if(ans == -1){
				cout << "Unknown\n";
			}
			else{
				cout << name[ans] << ' ' << x - wy[ans] + eby[ans] << '\n';
			}
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;

int main() {
    int N, Q;
    while(cin >> N >> Q, N) {
        vector<tuple<string, int, int>> v(N);
        for(int i=0; i<N; ++i) {
            string name;
            int eby, wy;
            cin >> name >> eby >> wy;
            v[i] = make_tuple(name, wy - eby + 1, wy);
        }
        sort(v.begin(), v.end(), [](auto const& t1, auto const& t2) {
            return get<1>(t1) < get<1>(t2);
        });

        for(int i=0; i<Q; ++i) {
            int year;
            cin >> year;
            bool finded = false;
            for(int i=0; i<v.size(); ++i) {
                if(get<1>(v[i]) <= year && year <= get<2>(v[i])) {
                    cout << get<0>(v[i]) << " " << year - get<1>(v[i]) + 1 << endl;
                    finded = true;
                    break;
                }
            }
            if(!finded) {
                cout << "Unknown" << endl;
            }
        }
    }
}

C++: #include <iostream>
#include <string>
using namespace std;

int main() {
	int n;
	string input;
	int T = 1, N = 2, W = 3;
	int tmp;

	while(cin >> n, n != 0) {
		T = 1, N = 2, W = 3;
		for(int i = 0; i < n; i++) {
			cin >> input;

			if(input == "north") {
				tmp = N;
				N = T;
				T = 7 - tmp;
			}
			else if(input == "west") {
				tmp = W;
				W = T;
				T = 7 - tmp;
			}
			else if(input == "south") {
				tmp = T;
				T = N;
				N = 7 - tmp;
			}
			else {
				tmp = T;
				T = W;
				W = 7 - tmp;
			}
		}

		cout << T << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;

struct Y{
    string name;
    int start,end;
    Y(string name,int start,int end) : name(name),start(start),end(end) {};
};

int main(){
    while(true){
        int N,Q;
        cin >> N >> Q;

        if(N == 0 and Q == 0) break;
        vector<Y> years;
        for(int i=0;i<N;i++){
            string name;
            int start,end,d;
            cin >> name >> d >> end;
            start = end-d + 1;
            years.push_back(Y(name,start,end));
        }
        for(int i=0;i<Q;i++){
            int year;
            cin >> year;
            for(int j=0;j<years.size();j++){
                if(years[j].start <= year and year <= years[j].end){
                    cout << years[j].name << " " << year-years[j].start+1 << endl;
                    goto end;
                }
            }
            cout << "Unknown" << endl;
        end:
            ;
        }
    }
    return 0;
}