With https://gerrit.wikimedia.org/r/#/c/379141/ unused images will be sectioned by source page then ordered by monument id.
For sparql harvests this makes less sense since there will (mostly) be one source per id (i.e. one Wikidata object per id).
For these it might make more sense to skip the sections but still make a new gallery for each id. I.e.
<gallery> File:Foo|[[d:Q123|1]] File:Foo|[[d:Q123|1]] </gallery> <gallery> File:Foo|[[d:Q231|2]] File:Foo|[[d:Q231|2]] </gallery>
==[[d:Q123]]== <gallery> File:Foo|1 File:Foo|1 </gallery> ==[[d:Q231]]== <gallery> File:Foo|2 File:Foo|2 </gallery>