added algorithm for query pairings of sheet3