package utils;

import java.util.*;
import java.math.*;

public class Queue extends TreeSet {

  public Queue() { super(new QComparator()); }

  // puts the object at the end of the Q
  public void push(Object o) {
    BigInteger rank = BigInteger.ZERO;
    if(!this.isEmpty()) {
      BigInteger lastRank = (BigInteger)((Pair)this.last()).car();
      rank = lastRank.add(BigInteger.ONE);
    }
    this.add(new Pair(rank,o));
  }
  // pulls the first object off the Q
  public Object pop() {
    Object first = this.first();
    this.remove(first);
    return ((Pair)first).cdr();
  }
}

class QComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    return ((BigInteger)((Pair)o1).car()).compareTo(((Pair)o2).car());
  }
  public boolean equals(Object obj) {
    return (obj instanceof QComparator);
  }
}
