PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ( COUNT( DISTINCT ?person ) AS ?count ) WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER ( ?start < "1964-10-14T00:00:00Z" ^^ xsd:dateTime ) ?person wdt:P31 wd:Q5 .
}

Using entities:
- General Secretary of the Communist Party of the Soviet Union (wd:Q1048744), also known as First Secretary of the Communist Party of the Soviet Union, General Secretary of the Soviet Union, Gensek of the CPSU: is instance of historical position / is subclass of party leader / is subclass of head of state / is subclass of general secretary / de facto head of state of the Soviet Union and dirigent of the Communist Party of the Soviet Union
- human (wd:Q5), also known as human being, humans, individual Homo sapiens, individual human, modern humans, etc.: is instance of class of organisms known by one particular common name / is instance of organisms known by a particular common name / is subclass of mammal / is subclass of natural person / is subclass of omnivore / is subclass of person / is subclass of individual animal / any single member of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo, from embryo to adult

Using properties:
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... instance of mountain; volcanoes form a subclass of mountains / type to which this subject corresponds/belongs. Different ... is an instance of mountain; volcano is a subclass of mountain
- position held (wd:P39 as p/ps), also known as function, held position, holds position, job, office held, etc.: subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office
- start time (wd:P580 as pq), also known as began, beginning, building date, effective, effectivity, etc.: is subproperty of point in time / indicates the time an item begins to exist or a statement starts being valid / indicates the time an item begins to exist or a statement starts being valid / time an entity begins to exist or a statement starts being valid

Got 1 row and 1 column:
| count       |
| ----------- |
| 2 (xsd:int) |

Answer: There were 2 General Secretaries of the CPSU before Leonid Brezhnev: Joseph Stalin and Nikita Khrushchev.

There were 2 General Secretaries of the CPSU before Leonid Brezhnev: Joseph Stalin and Nikita Khrushchev.

SPARQL query over wikidata:
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ( COUNT( DISTINCT ?person ) AS ?count ) WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER ( ?start < "1964-10-14T00:00:00Z" ^^ xsd:dateTime ) ?person wdt:P31 wd:Q5 .
}

Using entities:
- General Secretary of the Communist Party of the Soviet Union (wd:Q1048744), also known as First Secretary of the Communist Party of the Soviet Union, General Secretary of the Soviet Union, Gensek of the CPSU: is instance of historical position / is subclass of party leader / is subclass of head of state / is subclass of general secretary / de facto head of state of the Soviet Union and dirigent of the Communist Party of the Soviet Union
- human (wd:Q5), also known as human being, humans, individual Homo sapiens, individual human, modern humans, etc.: is instance of class of organisms known by one particular common name / is instance of organisms known by a particular common name / is subclass of mammal / is subclass of natural person / is subclass of omnivore / is subclass of person / is subclass of individual animal / any single member of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo / common name of Homo sapiens, unique extant species of the genus Homo, from embryo to adult

Using properties:
- instance of (wd:P31 as wdt), also known as example of, has type, is a, is an, is of type, etc.: that class of which this subject is a particular example and ... a subclass of mountain (and an instance of volcanic landform) / type to which this subject corresponds/belongs. Different ... instance of mountain; volcanoes form a subclass of mountains / type to which this subject corresponds/belongs. Different ... is an instance of mountain; volcano is a subclass of mountain
- position held (wd:P39 as p/ps), also known as function, held position, holds position, job, office held, etc.: subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office / subject currently or formerly holds the object position or public office
- start time (wd:P580 as pq), also known as began, beginning, building date, effective, effectivity, etc.: is subproperty of point in time / indicates the time an item begins to exist or a statement starts being valid / indicates the time an item begins to exist or a statement starts being valid / time an entity begins to exist or a statement starts being valid

Execution result:
Got 1 row and 1 column:
| count       |
| ----------- |
| 2 (xsd:int) |

[System]
You are a question answering assistant. Your job is to generate a SPARQL query to answer a given user question.

You should follow a step-by-step approach to generate the SPARQL query:
1. Determine possible entities and properties implied by the user question.
2. Search for the entities and properties in the knowledge graphs. Where applicable, constrain the searches with already identified entities and properties.
3. Gradually build up the SPARQL query using the identified entities and properties. Start with simple queries and add more complexity as needed. Execute intermediate queries to get feedback and to verify your assumptions. You may need to refine or rethink your current plan based on the query results and go back to step 2 if needed, possibly multiple times.
4. Use the answer or cancel function to finalize your answer and stop the generation process.

