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<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n, q;
	while (cin >> n >> q, n) {
		map<int, string>mp;
		map<string, int>m;
		rep(i, n) {
			string s;
			int j, k;
			cin >> s >> j >> k;
			mp[k - j] = s;
			m[s] = j;
		}
		rep(i, q) {
			int d; cin >> d;
			auto it = mp.lower_bound(d);
			it--;
			if (it == --mp.begin()) {
				cout << "Unknown" << endl;
				continue;
			}
			if (d - (*it).first > m[(*it).second]) {
				cout << "Unknown" << endl;
				continue;
			}
			cout << (*it).second << ' ' << d - (*it).first << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
int main()
{
	for(std::size_t N, Q;;){
		std::cin >> N>> Q;
		if(N == 0 and Q == 0){break;}
		std::map<std::string, std::pair<std::size_t,std::size_t>> mp;
		for(std::size_t i = 0,g,w; i < N; i++){
			std::string s;
			std::cin >> s >> w >>g, mp[s] = {g-w, g};
		}
		for(std::size_t q = 0,y; q < Q; q++){
			std::cin >> y;
			std::string ans = "Unknown";
			for(const auto p:mp){
				const std::size_t d = p.second.first, l = p.second.second;
				if(y > d and y <= l){
					ans = p.first + " " + std::to_string(y-d);
					break;
				}
			}
			std::cout << ans << std::endl;
		}
	}	
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <sstream>

using namespace std;

#define rep(i,n) for(int i = 0;i < n;i++)

int main() {
	int n, q;
	while (cin >> n >> q && n) {
		map<string, pair<int, int>> tb;
		rep(i, n) {
			string s;
			int a, end;
			cin >> s >> a >> end;
			int start = end - a + 1;
			if (tb.count(s)) {
				tb[s].first = min(tb[s].first, start);
				tb[s].second = max(tb[s].first, end);
			}
			else {
				tb[s].first = start;
				tb[s].second = end;
			}
		}
		rep(i, q) {
			int t;
			cin >> t;
			string out = "Unknown";
			for (auto i : tb) {
				if (i.second.first <= t && t <= i.second.second) {
					stringstream ss;
					ss << i.first << " " << t - i.second.first + 1;
					out = ss.str();
				}
			}
			cout << out << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<long long, long long> P;
typedef pair<P, string> PP;

void solve(int n, int q){
	vector<PP> v;
	for(int i=0; i<n; ++i){
		string s;
		int a, b;
		cin >> s >> a >> b;
		v.push_back(PP(P(b-a+1, b), s));
	}
	sort(v.begin(), v.end());
	for(int i=0; i<q; ++i){
		int a;
		cin >> a;
		for(int j=0; j<v.size(); ++j){
			if(j == 0 && a < v[j].first.first){
				cout << "Unknown\n";
				break;
			}
			if(v[j].first.first <= a && a <= v[j].first.second){
				cout << v[j].second << " " << a-v[j].first.first+1 << "\n";
				break;
			}else if(v[j].first.second < a && (j==v.size()-1 || a < v[j+1].first.first)){
				cout << "Unknown\n";
				break;
			}
		}
	}
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	while(1){
		int n, q;
		cin >> n >> q;
		if(n == 0 && q == 0) return 0;
		solve(n, q);
	}
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int N=10006, INF=0x3fffffff;

int dis[N], cost[N], pre[N];
bool vs[N], n, m;
struct data {
    int u, dis, cost;
    data() {}
    data(int _u, int _dis, int _cost) : u(_u), dis(_dis), cost(_cost) {}
};
vector <data> arc[N];
queue <int> Q;

void BFS() {
    while(!Q.empty()) Q.pop();
    for(int i=1; i<=n; i++) dis[i]=cost[i]=INF;
    memset(vs, 0, sizeof vs);
    memset(pre, -1, sizeof pre);
    cost[1]=dis[1]=0, vs[1]=1, Q.push(1);
    while(!Q.empty()) {
        int cur=Q.front();
        vs[cur]=0, Q.pop();
        int len=(int)arc[cur].size();
        for(int i=0; i<len; i++) {
            int u=arc[cur][i].u;
            int DIS=arc[cur][i].dis;
            int COST=arc[cur][i].cost;
            if(dis[u]>dis[cur]+DIS || dis[u]==dis[cur]+DIS && cost[u]>cost[cur]+COST) {
                pre[u]=cur;
                dis[u]=dis[cur]+DIS;
                cost[u]=cost[cur]+COST;
                if(!vs[u]) Q.push(u), vs[u]=1;
            }
        }
    }
}

int main() {
    freopen("/home/lovezzt/桌面/in.txt", "r", stdin);
    while(scanf("%d%d", &n, &m), n!=0 || m!=0) {
        for(int i=1; i<=n; i++) arc[i].clear();
        for(int i=0, a, b, c, d; i<m; i++) {
            scanf("%d%d%d%d", &a, &b, &c, &d);
            arc[a].push_back(data(b, c, d));
            arc[b].push_back(data(a, c, d));
        }
        BFS();
        int sum=0;
        memset(vs, 0, sizeof vs);
        for(int i=1; i<=n; i++) if(pre[i]!=-1) sum+=cost[i]-cost[pre[i]];
        printf("%d\n", sum);
    }
    return 0;
}