query: SELECT ?item ?image (GROUP_CONCAT(?categoryTitle) AS ?categories)
  WITH {
    SELECT ?item WHERE {
      SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:endpoint "en.wikipedia.org";
                        wikibase:api "Generator";
                        mwapi:generator "search";
                        mwapi:gsrsearch "hastemplate:Scholia";
                        mwapi:gsrlimit "max".
        ?item wikibase:apiOutputItem mwapi:item.
      }
    }
  } AS %items
  WHERE {
    hint:SubQuery hint:optimizer "None".
    INCLUDE %items.
    ?item wdt:P18 ?image.
    BIND(REPLACE(wikibase:decodeUri(CONCAT("File:", STRAFTER(STR(?image), "/wiki/Special:FilePath/"))), " ", "_") AS ?imageTitle)
    SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:api "Categories";
                      wikibase:endpoint "commons.wikimedia.org";
                      mwapi:titles ?imageTitle.
      ?categoryTitle wikibase:apiOutput mwapi:category.
    }
    FILTER(?categoryTitle = "Category:CC-Zero" || STRSTARTS(?categoryTitle, "Category:PD-"))
    FILTER(?item NOT IN (wd:wd:Q8613, wd:Q8567, wd:Q970550))
  }
  GROUP BY ?item ?image