Available knowledge graphs:
- wikidata at https://qlever.dev/api/wikidata with prefix index for entities and similarity index for properties, and notes:
  - P106 = occupation. Use wdt:P106 to get occupations and fetch English labels with rdfs:label + FILTER(LANG(?label)='en'). When returning human occupations confirm the subject is human (wdt:P31 wd:Q5) to avoid non-human items. For 'main business' prefer more specific occupation items (e.g., real estate entrepreneur Q7301138) over broad labels like business magnate.
  - Population (P1082) and tabular (P4179): use wdt:P1082 for direct values; for time-series use p:P1082 + ps:P1082 + pq:P585 and ORDER BY pq:P585 DESC. Inspect qualifiers (e.g., 'applies to part') and references (pr:P854) to detect scope. When computing density with area (wdt:P2046) confirm area unit/datatype (usually km²), ensure population and area present and >0, and BIND(?population/?area AS ?density).
  - Spouse nationality: do not rely only on P27. Check the spouse's place of birth (P19) and then the birthplace's country (P17) or admin parents (wdt:P131*). Many pre-unification German spouses appear under historical states—map those to modern countries when needed.
  - For languages on countries: prefer wdt:P37 (official language) and wdt:P2936 (language used). wdt:P1412 (languages spoken/written/signed) is more often used for peoples/individuals and may be empty for countries. Try P37 and P2936 first, then fall back to P1412/P407. Fetch English labels with rdfs:label + FILTER(LANG(?l)='en').
  - When filtering by date of birth (P569) use xsd:dateTime comparisons (e.g. >= "1900-01-01T00:00:00Z"^^xsd:dateTime). Inception (P571) often uses Jan-01 placeholders for year-only dates; inspect p:P571 -> ps:P571 and check qualifiers/references for precision. For independence dates, also search for related event statements/items (e.g., 'declaration of independence') and prefer qualifiers/references over assuming day/month from year-only values.
  - To retrieve qualifiers (e.g., award year) use the statement/qualifier pattern: p:Pxxx / ps:Pxxx / pq:P585. Use wdt:Pxxx for normalized/direct values when you don't need qualifiers. Example: p:P166 + pq:P585 gives award year.
  - P166 = award received. Use wdt:P166 for normalized/direct values when you only need award items. To get award years or qualifiers use p:P166 / ps:P166 / pq:P585. Many statements lack pq:P585 — use OPTIONAL { ?stmt pq:P585 ?date } and ORDER BY DESC(?date). When listing award winners, filter ?person wdt:P31 wd:Q5 to ensure humans.
  - Population time-series: use p:P1082 ?stmt . ?stmt ps:P1082 ?pop . OPTIONAL { ?stmt pq:P585 ?date } . To get the most recent value prefer a subquery to compute MAX(?date) per subject and join back to the statement to retrieve the matching population. Avoid HAVING(MAX(?date)=?date) — QLever can error. Inspect qualifiers/references for scope (city-proper vs metro).
  - wdt:P131* can be expensive and may be incomplete. First try direct wdt:P131 for immediate administrative parent(s). If missing, check wdt:P17 (country) and wdt:P625 (coordinates) or the place item's own statements. Prefer bounded traversal (1-2 hops) or explicit parent patterns and use DISTINCT/ORDER/LIMIT to de-duplicate rather than broad P131*.
  - Use search_property_of_entity to find candidate properties but always verify the property's semantics (domain/range) and whether it links to the office, office-holder, or related entity. Test with a small query before relying on results. For creative works, after locating candidate properties, filter results by instance-of (e.g., films wd:Q11424; board games wd:Q131436) to avoid non-work items.
  - Use search_property_of_entity to find relevant properties. Political parties are modelled inconsistently (P1142, P1313, P463, P39/P106). Fallback: inspect party pages/lists and MEP group memberships; document assumptions. For country government type prefer P122 (basic form of government); P1288 is inconsistent. To exclude historical states filter NOT EXISTS { ?item wdt:P576 ?d }.
  - Inspect statement references and qualifiers: use prov:wasDerivedFrom on p: statements to find sources, and check qualifiers (e.g., 'applies to part' or other scope qualifiers) to disambiguate city vs metro vs admin area. Reference labels can be empty; inspect reference node properties for URLs or publication dates.
  - Place of burial (P119): use wdt:P119 to get the direct burial place. If you need the city, follow P119 / wdt:P131 or check the burial place's wdt:P17 (country) or wdt:P625 (coordinates) if P131 is missing. Use p:P119 / ps:P119 / pq: qualifiers when you need burial-date or scope details and inspect references for source/context.
  - P118 (league/competition) is used inconsistently: sometimes on clubs, sometimes directly on players. When finding clubs use ?club wdt:P118 wd:Q... and verify ?club wdt:P31 wd:Q483394. If P118 returns players, filter by ?item wdt:P31 wd:Q5 to detect humans and avoid false positives. To get current players prefer club→p:P54 statements: ?player p:P54 ?stmt; ?stmt ps:P54 ?club. OPTIONAL{ ?stmt pq:P582 ?end } FILTER(!BOUND(?end)). Verify qualifiers/references and DOBs for plausibility.
  - Querying holders of offices (P39): prefer p:P39 / ps:P39 patterns to access qualifiers. To find current holder use: ?person p:P39 ?stmt. ?stmt ps:P39 wd:Q... . OPTIONAL{ ?stmt pq:P582 ?end } FILTER(!BOUND(?end)). Watch for duplicate statements (re-elections)  use DISTINCT or ORDER BY DESC(?start) LIMIT 1 on pq:P580. Avoid HAVING(MAX(...)=...) on QLever; prefer subqueries with ORDER/LIMIT.
  - Headquarters: use wdt:P159 for org HQs. If P159 gives a building follow that building's wdt:P131* (bounded hops). If P159 points to a place, verify its wdt:P31. Don't rely only on P17 for org-country—also check P159 and P276. Use UNION/nested patterns to cover ?org wdt:P17 wd:Q... OR ?org wdt:P159|wdt:P276 ?place . ?place wdt:P17 wd:Q... or ?place wdt:P131* wd:Q...

Index types used for entities and properties:
- Prefix index: Retrieves items by overlap between query keywords and item words. The query keywords can match item words exactly or as prefixes.
- Similarity index: Retrieves items by cosine similarity between their embeddings and the query embedding.

