#include <iostream>
#include <stdio.h>
#include <string.h>
#include <list>
#define uint unsigned int
using namespace std;
class CACHELINE
{
public:
uint content;
uint time;
CACHELINE()
{
content = 0;
time = 0;
}
};
int main()
{
int N;
int round = 1;
while(true)
{
cin >> N;
if(N == 0)
{
break;
}
list<CACHELINE*> caches;
printf("Simulation %d\n", round++);
string cmds;
char *cmd;
cin >> cmds;
cmd = (char*)cmds.c_str();
//printf("%s\n", cmd);
int length = strlen(cmd);
uint time = 0;
for(int i = 0; i < length; i++)
{
// if cmd[i] == '!' simulate!
if(cmd[i] == '!')
{
for(auto iter = caches.begin(); iter != caches.end(); ++iter)
{
CACHELINE* cache = *iter;
printf("%c", cache->content);
}
printf("\n");
// processing
time++;
continue;
}
// Find the duplicated slot
for(auto iter = caches.begin(); iter != caches.end(); ++iter)
{
CACHELINE* cache = *iter;
if(cache->content == cmd[i])
{
caches.erase(iter);
break;
}
}
// Push cmd until full
if(caches.size() < N)
{
CACHELINE* cache = new CACHELINE;
cache->content = cmd[i];
cache->time = time;
caches.push_back(cache);
}
// Else evict the oldest one
else
{
uint mtime = 0xFFFF;
auto idx = caches.begin();
for(auto iter = caches.begin(); iter != caches.end(); ++iter)
{
CACHELINE* cache = *iter;
if(mtime > cache->time)
{
mtime = cache->time;
idx = iter;
}
}
caches.erase(idx);
CACHELINE* cache = new CACHELINE;
cache->content = cmd[i];
cache->time = time;
caches.push_back(cache);
}
}
// Erase all elements in list
for(auto iter = caches.begin(); iter != caches.end();)
{
CACHELINE* cache = *iter;
delete cache;
iter = caches.erase(iter);
}
}
}