query: SELECT DISTINCT ?item 
  WHERE {
   {
     SELECT ?item WHERE {
      SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
        bd:serviceParam wikibase:api "Generator" .
        bd:serviceParam mwapi:generator "search" .
        bd:serviceParam mwapi:gsrsearch "hastemplate:Scholia" .
        bd:serviceParam mwapi:gsrlimit "max" .
        ?item wikibase:apiOutputItem mwapi:item .
      }
    } LIMIT 300
   }
    ?item wdt:P18 ?image .
    {
      SELECT ?image WHERE {
    wd:Q6938433 wdt:P910 ?category .
    
    ?link schema:about ?category; schema:isPartOf <https://commons.wikimedia.org/>; schema:name ?title .
    SERVICE wikibase:mwapi {
       bd:serviceParam wikibase:api "Generator" .
       bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
       bd:serviceParam mwapi:gcmtitle ?title .
       bd:serviceParam mwapi:generator "categorymembers" .
       bd:serviceParam mwapi:gcmprop "ids|title|type" .
       bd:serviceParam mwapi:gcmlimit "max" .
      # out
      ?subcat wikibase:apiOutput mwapi:title  .
      ?ns wikibase:apiOutput "@ns" .
      ?image wikibase:apiOutputItem mwapi:item .
    }
  }
        LIMIT 20
      }
    
   hint:Prior hint:runFirst "true".
  } LIMIT 20