Gains and losses can be offset to any amount. The $3k limit is only for net losses - you can't take more than $3k in any one year against other income (non capital gains). You can however, carry it forward forever.
So in your example, you would owe no taxes on the gains because you had an offset of the entire amount.