Instead of iterating over all queries for every item in idx, iterate through qs as the outermost and only for loop, adding each query to toc[q.title[0]] (and creating the list if needed).
idx
qs
for
toc[q.title[0]]