General notes across knowledge graphs:
- Use rdfs:label + FILTER(LANG(?label)='en') for labels; avoid SERVICE wikibase:label. If a query using SERVICE fails (e.g., HTTP 301 redirect), switch to rdfs:label + FILTER(LANG(...)) and/or schema:name. Language items (e.g., results from P2936) often lack rdfs:label@en; expect many hits without English labels—prefer P37 for official languages or map major languages by known QIDs.
- Practical query patterns: use rdfs:label + FILTER(LANG(...)) for labels. Use wdt:Pxxx for direct values; p:Pxxx/ps:Pxxx/pq:Pxxx for qualifiers. For roles: films use P161 (actor), directors P57; space missions use P1029 (crew). Query reverse and filter by instance-of film (wd:Q11424) or human (wd:Q5). For 'city' classification prefer ?x wdt:P31/wdt:P279* wd:Q515; use wdt:P2046 for area but check units/duplicates.
- When selecting latest qualifier-dated values (e.g., pq:P585 for award year or population), prefer a subquery to compute MAX(date) per subject and join back to get the matching statement/value. Avoid HAVING(MAX(?date)=?date) patterns on QLever (can cause engine errors). Use OPTIONAL for qualifiers when dates may be missing and GROUP_CONCAT for multi-values.
- Use search_entity to resolve names and verify instance-of (e.g., wdt:P31=wd:Q5). Note: search_entity can return class/designation items or noisy matches; constrain with instance-of, or search the related entity (e.g., country) and query its members. Use search_property_of_entity/search_autocomplete as fallback and always confirm P31 programmatically before trusting the top match.
- Use search_property to confirm property identifiers (e.g., "country" -> P17) before embedding them in queries. This prevents mistakes from assuming an ID and helps pick the best property if multiple choices exist.
- QLever endpoints do not support SERVICE wikibase:label — use rdfs:label + FILTER(LANG(...))='en'. Common SPARQL pitfalls: avoid '||' outside patterns (use UNION), put AND '&&' only inside FILTER, prefer VALUES over IN, and use rdfs:label filters to constrain language. When testing existence prefer FILTER EXISTS { ... } or OPTIONAL { ?s wdt:Pxx ?v . FILTER(?v = wd:Q...) } — writing OPTIONAL { ?s wdt:Pxx wd:Q... } creates no ?v for BOUND(?v). When execute() fails, simplify the query and test with list(...).
- Use list(...) to inspect all triples on an entity for exploration, but expect large results for notable items. Prefer wdt: properties or targeted p:/ps:/pq: patterns in SPARQL when you need normalized values or qualifiers, respectively.
- Before embedding property or item IDs in SPARQL, use search_property, search_property_of_entity, and search_object_of_property to discover the appropriate P/PID or object QIDs. This reduces errors from guessing IDs and surfaces better property alternatives and qualifiers. Also verify ambiguous item QIDs (e.g., currencies or template items) by checking rdfs:label and instance-of with list(...) before use.
- search_constrained and search_autocomplete can return no results for constrained queries or when the index is large. If they fail, fallback to search_entity + verify P31 (or use search_property_of_entity). Constrain with instance-of (wdt:P31) or other properties in the SPARQL to improve matches.
- When selecting awards by year, prefer xsd:dateTime range filters (e.g., >= "2001-01-01T00:00:00Z"^^xsd:dateTime && < "2002-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for portability and precision.
- When selecting award winners via wdt:P166, filter ?person wdt:P31 wd:Q5 to ensure humans — fictional characters or non-humans may have award claims. If you need award years use p:P166 / ps:P166 / pq:P585 and OPTIONAL { ?stmt pq:P585 ?year } to retrieve qualifier years.
- Use wdt:P625 (coordinates) together with wdt:P131* and wdt:P17 checks to validate a place's modern country or to filter by geography. When country labels mismatch historically, inspect admin parents and coordinates to resolve whether the item refers to historical/colonial scope or current boundaries.
- Prefer wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (e.g., pq:P585 for award year). Always fetch labels with rdfs:label + FILTER(LANG(?label) = "en"). When computing derived metrics (density/rates) confirm unit consistency (area units) and datatypes and do calculations inside SPARQL.
- If search_autocomplete returns too many candidates or errors, retry with constraints (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }) or narrower query text. If still failing, fallback to search_entity and verify wdt:P31 for type checks.
- For award-year queries use p:P166 / ps:P166 / pq:P585 to access award statements and year qualifiers. Filter by xsd:dateTime ranges (e.g., >= "2020-01-01T00:00:00Z"^^xsd:dateTime && < "2021-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for robustness.
- Use search_property_of_entity to surface relevant properties for an entity (e.g., 'birth' -> P569/P19; 'cause of death' -> P509; 'manner of death' -> P1196). list(...) can inspect triples but may return large results for notable items—prefer targeted wdt: patterns (wdt:Pxxx) or p:/ps:/pq: patterns when you need qualifiers.

SPARQL prefixes for use in function calls:
- bd: <http://www.bigdata.com/rdf#
- cc: <http://creativecommons.org/ns#
- data: <https://www.wikidata.org/wiki/Special:EntityData/
- dc: <http://purl.org/dc/elements/1.1/
- dct: <http://purl.org/dc/terms/
- foaf: <http://xmlns.com/foaf/0.1/
- geo: <http://www.opengis.net/ont/geosparql#
- geof: <http://www.opengis.net/def/function/geosparql/
- gn: <http://www.geonames.org/ontology#
- hint: <http://www.bigdata.com/queryHints#
- imdb: <https://www.imdb.com/
- math: <http://www.w3.org/2005/xpath-functions/math#
- ontolex: <http://www.w3.org/ns/lemon/ontolex#
- owl: <http://www.w3.org/2002/07/owl#
- p: <http://www.wikidata.org/prop/
- pq: <http://www.wikidata.org/prop/qualifier/
- pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/
- pqv: <http://www.wikidata.org/prop/qualifier/value/
- pr: <http://www.wikidata.org/prop/reference/
- prn: <http://www.wikidata.org/prop/reference/value-normalized/
- prov: <http://www.w3.org/ns/prov#
- prv: <http://www.wikidata.org/prop/reference/value/
- ps: <http://www.wikidata.org/prop/statement/
- psn: <http://www.wikidata.org/prop/statement/value-normalized/
- psv: <http://www.wikidata.org/prop/statement/value/
- qb: <http://purl.org/linked-data/cube#
- qfn: <http://qlever.cs.uni-freiburg.de/function#
- ql: <http://qlever.cs.uni-freiburg.de/builtin-functions/
- qlss: <https://qlever.cs.uni-freiburg.de/spatialSearch/
- rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
- rdfs: <http://www.w3.org/2000/01/rdf-schema#
- ref: <http://www.wikidata.org/reference/
- s: <http://www.wikidata.org/entity/statement/
- schema: <http://schema.org/
- skos: <http://www.w3.org/2004/02/skos/core#
- v: <http://www.wikidata.org/value/
- void: <http://rdfs.org/ns/void#
- wd: <http://www.wikidata.org/entity/
- wdno: <http://www.wikidata.org/prop/novalue/
- wdt: <http://www.wikidata.org/prop/direct/
- wdtn: <http://www.wikidata.org/prop/direct-normalized/
- wikibase: <http://wikiba.se/ontology#
- xsd: <http://www.w3.org/2001/XMLSchema#

Additional rules to follow:
- Explain your thought process before and after each step and function call.
- Do not just use or make up entity or property identifiers without verifying their existence in the knowledge graphs first.
- Do not ask the user for clarifications without first attempting to complete the task. If the task input is incomplete or ambiguous, try to make reasonable assumptions and document them.
- Do not use "SERVICE wikibase:label { bd:serviceParam wikibase:language ..." in SPARQL queries. It is not SPARQL standard and unsupported by the used QLever SPARQL endpoints. Use rdfs:label or similar properties to get labels instead.
- Always execute your final SPARQL query before giving an answer to make sure it returns the expected results.
- The SPARQL query should always return the actual identifiers / IRIs of the items in its result. It additionally may return labels or other human-readable information, but they are optional and should be put within optional clauses unless explicitly requested by the user.
- Do not stop early if there are still obvious improvements to be made to the SPARQL query. For example, keep refining your SPARQL query if its result contains irrelevant items or is missing items you expected.
- Do not perform additional computation (e.g. filtering, sorting, calculations) on the result of the SPARQL query to determine the answer. All computation should be done solely within SPARQL.
- For questions with a "True" or "False" answer the SPARQL query should be an ASK query.

[User] How many General Secretaries of the CPSU were there before Leonid Brezhnev?

[Assistant]
To determine how many General Secretaries of the CPSU (Communist Party of the Soviet Union) were there before Leonid Brezhnev, I need to:

1. Identify the item for "General Secretary of the CPSU" in Wikidata.
2. Identify Leonid Brezhnev's item in Wikidata.
3. Find all individuals who held the position of General Secretary of the CPSU before Brezhnev.
4. Count them.

### Step 1: Search for the position "General Secretary of the CPSU"
I'll search for the position using the search_property_of_entity or search_entity function to find the correct QID.

Question: General Secretary of the CPSU

Top 10 entity alternatives:
1. The Retirement of the General Secretary (wd:Q84873455): scientific article published on 01 October 1902 / scientific article published in October 1902 / scientific article published in October 1902 / is instance of scholarly article / is instance of academic article
2. The Retirement of the General Secretary (wd:Q94008531): is instance of scholarly article / is instance of academic article
3. The Retirement of the General Secretary (wd:Q94008347): is instance of scholarly article / is instance of academic article
4. General Secretary of the Chinese Communist Party (wd:Q849418, matched via General Secretary of the CCP), also known as CCP General Secretary, General Secretary, General Secretary of the CCP, General Secretary of the Central Committee of the Chinese Communist Party, General Secretary of the Communist Party of China: head of the Chinese Communist Party (CCP) and de facto leader of China / is subclass of paramount leader of the People's Republic of China / is subclass of Committee secretary of the Chinese Communist Party / is subclass of Leader of the Chinese Communist Party / is subclass of chief at the national level / is subclass of Paramount leader of China / is subclass of general secretary / is subclass of Paramount leader / is subclass of head of state / is subclass of party leader / is instance of position / is subclass of dictator
5. Secretary General of the Council of Europe (wd:Q2942251): Leadership position in the Council of Europe / is subclass of general secretary / is instance of position
6. Secretary General of the Confederación Nacional del Trabajo (wd:Q3393986, matched via Secretary General of the CNT), also known as Secretary General of the CNT: head of the confederation of Spanish anarcho-syndicalist labor unions / is instance of position
7. Secretary General of the Universal Esperanto Association (wd:Q12357331, matched via Secretary General of the UEA), also known as Ĝenerala Sekretario de UEA, Secretary General of the UEA, UEA Secretary General, Universal Esperanto Association Secretary General: is instance of position
8. General Secretary of the CIS (wd:Q119716722): head of the Commonwealth Secretariat / is instance of position
9. Secretary General of the CSTO (wd:Q119716745): diplomatic head of the CSTO / is instance of position
10. General Secretary of the YSP (wd:Q130385190): is instance of position

[Assistant]
The search results show that "General Secretary of the Chinese Communist Party" (wd:Q849418) is the closest match, but we need the position for the CPSU (Communist Party of the Soviet Union). Let me search more specifically for "General Secretary of the Communist Party of the Soviet Union".

Question: General Secretary of the Communist Party of the Soviet Union

Top 10 entity alternatives:
1. General Secretary of the Communist Party of the Soviet Union (wd:Q1048744), also known as First Secretary of the Communist Party of the Soviet Union, General Secretary of the Soviet Union, Gensek of the CPSU: de facto head of state of the Soviet Union and dirigent of the Communist Party of the Soviet Union / is instance of historical position / is subclass of general secretary / is subclass of head of state / is subclass of party leader
2. General Department of the Communist Party of the Soviet Union (wd:Q16682297): is instance of Department of the Central Committee of the Communist Party of the Soviet Union / organization
3. Secretary of the Communist Party of the Soviet Union (wd:Q115147301): is instance of historical position / is subclass of secretary / position in URSS
4. Russian Communist Workers' Party of the Communist Party of the Soviet Union (wd:Q1800161), also known as RCWP-CPSU, Russian Communist Workers' Party – Revolutionary Party of Communists: is instance of Political parties in Russia / is instance of political parties in Russia / Anti-revisionist communist party in Russia
5. The History of the Communist Party of the Soviet Union (wd:Q2657367), also known as A Short History of the Communist Party of the Soviet Union: history book commissioned by Joseph Stalin / is instance of creative work / is instance of written work
6. Office of the General Secretary of the Chinese Communist Party (wd:Q28413293)
7. Politburo of the Central Committee of the Communist Party of the Soviet Union (wd:Q842008): highest policy-making government authority under the Communist Party of the Soviet Union / is instance of politburo / is instance of CPSU body / is subclass of politburo
8. Secretariat of the Central Committee of the Communist Party of the Soviet Union (wd:Q1851579): administration organ of the Communist Party of the former Soviet Union / is instance of secretariat / is instance of CPSU body
9. Conference of Representatives of the Communist Party of the Soviet Union (wd:Q4231782): is subclass of party conference
10. Peak of the 19th Congress of the CPSU (wd:Q89029959, matched via Peak of the 19th Congress of the Communist Party of the Soviet Union), also known as Peak of the 19th Congress of the Communist Party of the Soviet Union: peak in the Trans-Alay range / is instance of mountain

[Assistant]
Perfect! I found the correct item: **General Secretary of the Communist Party of the Soviet Union** with QID **wd:Q1048744**.

### Step 2: Find Leonid Brezhnev's item
I'll search for Leonid Brezhnev.

Question: Leonid Brezhnev

Top 10 entity alternatives:
1. Leonid Brezhnev (wd:Q765165), also known as Brezhnev, Leonid Ilyich Brezhnev: General Secretary of the Communist Party of the Soviet Union (1906–1982) / General Secretary of the Communist Party of the Soviet Union (1906–1982) / has occupation Military personnel / has occupation military personnel / has occupation land surveyor / has occupation politician / has occupation surveyor / has occupation engineer / is instance of human
2. Admiral Kuznetsov (wd:Q274924, matched via Leonid Brezhnev), also known as Admiral Flota Sovetskovo Soyuza Kuznetsov, aircraft carrier Admiral Kuznetsov, Leonid Brezhnev, Riga, Tbilisi: 1985 Kuznetsov-class aircraft carrier / is instance of aircraft cruiser / is instance of aircraft carrier
3. Starry Metropolis (wd:Q22968553, matched via Leonid Brezhnev), also known as CT Neptune, IMO 7359498, Kareliya, Leonid Brezhnev, Neptune, etc.: is instance of cruise ship / ship built in 1976
4. Leonid Brezhnev (wd:Q131583282), also known as Whiskey on the Rocks Leonid Brezhnev: Leonid Brezhnev as depicted in television series Whiskey on the Rocks / is instance of historical character / is instance of television character / is instance of fictional human / has occupation politician
5. Leonid Brezhnev (wd:Q58345911): is instance of scholarly article / is instance of academic article / Scientific article (April 2014)
6. Leonid Brezhnev (wd:Q47511657): painting by Harvey Simpson / is instance of painting
7. Leonid Brezhnev (wd:Q47509621): painting by Boris Artzybasheff / painting by Boris Artzybasheff / is instance of painting
8. Leonid Brezhnev (wd:Q47510509): painting by Boris Chaliapin / painting by Boris Chaliapin / is instance of painting
9. Leonid Brezhnev (wd:Q47510378): painting by Burton Philip Silverman / painting by Burton Silverman / is instance of painting
10. Yuri Brezhnev (wd:Q8061482, matched via Yuri Leonidovich Brezhnev), also known as Yuri Leonidovich Brezhnev: Soviet politician (1933-2013) / has occupation politician / is instance of human

[Assistant]
Great! Leonid Brezhnev is **wd:Q765165**.

### Step 3: Find all General Secretaries of the CPSU before Leonid Brezhnev

I need to:
- Find all people who held the position of General Secretary of the CPSU (wd:Q1048744).
- Use the position holder property: **P39** (held office).
- Filter for those whose term ended before Brezhnev's term started.
- Use qualifiers on P39: **pq:P580** (start time) and **pq:P582** (end time) to determine the order.

I'll construct a query to:
1. Find all people with P39 = wd:Q1048744 (General Secretary of the CPSU).
2. Filter out those whose start date is after Brezhnev's start date.
3. Count them.

First, let me check Brezhnev's start date for the position.

list

wd:Q765165

[Assistant] Now I'll inspect one of the statements to see the qualifiers (start and end dates).

[Assistant]
Perfect! Brezhnev became General Secretary on **1964-10-14**.

Now I'll find all other General Secretaries of the CPSU who held the position before that date.

I'll write a SPARQL query to:
- Find all people who held the position of General Secretary of the CPSU (wd:Q1048744).
- Extract their start date (pq:P580).
- Filter those whose start date is before 1964-10-14.
- Count them.

I'll also ensure they are humans (wdt:P31 wd:Q5).

Let me execute the query.

execute

SELECT COUNT(DISTINCT ?person) AS ?count WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER(?start < "1964-10-14T00:00:00Z"^^xsd:dateTime)
  ?person wdt:P31 wd:Q5 .
}

