(d) Trusts.-The net tax attributable to property passing under the terms of any trust other than a trust created in the decedent's will shall be apportioned in the following order of priority:
1. The net tax attributable to nonresiduary interests of the trust shall be charged to and paid from the residuary portion of the trust, whether or not all interests in the residuary portion are included in the measure of the tax. If the residuary portion is insufficient to pay the net tax attributable to all nonresiduary interests, the balance of the net tax attributable to nonresid-uary interests shall be apportioned among the recipients of the nonresiduary interests in the proportion that the value of each nonresiduary interest included in the measure of the tax bears to the total of all nonresiduary interests included in the measure of the tax.
2. The net tax attributable to residuary interests of the trust shall be apportioned among the recipients of the residuary interests of the trust included in the measure of the tax in the proportion that the value of each residuary interest included in the measure of the tax bears to the total of all residuary interests of the trust included in the measure of the tax. If the residuary portion is insufficient to pay the net tax attributable to all residuary interests, the balance of the net tax attributable to residuary interests shall be apportioned among the recipients of the nonresiduary interests in the proportion that the value of each nonresiduary interest included in the measure of the tax bears to the total of all nonresiduary interests included in the measure of the tax.
Except as provided in paragraph (g), this paragraph applies separately for each trust.