In both of them jashatav sandhi( vyanjan sandhi) is used.
Before combining two varnas(alphabets) if there is any of the first,second consonant from five class ( like k, kh, ch, chh, t, th, p, ph etc.)and it is follwed by any swar( vowel) , or soft consonant that is last three alphabets from five classes( except "t" class) then after the combination that first or second consonant from these classes is converted into the third consonat of the same class.
For example , in vagmai "vak" has k at last and mayi has soft consonant " m" at first of mayi. When we combine these two alphabets then the alphabet "k" is converted into " g"- third alphabet of same class. So vaagmai is formed.
THUMBS UP.