Following from the previous question, During the event, a thief got inside the venue and started pick-pocketing from the corner. He sequentially sneaked the money from the guests. However, once a guest is already robbed, he becomes alert and cannot be robbed again, and on being found, the person informs his adjacent neighbors, and therefore the thief has to return their money. After giving the money, they leave the thief.
Your task is to calculate the total money thief managed to take out successfully.
Initially, the guest name and their money are provided. Next contains the t number of test cases. In each test case, n number of next neighbors are given following the name of next neighbors (Starting with ‘A’ for all cases).
Note
struct node {
char name;
int money;
struct node *next;
};
Input:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
2 3 4 5 6 3 2 4 5 6 3 1 2 4 5 7 8 9 6 5 3 2 3 4 5 2
3
5
B C R E W // A -> B -> C -> R -> E -> W
5
B D H R B // A -> B -> D -> H -> R -> B
2
I C // A -> I -> C
Output:
27 // A(2) + B(3) + C(4) + R(9) + E(6) + W(3) = 27
4 // A(2) + B(3) + D(5) + H(4) + R(9) - [ B(3) + D(5) + A(2) + R(9) ] = 2 + 3 + 5 + 4 + 9 - 3 - 5 - 2 - 9
11 // A(2) + I(5) + C(4)
Explanation
In case 2, the thief again attempted to loot B, who was looted before. Therefore, B found the thief and alerted his neighbors. Thus, the thief has to return the money of A, B, D and R. Only H does not get his money since he was not in the adjacent neighbor of B.
In case 2, the thief again attempted to loot B, who was looted before. Therefore, B found the thief and alerted his neighbors. Thus, the thief has to return the money of A, B, D and R. Only H does not get his money since he was not in the adjacent neighbor of B.