What is {position held} of {Gregory IX} that is {replaces} is {Ottaviano di Paoli} ?
What position did Gregory IX hold when he was replaced by Ottaviano di Paoli?
SELECT ?p WHERE { ?q property:position_held ?p . ?q property:replaces resource:Ottaviano_di_Paoli . resource:Gregory_IX property:position_held ?q }
select distinct ?obj  where {
?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> <http://wikidata.dbpedia.org/resource/Q124831> .
?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> <http://www.wikidata.org/entity/P39> .
?statement <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> ?obj .
?statement <http://www.wikidata.org/entity/P1365> <http://wikidata.dbpedia.org/resource/Q1227365>
} 

SELECT ?obj WHERE { wd:Q124831 p:P39 ?s . ?s ps:P39 ?obj . ?s pq:P1365 wd:Q1227365 }