Here comes the solution to my problem:
I guess most of the time the simplest solution is the best!
I divided the problem in two parts. Now I first merge the catalogs and paths so every catalog exits only once in the dictionary.
And in the second step I can build the query sequentially :)