Added query encoding and decoding function