Call to function execute returned an error:
failed to parse input: parse error at position 7 for token COUNT with content 'COUNT' (the input most likely does not follow the grammar)

[Assistant]
I made a syntax error — I used `?count` as a variable in the `SELECT COUNT(...)` but also tried to alias it as `?count`, which is invalid. I need to use `COUNT(*)` or `COUNT(?person)` without aliasing in the SELECT clause if I just want the count.

Let me fix the query and retry.

SELECT (COUNT(DISTINCT ?person) AS ?count) WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER(?start < "1964-10-14T00:00:00Z"^^xsd:dateTime)
  ?person wdt:P31 wd:Q5 .
}

Got 1 row and 1 column:
| count       |
| ----------- |
| 2 (xsd:int) |

[Assistant]
The query returned **2**, meaning there were **2 General Secretaries of the CPSU before Leonid Brezhnev**.

Let me verify who they were by running a query to list them with their start dates.

SELECT ?person ?personLabel ?start WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER(?start < "1964-10-14T00:00:00Z"^^xsd:dateTime)
  ?person wdt:P31 wd:Q5 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} ORDER BY ?start

SPARQL execution failed:
Error while executing a SERVICE request to <http://wikiba.se:80/ontology#label>: SERVICE responded with HTTP status code: 301, Moved Permanently. First 100 bytes of the response: '
<title>301 Moved Permanently</title>

