The UK has agreed a pact with the two Nordic nations to help each other if they were attacked.