decoding pair-wise inputs

if A-B and C-D are connected, i'd like to get only: 'AB' and 'CD' (as the rest are not connected..)