G7 pledges to defend democracy from foreign threats