How did the names Whig and Tory originate?

The names of these two English political parties Whig and Tory were each invented by the respective party’s rival as a pejorative term.

The Country Party, representing the merchants and middle class, called their opponents tories, a word for Irish plunderers.

The Court Party, representing the aristocrats, squires, and Anglican clergy, called their opponents Whigs, or Scottish cattle rustlers.