query: PREFIX wd: <http://www.wikidata.org/entity/>
  PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  PREFIX wikibase: <http://wikiba.se/ontology#>
  PREFIX p: <http://www.wikidata.org/prop/>
  PREFIX v: <http://www.wikidata.org/prop/statement/>
  PREFIX q: <http://www.wikidata.org/prop/qualifier/>
  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  PREFIX ps: <http://www.wikidata.org/prop/statement/>
  PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
  
     #  Query to find the most-frequently represented
     #  variants of the male given name ?tgt_name
     #  when ?tgt_name = Q2958359
     #  Query generated by [[Template:NameVariantStatsM]]
  
  SELECT ?name ?nameLabel ?count WHERE {
    hint:Query hint:optimizer "None" . 
    {
      SELECT ?name (COUNT(DISTINCT ?a) AS ?count) WHERE {
         { 
           SELECT DISTINCT ?name WHERE {
              {?name wdt:P460* wd:Q15235674 } UNION { wd:Q15235674 wdt:P460* ?name} .
              ?name wdt:P31 wd:Q12308941 .
           } LIMIT 100
         } .
         ?a wdt:P735 ?name .
         ?a wdt:P31 wd:Q5 .
      } GROUP BY ?name
    } . 
    SERVICE wikibase:label {
      bd:serviceParam wikibase:language "en" .
    }
  }
  # HAVING (?count > 1)
  ORDER BY DESC(?count) ?itemLabel
  