Very crude beginning, but it works.

Unfortunately, Gmail quite smartly limits the length of a search string. The generated query exceeds that with the number of labels that I have.