301 Moved Permanently'

[Assistant]
I forgot — I cannot use `SERVICE wikibase:label` on QLever. I need to use `rdfs:label` with language filter instead.

Let me fix the query to use `rdfs:label` and retry.

SELECT ?person ?start ?label WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER(?start < "1964-10-14T00:00:00Z"^^xsd:dateTime)
  ?person wdt:P31 wd:Q5 .
  ?person rdfs:label ?label .
  FILTER(LANG(?label) = 'en')
} ORDER BY ?start

[Assistant]
Perfect! The two General Secretaries of the CPSU before Leonid Brezhnev were:

1. **Joseph Stalin** (started: 1922-04-03)
2. **Nikita Khrushchev** (started: 1953-09-14)

This confirms that there were **2** General Secretaries before Brezhnev.

### Final Answer:
There were **2** General Secretaries of the CPSU before Leonid Brezhnev: Joseph Stalin and Nikita Khrushchev.

I will now provide the final SPARQL query and answer.

answer

SELECT (COUNT(DISTINCT ?person) AS ?count) WHERE {
  ?person p:P39 ?stmt .
  ?stmt ps:P39 wd:Q1048744 .
  ?stmt pq:P580 ?start .
  FILTER(?start < "1964-10-14T00:00:00Z"^^xsd:dateTime)
  ?person wdt:P31 wd:Q5 .
}

Answer: There were 2 General Secretaries of the CPSU before Leonid Brezhnev: Joseph Stalin and Nikita Khrushchev.


PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?deathDate WHERE {
  wd:Q6711 wdt:P570 ?deathDate
}

Using entities:
- Allen Ginsberg (wd:Q6711), also known as Irwin Allen Ginsberg: has occupation photographer / has occupation musician / has occupation poet / has occupation teacher / has occupation writer / has occupation autobiographer / has occupation screenwriter / has occupation playwright / has occupation diarist / is instance of human / American poet and writer (1926–1997)

Using properties:
- date of death (wd:P570 as wdt), also known as date of the end, dead, death, death date, deathdate, etc.: is subproperty of dissolved, abolished or demolished / is subproperty of dissolved, abolished or demolished date / is subproperty of end time / date on which the subject died / date on which the subject died / date on which the subject died

Answer: Allen Ginsberg died on April 5, 1997.

Allen Ginsberg died on April 5, 1997.

SPARQL query over wikidata:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?deathDate WHERE {
  wd:Q6711 wdt:P570 ?deathDate
}

Using entities:
- Allen Ginsberg (wd:Q6711), also known as Irwin Allen Ginsberg: has occupation photographer / has occupation musician / has occupation poet / has occupation teacher / has occupation writer / has occupation autobiographer / has occupation screenwriter / has occupation playwright / has occupation diarist / is instance of human / American poet and writer (1926–1997)

Using properties:
- date of death (wd:P570 as wdt), also known as date of the end, dead, death, death date, deathdate, etc.: is subproperty of dissolved, abolished or demolished / is subproperty of dissolved, abolished or demolished date / is subproperty of end time / date on which the subject died / date on which the subject died / date on which the subject died

Execution result:
Got 1 row and 1 column:
| deathDate                           |
| ----------------------------------- |
| 1997-04-05T00:00:00Z (xsd:dateTime) |

[System]
You are a question answering assistant. Your job is to generate a SPARQL query to answer a given user question.

You should follow a step-by-step approach to generate the SPARQL query:
1. Determine possible entities and properties implied by the user question.
2. Search for the entities and properties in the knowledge graphs. Where applicable, constrain the searches with already identified entities and properties.
3. Gradually build up the SPARQL query using the identified entities and properties. Start with simple queries and add more complexity as needed. Execute intermediate queries to get feedback and to verify your assumptions. You may need to refine or rethink your current plan based on the query results and go back to step 2 if needed, possibly multiple times.
4. Use the answer or cancel function to finalize your answer and stop the generation process.

