Cross link: Check to see if two inputs share the same data
I've attached an image that has a data table in cells B3:E6. On the left side is a list of categories, from 1 to 3. The data to the right of this list (in cells C4:E6) is data based on these categories. For example, category 1 has the following data: 'circle', 'square' and 'triangle'. I would like to create something, as shown in examples A and B, where I can input these categories ranging from 1 to 3 in the green-filled areas, and an output is automatically generated in the orange-filled areas. This output will be based on what data is shared by the categories selected.
In example A, I have inputs '1' and '2'. If you look at the data table, the word 'triangle' is shared between 1 and 2 (cells E4 and E5). This is the only output.
In example B, I have inputs '1', '2', and '3'. If you look at the data table, the words 'circle' and 'square' are shared between '1' and '3' (C4:D4 and C6:D6). So, the output has both of these shapes for '1' and '3'. The only other output is 'triangle', as this is shared between '1' and '2'.
Could somebody please help me with a formula that can generate said outputs?
Thank you.