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

class Cube{
public:
  int id;
  Cube* upper;
  Cube* lower;
  Cube(int id){
    this->id = id;
    upper = NULL;
    lower = NULL;
  }
  Cube(const Cube &c){
    this->id = c.id;
    this->upper = c.upper;
    this->lower = c.lower;
  }
  bool operator == (const Cube &c) const{
    return id == c.id;
  }
};

bool isSatisfied(Cube &low, Cube &high){
  Cube* c = &low;
  while(1){
    if(*c == high) return true;
    if(c->upper == NULL) return false;
    c = c->upper;
  }
  /*
  Cube c = Cube(low);
  while(1){
    if(c == high) return true;
    if(c.upper == NULL) return false;
    c = Cube(*c.upper);
  }
  */
}

void putOff(Cube &off){
  Cube* c = &off;
  if(c->lower != NULL){
    c->lower->upper = NULL;
    c->lower = NULL;
  }
  while(c->upper != NULL){
    c = c->upper;
    c->lower->upper = NULL;
    c->lower = NULL;
  }
  /*
  Cube c = Cube(off);
  while(c.upper == NULL){
    c = Cube(*c.upper);
    (*c.lower).upper = NULL;
      c.lower = NULL;
  }
  */
}

void putOn(Cube &low, Cube &high){
  assert(high.upper == NULL);
  assert(high.lower == NULL);
  Cube *c = &low;
  while(c->upper != NULL){
    c = c->upper;
  }
  c->upper = &high;
  c->upper->lower = c;

}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m;
  while(cin >> m, m){
    vector<Cube> cubes;
    for(int i = 0; i < m; i++){
      cubes.push_back(Cube(i+1));
    }
    int I, J;
    while(cin >> I >> J, I | J){
      if(J == 0){
        I--;
        if(cubes[I].lower == NULL) continue;
        putOff(cubes[I]);
        continue;
      }
      I--; J--;
      if(isSatisfied(cubes[J], cubes[I])) continue;
      putOff(cubes[I]);
      putOn(cubes[J], cubes[I]);
    }
    vector<int> ans;
    for(size_t i = 0; i < cubes.size(); i++){
      if(cubes[i].lower == NULL){
        int cnt = 1;
        Cube *c = &cubes[i];
        while(c->upper != NULL){
          cnt++;
          c = c->upper;
        }
      ans.push_back(cnt);
      }
    }
    sort(ans.begin(), ans.end());
    for(size_t i = 0; i < ans.size(); i++){
      cout << ans[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}