You will need to file your NH nonresident return first. When you get to the end of the NH nonresident return, you will see how much you owe to the state of NH. Then you will enter this amount on your MA return for a credit to taxes paid to another state.
In general, your home state will tax all in come from all sources. Then you will file a nonresident return for the states that you do not reside in, but you visit for work and earn money in. When you file, you will fille out your nonresident states first. Then you will fill out your resident state, claiming a credit for taxes paid to the nonresident states on your home state return. You may or may not still end up owing money to your resident state depending on whether or not their tax rate is higher or lower than your nonresident state. If the non resident state had a higher tax rate than your resident state, your credit will be limited to the amount of tax you would have paid to your resident state. They will not give you a refund of the taxes you paid to the nonresident state.