#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <sstream>
#include <functional>

using namespace std;

#define F(a,b) for(int a=0;a<b;++a)
typedef long long LL;

typedef pair<int,int>P;
int main(){
	string s;
	stringstream ss;
	priority_queue<P,vector<P>,greater<P> >p;
	map<int,int> m;
	while(getline(cin,s)){
		ss << s;
		string a; ss >> a;
		if(a == "#") break;
		int b,c; ss >> b >> c;
		m[b] = c;
		p.push(P(c,b));
		ss.clear();
	}
	int t; cin >> t;
	while(t--){
		P now = p.top(); p.pop();
		cout << now.second << "\n";
		p.push(P(m[now.second] + now.first,now.second));
	}
}