When an estate is being calculated, generally the executors will collect the assets, then pay any tax, debts and administrative expenses due.
Then, when the executors are confident there is sufficient money in the estate to meet any further tax or other liabilities, they will pay legacies.
Having paid the legacies, the executors will finalise the tax and any other debts and expenses before calculating what is left and due to the residuary beneficiaries.