Added the facilities for efficient bi-directional lookup.

At the expense of some extra storage of course. But this should be
more than compensated by avoiding storing duplicate queries.