RX+AgCN→(P)RNC+AgX RX+KCN→(Q)RCN+KX
Both C and N can donate electron pairs. Since KCN is predominantly ionic and C−C bond is more stable than C−N bond in case of KCN, attack takes place through carbon atom.
AgCN is predominantly covalent hence only nitrogen pair is available for bonding resulting in formation of isocyanide.