/**
 * ???????????? II

Dice I ??¨????§????????????§?????\??????????????°????????????????????????????????????????????????

????????????????????? Dice I
???????????§?????¢????????????????????¢??¨?????¢?????°??????????????¨??????????????????????????§????????´?????¢?????°???????????????????????°????????????????????????????????????





Input
?????????????????¢?????°???????????????????????????????????????????????§?????????????????????
???????????????????????° q ????????????????????????

?¶???? q
???????????????????????????????????????????????§????????¢??¨?????¢?????°????????¨??????????????´??°????????????????????§??????????????????????????????

Output
??????????????¨??????????????????????????´?????¢?????°??????????????????????????????????????????

Constraints
0??? ??\?????????????????????????????¢?????°??? ???100
??\?????????????????????????????¢?????°?????????????????°??????
1???q???24

Sample Input
1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output
3
5
6
 */

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

class Dice {
private:
  std::vector<unsigned short> Value = {1, 2, 3, 4, 5, 6};
  unsigned short Top = Value[0];
  unsigned short Front = Value[1];
  unsigned short Right = Value[2];
  unsigned short Left = Value[3];
  unsigned short Rear = Value[4];
  unsigned short Bottom = Value[5];
  enum Label { ONE, TWO, THREE, FOUR, FIVE, SIX };
  std::vector<std::vector<unsigned short>> RightState =
      std::vector<std::vector<unsigned short>>(
          6, std::vector<unsigned short>(6, 0));

public:
  Dice() : Dice("123456") {}

  Dice(std::string Param) {
    RightState[ONE][TWO] = RightState[TWO][SIX] = RightState[SIX][FIVE] =
        RightState[FIVE][ONE] = THREE;
    RightState[ONE][THREE] = RightState[THREE][SIX] = RightState[SIX][FOUR] =
        RightState[FOUR][ONE] = FIVE;
    RightState[ONE][FIVE] = RightState[FIVE][SIX] = RightState[SIX][TWO] =
        RightState[TWO][ONE] = FOUR;
    RightState[ONE][FOUR] = RightState[FOUR][SIX] = RightState[SIX][THREE] =
        RightState[THREE][ONE] = TWO;

    RightState[FOUR][TWO] = RightState[TWO][THREE] = RightState[THREE][FIVE] =
        RightState[FIVE][FOUR] = ONE;
    RightState[FOUR][ONE] = RightState[ONE][THREE] = RightState[THREE][SIX] =
        RightState[SIX][FOUR] = FIVE;
    RightState[FOUR][FIVE] = RightState[FIVE][THREE] = RightState[THREE][TWO] =
        RightState[TWO][FOUR] = SIX;
    RightState[FOUR][SIX] = RightState[SIX][THREE] = RightState[THREE][ONE] =
        RightState[ONE][FOUR] = TWO;

    setNumbers(Param);
  }

  unsigned short getTop();
  unsigned short getRight(unsigned short, unsigned short);
  void lotate(std::string);
  void setNumbers(std::string);
};

unsigned short Dice::getTop() { return Top; }

void Dice::lotate(std::string Direction) {
  for (const auto &e : Direction) {
    unsigned int Tmp;
    if (e == 'N') {
      Tmp = Rear;
      Rear = Top;
      Top = Front;
      Front = Bottom;
      Bottom = Tmp;
    } else if (e == 'E') {
      Tmp = Right;
      Right = Top;
      Top = Left;
      Left = Bottom;
      Bottom = Tmp;
    } else if (e == 'W') {
      Tmp = Left;
      Left = Top;
      Top = Right;
      Right = Bottom;
      Bottom = Tmp;
    } else if (e == 'S') {
      Tmp = Front;
      Front = Top;
      Top = Rear;
      Rear = Bottom;
      Bottom = Tmp;
    }
  }
}

void Dice::setNumbers(std::string Str) {
  std::istringstream iss(Str);
  unsigned short n;
  size_t i = 0;

  while (iss >> n) {
    switch (i++) {
    case 0:
      Top = Value[i - 1] = n;
      break;
    case 1:
      Front = Value[i - 1] = n;
      break;
    case 2:
      Right = Value[i - 1] = n;
      break;
    case 3:
      Left = Value[i - 1] = n;
      break;
    case 4:
      Rear = Value[i - 1] = n;
      break;
    case 5:
      Bottom = Value[i - 1] = n;
      break;
    }
  }
}

unsigned short Dice::getRight(unsigned short Top, unsigned short Front) {
  auto itr = find(Value.begin(), Value.end(), Top);
  size_t Ti = std::distance(Value.begin(), itr);
  itr = find(Value.begin(), Value.end(), Front);
  size_t Fi = std::distance(Value.begin(), itr);
  return Value[RightState[Ti][Fi]];
}

int main(int argc, char const *argv[]) {
  std::string Param;
  std::istringstream iss;
  getline(std::cin, Param);

  Dice D(Param);

  getline(std::cin, Param);
  iss.str(Param);
  size_t n;
  iss >> n;
  iss.clear();
  for (size_t i = 0; i < n; i++) {
    getline(std::cin, Param);
    iss.str(Param);
    unsigned short t, f, r;
    iss >> t >> f;
    iss.clear();

    r = D.getRight(t, f);
    std::cout << r << std::endl;
  }
  return 0;
}

Measured memory_bytes: 3172