Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice I

Write a program to simulate rolling a dice, which can be constructed by the following net.

As shown in the figures, each face is identified by a different label from 1 to 6.

Write a program which reads integers assigned to each face identified by the label and a sequence of commands to roll the dice, and prints the integer on the top face. At the initial state, the dice is located as shown in the above figures.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.

In the second line, a string which represents a sequence of commands, is given. The command is one of 'E', 'N', 'S' and 'W' representing four directions shown in the above figures.

Output

Print the integer which appears on the top face after the simulation.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

$0 \leq $ the length of the command $\leq 100$

Sample Input 1

1 2 4 8 16 32
SE

Sample Output 1

8

You are given a dice where 1, 2, 4, 8, 16 are 32 are assigned to a face labeled by 1, 2, ..., 6 respectively. After you roll the dice to the direction S and then to the direction E, you can see 8 on the top face.

Sample Input 2

1 2 4 8 16 32
EESWN

Sample Output 2

32

Solution in PHP (judge verdict: Accepted)

<?php
$main = new Main();
$main->execute();

class Main {

  public function execute()
  {
    list($top, $front, $right, $left, $back, $bottom) = explode(' ', trim(fgets(STDIN)));
    $directions = str_split(trim(fgets(STDIN)));
    $dice = new Dice($top, $front, $right, $left, $back, $bottom);
    $rotater = new DiceRotater($dice);

    foreach ($directions as $direction) {
      $rotater->rotate($direction);
    }

    echo $rotater->getDice()->top."\n";
  }

}


class Dice {
  public $top;
  public $front;
  public $right;
  public $left;
  public $back;
  public $bottom;

  public function __construct($top, $front, $right, $left, $back, $bottom)
  {
    $this->top = $top;
    $this->front = $front;
    $this->right = $right;
    $this->left = $left;
    $this->back = $back;
    $this->bottom = $bottom;
  }

}

class DiceRotater {
  private $dice;

  public function __construct(Dice $dice)
  {
    $this->dice = $dice;
  }

  public function rotate($direction)
  {
    switch ($direction) {
      case ('N'):
        $this->rotateN();
        break;
      case ('S'):
        $this->rotateS();
        break;
      case ('W'):
        $this->rotateW();
        break;
      case ('E'):
        $this->rotateE();
        break;
    }
  }

  public function rotateN()
  {
    $this->dice = new Dice(
      $this->dice->front,
      $this->dice->bottom,
      $this->dice->right,
      $this->dice->left,
      $this->dice->top,
      $this->dice->back
    );
  }

  public function rotateS()
  {
    $this->dice = new Dice(
      $this->dice->back,
      $this->dice->top,
      $this->dice->right,
      $this->dice->left,
      $this->dice->bottom,
      $this->dice->front
    );
  }

  public function rotateE()
  {
    $this->dice = new Dice(
      $this->dice->left,
      $this->dice->front,
      $this->dice->top,
      $this->dice->bottom,
      $this->dice->back,
      $this->dice->right
    );
  }

  public function rotateW()
  {
    $this->dice = new Dice(
      $this->dice->right,
      $this->dice->front,
      $this->dice->bottom,
      $this->dice->top,
      $this->dice->back,
      $this->dice->left
    );
  }

  public function getDice()
  {
    return $this->dice;
  }

}