The declarer has plenty of tricks, but is missing the trump queen and two top hearts; giving up the third heart early would probably mean losing the contract. Since A is almost certainly with West, if East gets the trump queen, he could finesse declarer's Q. Thus, the declarer must not allow East to get the trick, and so must play a trump Ace first and finesse against the queen in East's hand. If West has the Q and takes it, he could only take one more heart trick.
Note that, even if South had fifth trump instead of a diamond (playing in 5-4 fit), the same technique—finessing the queen against East—would practically ensure the contract (safety play). If the trumps are distributed x—Qxx, the percentage play of playing AK in trumps would endanger the contract, as East could ruff a later round of clubs with Q and play a heart through. With the avoidance finesse, West could win a "non-existing" trump trick if the trumps were distributed Qx–xx, but could not set the contract.