use std::io::*;
use std::str::FromStr;

#[allow(unused_macros)]
macro_rules! scan {
  ($e:expr; $t:ty) => {
    $e.get::<$t>()
  };
  ($e:expr; $($t:ty), *) => {
    ($($e.get::<$t>(),)*)
  }
}

struct Scanner<R: BufRead> {
  reader: R,
  iter: std::vec::IntoIter<String>
}

#[allow(dead_code)]
impl<R: BufRead> Scanner<R> {
  fn new(reader: R) -> Scanner<R> {
    Scanner { reader, iter: vec![String::new()].into_iter() }
  }
  fn new_line(&mut self) {
    let mut line = String::new();
    self.reader.read_line(&mut line).ok();
    self.iter = line.trim().split_whitespace().map(|s| s.to_string()).collect::<Vec<String>>().into_iter();
  }
  fn get<T: FromStr>(&mut self) -> T {
    self.iter.next().unwrap().parse().ok().unwrap()
  }
  fn get_as_vec<T: FromStr>(&mut self) -> Vec<T> {
    self.iter.clone().map(|v| v.parse().ok().unwrap()).collect()
  }
  fn get_line(&mut self) -> String {
    let mut line = String::new();
    self.reader.read_line(&mut line).ok();
    line.trim().to_string()
  }
}

struct Dice {
  label: Vec<i32>,
}

#[allow(dead_code)]
impl Dice {
  fn new(dice: Vec<i32>) -> Dice {
    Dice { label: dice }
  }
  fn top(&self) -> i32 {
    self.label[0]
  }
  fn front(&self) -> i32 {
    self.label[1]
  }
  fn right(&self) -> i32 {
    self.label[2]
  }
  fn east(&mut self) {
    let tmp = self.label[0];
    self.label[0] = self.label[3];
    self.label[3] = self.label[5];
    self.label[5] = self.label[2];
    self.label[2] = tmp;
  }
  fn west(&mut self) {
    let tmp = self.label[0];
    self.label[0] = self.label[2];
    self.label[2] = self.label[5];
    self.label[5] = self.label[3];
    self.label[3] = tmp;
  }
  fn north(&mut self) {
    let tmp = self.label[0];
    self.label[0] = self.label[1];
    self.label[1] = self.label[5];
    self.label[5] = self.label[4];
    self.label[4] = tmp;
  }
  fn south(&mut self) {
    let tmp = self.label[0];
    self.label[0] = self.label[4];
    self.label[4] = self.label[5];
    self.label[5] = self.label[1];
    self.label[1] = tmp;
  }
  fn rot_right(&mut self) {
    let tmp = self.label[1];
    self.label[1] = self.label[3];
    self.label[3] = self.label[4];
    self.label[4] = self.label[2];
    self.label[2] = tmp;
  }
  fn rot_left(&mut self) {
    let tmp = self.label[1];
    self.label[1] = self.label[2];
    self.label[2] = self.label[4];
    self.label[4] = self.label[3];
    self.label[3] = tmp;
  }
  fn include_of(&self, elem: i32) -> usize {
    self.label.iter().position(|&e| e == elem).unwrap()
  }
  fn include_horizontal(&self, elem: i32) -> bool {
    match self.include_of(elem) {
      1 | 2 | 3 | 4 => true,
      _ => false
    }
  }
  fn include_vertical(&self, elem: i32) -> bool {
    match self.include_of(elem) {
      0 | 1 | 4 | 5 => true,
      _ => false
    }
  }
}

fn main() {
  let cin = stdin();
  let cin = cin.lock();
  let mut sc = Scanner::new(cin);
  sc.new_line();
  let dice: Vec<i32> = sc.get_as_vec();
  let mut dice = Dice::new(dice);
  sc.new_line();
  let q = scan!(sc; i32);
  for _ in 0..q {
    sc.new_line();
    let (t, f) = scan!(sc; i32, i32);
    if !dice.include_vertical(t) {
      dice.rot_right();
    }
    while dice.top() != t {
      dice.south();
    }
    while dice.front() != f {
      dice.rot_right();
    }
    println!("{}", dice.right());
  }
}



Measured memory_bytes: 5028