Available knowledge graphs:
- wikidata at https://qlever.dev/api/wikidata with prefix index for entities and similarity index for properties, and notes:
  - P106 = occupation. Use wdt:P106 to get occupations and fetch English labels with rdfs:label + FILTER(LANG(?label)='en'). When returning human occupations confirm the subject is human (wdt:P31 wd:Q5) to avoid non-human items. For 'main business' prefer more specific occupation items (e.g., real estate entrepreneur Q7301138) over broad labels like business magnate.
  - Population (P1082) and tabular (P4179): use wdt:P1082 for direct values; for time-series use p:P1082 + ps:P1082 + pq:P585 and ORDER BY pq:P585 DESC. Inspect qualifiers (e.g., 'applies to part') and references (pr:P854) to detect scope. When computing density with area (wdt:P2046) confirm area unit/datatype (usually km²), ensure population and area present and >0, and BIND(?population/?area AS ?density).
  - Spouse nationality: do not rely only on P27. Check the spouse's place of birth (P19) and then the birthplace's country (P17) or admin parents (wdt:P131*). Many pre-unification German spouses appear under historical states—map those to modern countries when needed.
  - For languages on countries: prefer wdt:P37 (official language) and wdt:P2936 (language used). wdt:P1412 (languages spoken/written/signed) is more often used for peoples/individuals and may be empty for countries. Try P37 and P2936 first, then fall back to P1412/P407. Fetch English labels with rdfs:label + FILTER(LANG(?l)='en').
  - When filtering by date of birth (P569) use xsd:dateTime comparisons (e.g. >= "1900-01-01T00:00:00Z"^^xsd:dateTime). Inception (P571) often uses Jan-01 placeholders for year-only dates; inspect p:P571 -> ps:P571 and check qualifiers/references for precision. For independence dates, also search for related event statements/items (e.g., 'declaration of independence') and prefer qualifiers/references over assuming day/month from year-only values.
  - To retrieve qualifiers (e.g., award year) use the statement/qualifier pattern: p:Pxxx / ps:Pxxx / pq:P585. Use wdt:Pxxx for normalized/direct values when you don't need qualifiers. Example: p:P166 + pq:P585 gives award year.
  - P166 = award received. Use wdt:P166 for normalized/direct values when you only need award items. To get award years or qualifiers use p:P166 / ps:P166 / pq:P585. Many statements lack pq:P585 — use OPTIONAL { ?stmt pq:P585 ?date } and ORDER BY DESC(?date). When listing award winners, filter ?person wdt:P31 wd:Q5 to ensure humans.
  - Population time-series: use p:P1082 ?stmt . ?stmt ps:P1082 ?pop . OPTIONAL { ?stmt pq:P585 ?date } . To get the most recent value prefer a subquery to compute MAX(?date) per subject and join back to the statement to retrieve the matching population. Avoid HAVING(MAX(?date)=?date) — QLever can error. Inspect qualifiers/references for scope (city-proper vs metro).
  - wdt:P131* can be expensive and may be incomplete. First try direct wdt:P131 for immediate administrative parent(s). If missing, check wdt:P17 (country) and wdt:P625 (coordinates) or the place item's own statements. Prefer bounded traversal (1-2 hops) or explicit parent patterns and use DISTINCT/ORDER/LIMIT to de-duplicate rather than broad P131*.
  - Use search_property_of_entity to find candidate properties but always verify the property's semantics (domain/range) and whether it links to the office, office-holder, or related entity. Test with a small query before relying on results. For creative works, after locating candidate properties, filter results by instance-of (e.g., films wd:Q11424; board games wd:Q131436) to avoid non-work items.
  - Use search_property_of_entity to find relevant properties. Political parties are modelled inconsistently (P1142, P1313, P463, P39/P106). Fallback: inspect party pages/lists and MEP group memberships; document assumptions. For country government type prefer P122 (basic form of government); P1288 is inconsistent. To exclude historical states filter NOT EXISTS { ?item wdt:P576 ?d }.
  - Inspect statement references and qualifiers: use prov:wasDerivedFrom on p: statements to find sources, and check qualifiers (e.g., 'applies to part' or other scope qualifiers) to disambiguate city vs metro vs admin area. Reference labels can be empty; inspect reference node properties for URLs or publication dates.
  - Place of burial (P119): use wdt:P119 to get the direct burial place. If you need the city, follow P119 / wdt:P131 or check the burial place's wdt:P17 (country) or wdt:P625 (coordinates) if P131 is missing. Use p:P119 / ps:P119 / pq: qualifiers when you need burial-date or scope details and inspect references for source/context.
  - P118 (league/competition) is used inconsistently: sometimes on clubs, sometimes directly on players. When finding clubs use ?club wdt:P118 wd:Q... and verify ?club wdt:P31 wd:Q483394. If P118 returns players, filter by ?item wdt:P31 wd:Q5 to detect humans and avoid false positives. To get current players prefer club→p:P54 statements: ?player p:P54 ?stmt; ?stmt ps:P54 ?club. OPTIONAL{ ?stmt pq:P582 ?end } FILTER(!BOUND(?end)). Verify qualifiers/references and DOBs for plausibility.
  - Querying holders of offices (P39): prefer p:P39 / ps:P39 patterns to access qualifiers. To find current holder use: ?person p:P39 ?stmt. ?stmt ps:P39 wd:Q... . OPTIONAL{ ?stmt pq:P582 ?end } FILTER(!BOUND(?end)). Watch for duplicate statements (re-elections)  use DISTINCT or ORDER BY DESC(?start) LIMIT 1 on pq:P580. Avoid HAVING(MAX(...)=...) on QLever; prefer subqueries with ORDER/LIMIT.
  - Headquarters: use wdt:P159 for org HQs. If P159 gives a building follow that building's wdt:P131* (bounded hops). If P159 points to a place, verify its wdt:P31. Don't rely only on P17 for org-country—also check P159 and P276. Use UNION/nested patterns to cover ?org wdt:P17 wd:Q... OR ?org wdt:P159|wdt:P276 ?place . ?place wdt:P17 wd:Q... or ?place wdt:P131* wd:Q...

Index types used for entities and properties:
- Prefix index: Retrieves items by overlap between query keywords and item words. The query keywords can match item words exactly or as prefixes.
- Similarity index: Retrieves items by cosine similarity between their embeddings and the query embedding.

