Pirates rally for 5-3 win over Royals