It looks like INDEX & MATCH issue.

You can do it using an Array formula OR using helper columns.

If you think that you can not handle this, pls upload a small sample workbook.