// Задача "Перекресток"
// Вторая командная Интернет-олимпиада сезона 2010-2011, базовый уровень.
// Автор задачи: Лидия Перовская.
// Автор решения: Лидия Перовска, lperovkaya@gmail.com

program carrefour_lp_wr;
var a, b, c, d, N, i, time : integer;
    ti, di : array [1..100] of integer;
    queue : array [1..4,1..100] of integer;
    head : array [1..4] of integer;
    res : array [1..100] of integer;
    crossing : boolean;

procedure swap(var x,y : integer);
var z : integer;
begin
  z := x;
  x := y;
  y := z;
end;

begin

  reset (input, 'carrefour.in');
  rewrite (output, 'carrefour.out');
  readln (N);
  readln (a, b);
  if abs(a-b) = 2 then
  begin
    c := a mod 4 + 1;
    d := b mod 4 + 1;
  end
  else
  begin
    if a = b mod 4 + 1 then
      swap(a,b);
    c := b mod 4 + 1;
    d := c mod 4 + 1;
  end;
  for i := 1 to 4 do
  begin
    head[i] :=101;
  end;
  for i := 1 to N do
  begin
    queue[1][N] := 0;
    queue[2][N] := 0;
    queue[3][N] := 0;
    queue[4][N] := 0;
  end;
  for i := 1 to N do
  begin
    res[i] := 0;
    readln (di[i], ti[i]);
    queue[di[i]][ti[i]] := i;
    if ti[i] < head[di[i]] then
      head[di[i]] := ti[i];
  end;
  time := 0;
  while (head[1] <= 100) or (head[2] <= 100) or (head[3] <= 100) or (head[4] <= 100) do
  begin
    crossing := false;
    if (b-a) = 2 then
    begin
      if (head[a] <= 100) and (head[a] <= time) then
      begin
        res[queue[a][head[a]]] := time;
        crossing := true;
        head[a] := head[a] + 1;
        while (head[a] <= 100) and (queue[a][head[a]] = 0) do
          head[a] := head[a] + 1;
      end;
      if (head[b] <= 100) and (head[b] <= time) then
      begin
        res[queue[b][head[b]]] := time;
        crossing := true;
        head[b] := head[b] + 1;
        while (head[b] <= 100) and (queue[b][head[b]] = 0) do
          head[b] := head[b] + 1;
      end;
      if not crossing then
      begin
        if (head[c] <= 100) and (head[c] <= time) then
        begin
          res[queue[c][head[c]]] := time;
          head[c] := head[c] + 1;
          while (head[c] <= 100) and (queue[c][head[c]] = 0) do
            head[c] := head[c] + 1;
        end;
        if (head[d] <= 100) and (head[d] <= time) then
        begin
          res[queue[d][head[d]]] := time;
          head[d] := head[d] + 1;
          while (head[d] <= 100) and (queue[d][head[d]] = 0) do
            head[d] := head[d] + 1;
        end;
      end;
      time := time + 1;
      continue;
    end
    else
    begin
      if (head[a] <= 100) and (head[a] <= time) then
      begin
        res[queue[a][head[a]]] := time;
        crossing := true;
        head[a] := head[a] + 1;
        while (head[a] <= 100) and (queue[a][head[a]] = 0) do
          head[a] := head[a] + 1;
      end;
      if ( not crossing) and (head[b] <= 100) and (head[b] <= time) then
      begin
        res[queue[b][head[b]]] := time;
        crossing := true;
        head[b] := head[b] + 1;
        while (head[b] <= 100) and (queue[b][head[b]] = 0) do
          head[b] := head[b] + 1;
      end;
      if (not crossing) and (head[c] <= 100) and (head[c] <= time) then
      begin
        res[queue[c][head[c]]] := time;
        crossing := true;
        head[c] := head[c] + 1;
        while (head[c] <= 100) and (queue[c][head[c]] = 0) do
          head[c] := head[c] + 1;
      end;
      if (not crossing) and (head[d] <= 100) and (head[d] <= time) then
      begin
        res[queue[d][head[d]]] := time;
        head[d] := head[d] + 1;
        while (head[d] <= 100) and (queue[d][head[d]] = 0) do
          head[d] := head[d] + 1;
      end;
      time := time + 1;
      continue;

    end;
  end;
  for i:= 1 to N do
  begin
    writeln(res[i]);
  end;
end.

