NJEG1 - Maths is Magic

Nikita is facinated by card tricks. She knows that many of the tricks are nothing but mathematical calculations, so she decides to create her own trick. She calls her three friends Medha, Muskan and Anita to show her trick.

The trick goes like this. Nikita has a deck of cards (52 cards), 4 plates (plate1, plate2, plate3, plate4). She asks her friend to shuffle them.

Now she wants her friends to assume some things:

  1. Jack card has value = 1.
  2. Queen has value = 2.
  3. King has value = 3.
  4. Ace has value = 4.
  5. Rest of the cards have the value that are on them.
  6. Suits don't matter.

Now from the shuffled deck, Nikita takes out one by one 26 cards and puts them on plate1 (these 26 cards are shown to everyone) upside down and hands over the remaining deck to Medha.

She asks Medha to take any card say card A from the deck and put it on the plate2 and make its value to 10 by adding the other cards to the plate. For instance, suppose the card taken by Medha is 4, now its value can only be made to 10 by adding any 6 cards (irrespective of the value of the cards) from the deck. Medha passes the remaining cards to Muskan.

Now she asks Muskan to take any card from the remaining deck of cards say card B and put it on the plate 3 and make its value to 10 like Medha did. For instance, Suppose Muskan choose Jack (value = 1), now its value can only be made to 10 by adding any 9 cards from the deck. Muskan passes remaining cards to Anita.

Now she asks Anita to take any card from the remaining deck of cards say card C and put it on the plate 4 and make its value to 10 like Medha did, If Anita doesn't have sufficient cards she can borrow from the plate1 (from top of the deck). For instance, suppose Anita choose 7, now its value can only be made to 10 by adding any 3 cards from the deck. Anita passes remaining cards to Nikita.

Now she put the remaining cards on the deck that is already on plate1 (upside down), and asks her friend to tell her the sum of card A + card B + card C. Say this sum is index.

From the deck of cards on plate1, she predicts the value of a card at the index (index = card A + card B + card C). Her friends take out the cards from plate1 and find the same card at that index. Voila they are surprised.

Input

First line will contain 'T', the number of test cases.

Each line will have values of 26 cards, followed by the values of Card A, Card B and Card C.

(It is guaranteed that the cards A, B and C are not Q, K, A.)

Output

Each test case must predict the value of the card in plate1 at the index. Where index = values of card A + card B + card C.

(Remember plate1 has 26 cards plus remaining cards given by Anita.)

Example

Input:
1
A 4 2 3 6 5 7 8 9 10 J K Q 2 3 4 5 6 7 8 J Q 8 9 5 4
7 2 4

Output:
7

Explanation

Plate1 initially has 26 cards. Now from remaning 26 cards 7 (Card A) was chosen by Medha and to make its value 10, three more cards were added to plate2. From remaining 22 cards, Muskan choose 2 (Card B) and added 8 more cards to make its value 10. Now from remaining 13 cards, Anita choose 4 Card C) and added 6 more cards. Remaining 6 cards were put on 26 cards already in plate1 i.e. plate1 now has (26 + 6) = 32 cards.

Index = 13 (7 + 2 + 4), So Nikita predicted card at index number = 13 from the 32 cards.)


Added by:ragwave
Date:2016-10-28
Time limit:0.5s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU

hide comments
2018-08-15 17:55:52
could anyone give a hint to solve this, please!
2017-08-29 14:51:59
@Problem Setter: Who is NIkita and why is she so fascinated by cards???
2016-11-03 22:22:08
Enjoyed solving it !
2016-10-31 13:42:58
just can say wow and thanks for the problem
2016-10-31 07:00:51
This should be a tutorial or basic. Please review the problem.

Edit: Thanks for the review!

Last edit: 2016-10-31 12:11:21
2016-10-30 10:41:24 Sushovan Sen
This is not a classical problem.
2016-10-30 08:35:15
if you get the logic, 5 line code....
2016-10-29 06:52:50 shahzada
i think this condition will never happen .. as i got AC assuming that!!
2016-10-29 05:42:52 [Rampage] Blue.Mary
What will happen if Anita find the number of remaining cards is not enough to make the value of 10?

@Blue.Mary: Editing has been done. So there is no case where Anita would be deprived of cards to make her value 10.

Last edit: 2016-10-29 07:24:01
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.