#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <cassert>
#include <queue>
using namespace std;

enum class Direction { North, East, West, South, };

struct Faces {
  int top;
  int front;
  int right;
  int left;
  int back;
  int bottom;

  Faces(const int t, const int f, const int r, const int l, const int ba, const int bo):
    top(t), front(f), right(r), left(l), back(ba), bottom(bo) {}
};

bool operator==(const Faces& lval, const Faces& rval) {
  return
    lval.top == rval.top &&
    lval.front == rval.front &&
    lval.right == rval.right &&
    lval.left == rval.right &&
    lval.back == rval.back &&
    lval.bottom == rval.bottom;
}

class Dice {
private:
  Faces faces_;
public:
  Dice(const Faces& faces):
    faces_(faces) {}

  Dice rotate_to(const Direction direction) {
    const Faces faces = [&](Direction direction) {
                          switch (direction) {
                          case Direction::North:
                           return Faces(faces_.front, faces_.bottom, faces_.right, faces_.left, faces_.top, faces_.back);
                          case Direction::East:
                            return Faces(faces_.left, faces_.front, faces_.top, faces_.bottom, faces_.back, faces_.right);
                          case Direction::West:
                            return Faces(faces_.right, faces_.front, faces_.bottom, faces_.top, faces_.back, faces_.left);
                          case Direction::South:
                            return Faces(faces_.back, faces_.top, faces_.right, faces_.left, faces_.bottom, faces_.front);
                          default:
                            assert("Invalid Direction.");
                          }
                        }(direction);
    return Dice(faces);
  }

  Faces faces() const {
    return this->faces_;
  }

  int top() const {
    return this->faces_.top;
  }
};

bool operator==(const Dice& lhs, const Dice& rhs) {
  return lhs.faces() == rhs.faces();
}

Dice make_dice_from_input() {
  int top,front,right,left,back,bottom;
  cin >> top >> front >> right >> left >> back >> bottom;
  Faces faces(top, front, right, left, back, bottom);
  Dice dice(faces);
  return dice;
}

Direction to_direction(const char& c) {
  Direction d = Direction::North;
  switch (c) {
  case 'N':
    d = Direction::North;
    break;
  case 'E':
    d = Direction::East;
    break;
  case 'W':
    d = Direction::West;
    break;
  case 'S':
    d = Direction::South;
    break;
  default:
    assert("Invalid Direction Char.");
  }
  return d;
}

void roll_from_input() {
  Dice dice = make_dice_from_input();
  string directions;
  cin >> directions;
  for (const char& direct : directions) {
    Direction d = to_direction(direct);
    dice = dice.rotate_to(d);
  }
  cout << dice.top() << endl;
}

bool exist(const Dice& dice, const vector<Dice>& memo) {
  return find(memo.begin(), memo.end(), dice) != memo.end();
}

int problem_B(queue<Dice>& dices, vector<Dice>& memo, int top, int front) {
  const vector<Direction> directions = { Direction::North, Direction::East, Direction::West, Direction::South };
  while(true) {
    for(const auto& direction : directions) {
      const auto dice = dices.front().rotate_to(direction);
      if(dice.faces().top == top && dice.faces().front == front) {
        return dice.faces().right; // 発見
      }
      if(exist(dice, memo)) {
        continue;
      }
      dices.push(dice);
    }
    dices.pop();
  }
}

int main() {
  const auto dice = make_dice_from_input();
  int q;
  cin >> q;
  vector<int> ansers;
  for (int i = 0; i < q; i++) {
    int top, front;
    cin >> top >> front;
    vector<Dice> memo = { dice };
    queue<Dice> dices;
    dices.push(dice);
    int ans = problem_B(dices, memo, top, front);
    ansers.push_back(ans);
  }

  for(auto ans : ansers) {
    cout << ans << endl;
  }
  return 0;
}



Measured memory_bytes: 3128