How many dynasties fought for control from the 8th to the 10th century