function Dice( faces ) {
  this.faces = faces;
  this.getFace = function ( id ) {
    return this.faces[id];
  }
  this.roll = function ( cmds ) {
    var t;
    for( var i=0; i<cmds.length; i++ ) {
      if( 'E' == cmds[i] ) {
        t = this.faces[Dice.TOP];
        this.faces[Dice.TOP] = this.faces[Dice.LEFT];
        this.faces[Dice.LEFT] = this.faces[Dice.BOTTOM];
        this.faces[Dice.BOTTOM] = this.faces[Dice.RIGHT];
        this.faces[Dice.RIGHT] = t;
      } else if( 'S' == cmds[i] ) {
        t = this.faces[Dice.TOP];
        this.faces[Dice.TOP] = this.faces[Dice.BACK];
        this.faces[Dice.BACK] = this.faces[Dice.BOTTOM];
        this.faces[Dice.BOTTOM] = this.faces[Dice.FRONT];
        this.faces[Dice.FRONT] = t;
      } else if( 'W' == cmds[i] ) {
        t = this.faces[Dice.TOP];
        this.faces[Dice.TOP] = this.faces[Dice.RIGHT];
        this.faces[Dice.RIGHT] = this.faces[Dice.BOTTOM];
        this.faces[Dice.BOTTOM] = this.faces[Dice.LEFT];
        this.faces[Dice.LEFT] = t;
      } else if( 'N' == cmds[i] ) {
        t = this.faces[Dice.TOP];
        this.faces[Dice.TOP] = this.faces[Dice.FRONT];
        this.faces[Dice.FRONT] = this.faces[Dice.BOTTOM];
        this.faces[Dice.BOTTOM] = this.faces[Dice.BACK];
        this.faces[Dice.BACK] = t;        
      } else if( 'M' == cmds[i] ) {
        t = this.faces[Dice.FRONT];
        this.faces[Dice.FRONT] = this.faces[Dice.RIGHT];
        this.faces[Dice.RIGHT] = this.faces[Dice.BACK];
        this.faces[Dice.BACK] = this.faces[Dice.LEFT];
        this.faces[Dice.LEFT] = t;        
      }
    }
  }  
}
Dice.TOP = 0;
Dice.FRONT = 1;
Dice.RIGHT = 2;
Dice.LEFT = 3;
Dice.BACK = 4;
Dice.BOTTOM = 5;

var input = '';
var readable = process.stdin;
readable.resume();
readable.setEncoding( 'utf-8' );
readable.on( 'data', function( chunk ) {
  input += chunk;
});

readable.on( 'end', function() {
  var data = input.split( '\n' );
  var output = [];
  var faces = data[0].split( ' ' ).map( Number );
  var mydice = new Dice( faces ); 
  var q = parseInt( data[1] );
  
  for( var i=0; i<q; i++ ) {
    var labels = data[2+i].split( ' ' ).map( Number );
    var cmds = "NWWWN";
    for( var j=0; j<cmds.length; j++ ) {
      if( labels[0] == mydice.getFace( Dice.TOP ) ) { break; }
      mydice.roll( cmds[j]  );
    }
    cmds = "MMM";
    for( var j=0; j<cmds.length; j++ ) {
      if( labels[1] == mydice.getFace( Dice.FRONT ) ) { break; }
      mydice.roll( cmds[j] );
    }
    output.push( mydice.getFace( Dice.RIGHT ) );
  }
  console.log( output.join( '\n' ) );
});