Jwang refactor query encoding