optimized serial version with many test cases