template< class T ,
class Sequence=vector<T> ,
class Compare=less<typename Sequence::value_type> >
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct node {
int no;
int year;
int period;
int current;
};
int num;
struct cmp {
bool operator() (const node &a, const node &b)
{
if (a.current < b.current)
return false;
else if (a.current == b.current)
return (a.year > b.year);
else return true;
}
};
priority_queue<node, vector<node>, cmp> p;
void slove()
{
while (num--)
{
node e = p.top();
p.pop();
printf("%d/n", e.year);
e.current += e.period;
p.push(e);
}
}
int main()
{
node a;
char s[100];
for (int i=0; scanf("%s", s) && strcmp(s, "#"); i++)
{
scanf("%d%d", &a.year, &a.period);
a.current = a.period;
a.no = i;
p.push(a);
}
scanf("%d", &num);
slove();
return 0;
}