What are P and Q?

P stands for Please and Q stands for Thankyou. Where the phrase ‘watch your p’s and q’s’ to mind your manners. Is it to encourage people to speak politely especially the children.