General notes across knowledge graphs:
- Use rdfs:label + FILTER(LANG(?label)='en') for labels; avoid SERVICE wikibase:label. If a query using SERVICE fails (e.g., HTTP 301 redirect), switch to rdfs:label + FILTER(LANG(...)) and/or schema:name. Language items (e.g., results from P2936) often lack rdfs:label@en; expect many hits without English labels—prefer P37 for official languages or map major languages by known QIDs.
- Practical query patterns: use rdfs:label + FILTER(LANG(...)) for labels. Use wdt:Pxxx for direct values; p:Pxxx/ps:Pxxx/pq:Pxxx for qualifiers. For roles: films use P161 (actor), directors P57; space missions use P1029 (crew). Query reverse and filter by instance-of film (wd:Q11424) or human (wd:Q5). For 'city' classification prefer ?x wdt:P31/wdt:P279* wd:Q515; use wdt:P2046 for area but check units/duplicates.
- When selecting latest qualifier-dated values (e.g., pq:P585 for award year or population), prefer a subquery to compute MAX(date) per subject and join back to get the matching statement/value. Avoid HAVING(MAX(?date)=?date) patterns on QLever (can cause engine errors). Use OPTIONAL for qualifiers when dates may be missing and GROUP_CONCAT for multi-values.
- Use search_entity to resolve names and verify instance-of (e.g., wdt:P31=wd:Q5). Note: search_entity can return class/designation items or noisy matches; constrain with instance-of, or search the related entity (e.g., country) and query its members. Use search_property_of_entity/search_autocomplete as fallback and always confirm P31 programmatically before trusting the top match.
- Use search_property to confirm property identifiers (e.g., "country" -> P17) before embedding them in queries. This prevents mistakes from assuming an ID and helps pick the best property if multiple choices exist.
- QLever endpoints do not support SERVICE wikibase:label — use rdfs:label + FILTER(LANG(...))='en'. Common SPARQL pitfalls: avoid '||' outside patterns (use UNION), put AND '&&' only inside FILTER, prefer VALUES over IN, and use rdfs:label filters to constrain language. When testing existence prefer FILTER EXISTS { ... } or OPTIONAL { ?s wdt:Pxx ?v . FILTER(?v = wd:Q...) } — writing OPTIONAL { ?s wdt:Pxx wd:Q... } creates no ?v for BOUND(?v). When execute() fails, simplify the query and test with list(...).
- Use list(...) to inspect all triples on an entity for exploration, but expect large results for notable items. Prefer wdt: properties or targeted p:/ps:/pq: patterns in SPARQL when you need normalized values or qualifiers, respectively.
- Before embedding property or item IDs in SPARQL, use search_property, search_property_of_entity, and search_object_of_property to discover the appropriate P/PID or object QIDs. This reduces errors from guessing IDs and surfaces better property alternatives and qualifiers. Also verify ambiguous item QIDs (e.g., currencies or template items) by checking rdfs:label and instance-of with list(...) before use.
- search_constrained and search_autocomplete can return no results for constrained queries or when the index is large. If they fail, fallback to search_entity + verify P31 (or use search_property_of_entity). Constrain with instance-of (wdt:P31) or other properties in the SPARQL to improve matches.
- When selecting awards by year, prefer xsd:dateTime range filters (e.g., >= "2001-01-01T00:00:00Z"^^xsd:dateTime && < "2002-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for portability and precision.
- When selecting award winners via wdt:P166, filter ?person wdt:P31 wd:Q5 to ensure humans — fictional characters or non-humans may have award claims. If you need award years use p:P166 / ps:P166 / pq:P585 and OPTIONAL { ?stmt pq:P585 ?year } to retrieve qualifier years.
- Use wdt:P625 (coordinates) together with wdt:P131* and wdt:P17 checks to validate a place's modern country or to filter by geography. When country labels mismatch historically, inspect admin parents and coordinates to resolve whether the item refers to historical/colonial scope or current boundaries.
- Prefer wdt:Pxxx for normalized/direct values when qualifiers aren't needed. Use p:Pxxx / ps:Pxxx / pq:Pxxx to access qualifiers (e.g., pq:P585 for award year). Always fetch labels with rdfs:label + FILTER(LANG(?label) = "en"). When computing derived metrics (density/rates) confirm unit consistency (area units) and datatypes and do calculations inside SPARQL.
- If search_autocomplete returns too many candidates or errors, retry with constraints (e.g., SELECT * WHERE { ?search wdt:P31 wd:Q5 }) or narrower query text. If still failing, fallback to search_entity and verify wdt:P31 for type checks.
- For award-year queries use p:P166 / ps:P166 / pq:P585 to access award statements and year qualifiers. Filter by xsd:dateTime ranges (e.g., >= "2020-01-01T00:00:00Z"^^xsd:dateTime && < "2021-01-01T00:00:00Z"^^xsd:dateTime) instead of YEAR() for robustness.
- Use search_property_of_entity to surface relevant properties for an entity (e.g., 'birth' -> P569/P19; 'cause of death' -> P509; 'manner of death' -> P1196). list(...) can inspect triples but may return large results for notable items—prefer targeted wdt: patterns (wdt:Pxxx) or p:/ps:/pq: patterns when you need qualifiers.

SPARQL prefixes for use in function calls:
- bd: <http://www.bigdata.com/rdf#
- cc: <http://creativecommons.org/ns#
- data: <https://www.wikidata.org/wiki/Special:EntityData/
- dc: <http://purl.org/dc/elements/1.1/
- dct: <http://purl.org/dc/terms/
- foaf: <http://xmlns.com/foaf/0.1/
- geo: <http://www.opengis.net/ont/geosparql#
- geof: <http://www.opengis.net/def/function/geosparql/
- gn: <http://www.geonames.org/ontology#
- hint: <http://www.bigdata.com/queryHints#
- imdb: <https://www.imdb.com/
- math: <http://www.w3.org/2005/xpath-functions/math#
- ontolex: <http://www.w3.org/ns/lemon/ontolex#
- owl: <http://www.w3.org/2002/07/owl#
- p: <http://www.wikidata.org/prop/
- pq: <http://www.wikidata.org/prop/qualifier/
- pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/
- pqv: <http://www.wikidata.org/prop/qualifier/value/
- pr: <http://www.wikidata.org/prop/reference/
- prn: <http://www.wikidata.org/prop/reference/value-normalized/
- prov: <http://www.w3.org/ns/prov#
- prv: <http://www.wikidata.org/prop/reference/value/
- ps: <http://www.wikidata.org/prop/statement/
- psn: <http://www.wikidata.org/prop/statement/value-normalized/
- psv: <http://www.wikidata.org/prop/statement/value/
- qb: <http://purl.org/linked-data/cube#
- qfn: <http://qlever.cs.uni-freiburg.de/function#
- ql: <http://qlever.cs.uni-freiburg.de/builtin-functions/
- qlss: <https://qlever.cs.uni-freiburg.de/spatialSearch/
- rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
- rdfs: <http://www.w3.org/2000/01/rdf-schema#
- ref: <http://www.wikidata.org/reference/
- s: <http://www.wikidata.org/entity/statement/
- schema: <http://schema.org/
- skos: <http://www.w3.org/2004/02/skos/core#
- v: <http://www.wikidata.org/value/
- void: <http://rdfs.org/ns/void#
- wd: <http://www.wikidata.org/entity/
- wdno: <http://www.wikidata.org/prop/novalue/
- wdt: <http://www.wikidata.org/prop/direct/
- wdtn: <http://www.wikidata.org/prop/direct-normalized/
- wikibase: <http://wikiba.se/ontology#
- xsd: <http://www.w3.org/2001/XMLSchema#

Additional rules to follow:
- Explain your thought process before and after each step and function call.
- Do not just use or make up entity or property identifiers without verifying their existence in the knowledge graphs first.
- Do not ask the user for clarifications without first attempting to complete the task. If the task input is incomplete or ambiguous, try to make reasonable assumptions and document them.
- Do not use "SERVICE wikibase:label { bd:serviceParam wikibase:language ..." in SPARQL queries. It is not SPARQL standard and unsupported by the used QLever SPARQL endpoints. Use rdfs:label or similar properties to get labels instead.
- Always execute your final SPARQL query before giving an answer to make sure it returns the expected results.
- The SPARQL query should always return the actual identifiers / IRIs of the items in its result. It additionally may return labels or other human-readable information, but they are optional and should be put within optional clauses unless explicitly requested by the user.
- Do not stop early if there are still obvious improvements to be made to the SPARQL query. For example, keep refining your SPARQL query if its result contains irrelevant items or is missing items you expected.
- Do not perform additional computation (e.g. filtering, sorting, calculations) on the result of the SPARQL query to determine the answer. All computation should be done solely within SPARQL.
- For questions with a "True" or "False" answer the SPARQL query should be an ASK query.

[User] When did Allen Ginsberg die?

[Assistant]
To determine when Allen Ginsberg died, I need to find his date of death from the knowledge graph. Let's start by searching for the entity corresponding to Allen Ginsberg.

Question: allen ginsberg

Top 10 entity alternatives:
1. Allen Ginsberg (wd:Q6711), also known as Irwin Allen Ginsberg: American poet and writer (1926–1997) / has occupation autobiographer / has occupation photographer / has occupation screenwriter / has occupation playwright / has occupation musician / has occupation teacher / has occupation diarist / has occupation writer / is instance of human / has occupation poet
2. Allen Ginsberg (wd:Q102249265): Ph.D. Rutgers University, New Brunswick 1986 / is instance of human
3. Allen Ginsberg Library (wd:Q117833470): this entity is a library in the state of colorado. it was identified and verified by the chr task force / is instance of library
4. Untitled (Allen Ginsberg) (wd:Q76558858): print in the National Gallery of Art (NGA 155538) / is instance of work of art / is instance of Work of art
5. Ginsberg, Allen (1926-1997), poet (wd:Q103859353): is instance of encyclopaedic article / is instance of encyclopedia article / encyclopedia article
6. Allen Ginsberg no number (wd:Q64163104): photograph in the National Gallery of Art (NGA 88333) / is instance of photograph
7. Allen Ginsberg no number (wd:Q64163102): photograph in the National Gallery of Art (NGA 88336) / is instance of photograph
8. Allen Ginsberg no number (wd:Q64163079): photograph in the National Gallery of Art (NGA 88334) / is instance of photograph
9. Allen Ginsberg no number (wd:Q64162264): photograph in the National Gallery of Art (NGA 88332) / is instance of photograph
10. Allen Ginsberg no number (wd:Q64162725): photograph in the National Gallery of Art (NGA 88330) / is instance of photograph

[Assistant]
The first result, Allen Ginsberg (wd:Q6711), is the correct entity for the American poet and writer. Now, I will check his properties to find the date of death (P570).

Question: death

Top 10 property alternatives:
1. date of death (wd:P570 as p/wdt, matched via death), also known as date of the end, dead, death, death date, deathdate, etc.: is subproperty of dissolved, abolished or demolished date / is subproperty of dissolved, abolished or demolished / date on which the subject died / date on which the subject died / date on which the subject died / is subproperty of end time
2. manner of death (wd:P1196 as p/wdt, matched via death manner), also known as circumstance of death, death manner, death type, nature of death, type of death, etc.: general circumstances of a person's death; e.g. natural ... mechanism, e.g. heart attack, trauma, pneumonia etc. / general circumstances of a person's death; e.g. natural ... mechanism, e.g. heart attack, trauma, pneumonia etc. / general circumstances of a person's death; e.g. natural ... mechanism, e.g. heart attack, trauma, pneumonia...
3. place of death (wd:P20 as p/wdt, matched via death place), also known as death location, death place, deathplace, died, died in, etc.: most specific known (e.g. city instead of country, or ... death location of a person, animal or fictional character / most specific known (e.g. city instead of country, or ... death location of a person, animal or fictional character / most specific known (e.g. city instead of country, or ... death location of a person, animal or fictional character / is subproperty of location
4. cause of death (wd:P509 as p/wdt, matched via death cause), also known as death cause, died of, die from, die of, method of murder, etc.: underlying or immediate cause of death.  Underlying cause ... category, e.g. natural causes, accident, homicide, suicide / underlying or immediate cause of death. Underlying cause ... category, e.g. natural causes, accident, homicide, suicide / an item describing the cause of a person or specific organism's death. Samples: leukaemia, car crash / medical cause of a person or specific organism's death (not to be confused with P1196)
5. place of burial (wd:P119 as p/wdt, matched via interment), also known as ashes scattered at, burial location, burial place, buried at, buried in, etc.: location of grave, resting place, place of ash-scattering, ... places: e.g., re-burials, parts of body buried separately. / location of grave, resting place, place of ash-scattering, ... places: e.g., re-burials, parts of body buried separately. / location of grave, resting place, place of ash-scattering, ... e.g. re-burials, cenotaphs, parts of body buried separately / location of grave, resting place, place of ash-scattering, etc, (e.g. city or cemetery) for a person / is subproperty of location
6. birth name (wd:P1477 as p/wdt, matched via nee), also known as birthname, bn, bname, born as, full birth name, etc.: full name of a person at birth, if different from their ... John Peter Doe for Joe Doe, Ann Smith for Ann Miller) / full name of a person at birth, if different from their current, generally used name / full name of a person at birth, if different from their current name / is subproperty of name
7. unmarried partner (wd:P451 as p/wdt, matched via lover), also known as boyfriend, cohabitant, domestic partner, enbyfriend, girlfriend, etc.: someone with whom the person is in a relationship without being married. Use "spouse" (P26) for married couples / someone with whom the person is in a relationship without being married. Use "spouse" (P26) for married couples / is subproperty of significant person
8. ethnic group (wd:P172 as p, matched via race), also known as (cultural) nationality, culture, ethnicity, ethnic or cultural origin, people, etc.: subject's ethnicity (consensus is that a VERY high standard ... on by scholars, or 3) is fictional and portrayed as such) / subject's ethnicity (consensus is that a VERY high standard ... on by scholars, or 3) is fictional and portrayed as such)
9. described by source (wd:P1343 as p/wdt, matched via entry), also known as defined in, described by biography, described by encyclopedia, described by obituary, described by reference work, etc.: dictionary, encyclopaedia, etc. where this item is described / work where this item is described / is subproperty of attested in
10. movement (wd:P135 as p/wdt), also known as artistic movement, artistic school, art movement, literary movement, music scene, etc.: literary, artistic, scientific or philosophical movement or ... with this person or work. For political ideologies use P1142. / literary, artistic or philosophical movement associated with this person or work

No other items found

[Assistant]
The property "date of death" (P570) is the one we need. Now, I will retrieve the date of death for Allen Ginsberg (wd:Q6711) using this property.

execute

SELECT ?deathDate WHERE { wd:Q6711 wdt:P570 ?deathDate }

[Assistant]
Allen Ginsberg died on April 5, 1997. I will now provide the final answer.

answer

SELECT ?deathDate WHERE { wd:Q6711 wdt:P570 ?deathDate }

Answer: Allen Ginsberg died on April 5, 1997.