/**
   * Counts how many elements in the priority queue match the element at the front of the queue,
   * which should be from the first bag.
   *
   * @param pq priority queue
   * @return number of matches
   */
  public int countMatches(PriorityQueue<Pair> pq) {
    Pair nextPair = pq.peek();
    Tuple data = nextPair.data;

    // sanity check
    if (!nextPair.index.equals(0)) {
      throw new RuntimeException("Expected next bag to have index 0");
    }

    int matches = 0;
    for (Pair p : pq) {
      if (data.equals(p.data)) matches++;
    }
    // subtract 1 since element matches itself
    return matches - 1;
  }