How to merge AND queries

def intersect(p1, p2)
    answer = None
    while p1 != None and p2 != None:
        if docID(p1) == docID(p2):
            answer += docID(p1)

        p1 = next(p1)
        p2 = next(p2)

To do so efficiently we merge smaller terms first

Suppose we have the following counts

singapore := 1.4b uni := 3.7b national := 4.3b

and the query

singapore && uni && national

compute singapore and uni := 1.4b (max)

then compute national and the above := 1.4b (max)