Question regarding salary cap

My understanding is that the cap his each year is based on the average salary over the life of the contract. From what I read on capgeek, it seems that most of the contracts are relatively flat amounts, though some vary a bit year to year. My question is about the cap impact if a player is traded if the contract is significantly different in the later years than the early years. So for example let's look at a player with a 5 year contract at salaries of $8 million, 8, 6, 5 and 3. That's a total of $30 million, or an cap hit each year of $6 mil.

But what happens if the player is traded after year 3 - the original team will have only booked $18 million in cap hit, but will have paid $22 million in cash - what happens to the $4 million. Furthermore, what is the cap hit for the acquiring team - I would assume it is based on the remaining contract of 2 years $8 million combined, or $4 million per year

All FanPosts and FanShots are the respective work of the author and not representative of the writers or other users of All About the Jersey.