In Wikidata I want to find the country of the goods. Either directly, if the goods have a country directly, or by going up to P131 (located in the administrative territorial unit) until I find the country. Here is the request:
?item wdt:P131*/wdt:P17 ?country.
The above query works fine ... unless the unit belonged to another country, for example Q25270 (Pristina). In this case, the result may be anachronistic. This is what I want to fix.
Great news: in such cases, we should consider only the unique P131 (located in the administrative-territorial unit), which does not have the sub-property P582 attached to it (end of time), and the problem is solved!
My question is : how to modify my request above to achieve this?
Example : let’s say that MyItem is in MyStreet, in MyTown is MyRegion in MyCountry, I have to make sure that MyStreet, MyTown and MyRegion do not have P582 (end time).

(If the "sub-property" is not the correct term, tell me the correct term and I will fix the question, thanks!)
Attempt
The query below works in most cases , but, unfortunately, it has an error: it finds the wrong country in cases where the current country is also a country in the past (for example, Alsace belonged to France until 1871, then to Germany and now during France again).
SELECT DISTINCT ?country WHERE {
wd:Q6556803 wdt:P131* ?area .
?area wdt:P17 ?country .
OPTIONAL {
wd:Q6556803 wdt:P131*/p:P131 [
pq:P582 ?endTime; ps:P131/wdt:P131* ?area
] .
} .
FILTER( !BOUND( ?endTime ) ) .
}