Fix encoding of queries containing reference.