Fix mapping quantities to selected lines (backport)

Cherry-picked from 6a10fee64cd147e383f11678f467b416fec76a4d