fix infinite loop bug in Query.cpp