code: func mergeInvQueries(qs []invQuery, limit int, out []*Invocation) ([]*Invocation, bool, error) {
  	maxLen := len(out) + limit
  
  	for {
  		// Find the smallest invocation from heads of all queries. Do it even if
  		// already reached the limit, to check whether there are more items left.
  		var smallest *Invocation
  		for _, q := range qs {
  			inv, err := q.peek()
  			if err != nil {
  				return nil, false, err
  			}
  			if inv == nil {
  				continue // exhausted results of this query
  			}
  			if smallest == nil || inv.ID < smallest.ID {
  				smallest = inv
  			}
  		}
  
  		switch {
  		case smallest == nil:
  			return out, true, nil // exhausted results of all queries
  		case len(out) == maxLen:
  			return out, false, nil // actually filled up to the limit
  		}
  		out = append(out, smallest)
  
  		// There may be duplicates in the queries, so need to pop the consumed
  		// invocation from all queries.
  		for _, q := range qs {
  			for {
  				inv, err := q.peek()
  				if err != nil {
  					return nil, false, err
  				}
  				if inv == nil || inv.ID > smallest.ID {
  					break // found something larger at the head
  				}
  				if err := q.advance(); err != nil {
  					return nil, false, err
  				}
  			}
  		}
  	}
  }
docs: // mergeInvQueries merges results of multiple queries together.
  //
  // It picks smallest IDs first. In presence of duplicates, it favors queries
  // that are listed in 'qs' earlier.
  //
  // Places the results into 'out' slice, returning the extended slice at the end.
  //
  // Returns (..., true, nil) in case results of all queries has been exhausted,
  // and (..., false, nil) if it hit the limit on number of results.