You can't target an element depending on what child elements it has, so you would have to add something to the code to target the different links.
Can't you use underline instead of a bottom border? That would work as it's applied to the text in the link rather than the link element itself.
#sidebar a:hover { text-decoration: underline; }
#sidebar a:hover img { text-decoration: none; }