const
  maxn = 100;

var
  q : array[1..4, 1..maxn] of integer;
  bq, eq, pr : array[1..4] of integer;
  nn, t, d, ans : array[1..maxn] of integer;
  main1, main2, n, tt, i, j, prior, time : integer;
  found : boolean;


function notempty : boolean;
var
  i : integer;
begin
  for i := 1 to 4 do begin
    if (bq[i] < eq[i]) then begin
      result := true;
      exit;
    end;
  end;
  result := false;
end;

procedure addqueue(i, n : integer);
begin
  q[i][eq[i]] := n;
  eq[i] := eq[i] + 1;
end;

begin
  reset(input, 'carrefour.in');
  rewrite(output, 'carrefour.out');
  read(n);
  assert((1 <= n) and (n <= maxn));
  read(main1, main2);
  assert((1 <= main1) and (main1 <= 4));
  assert((1 <= main2) and (main2 <= 4));
  assert((main1 <> main2));
  if (main1 > main2) then begin
    tt := main1;
    main1 := main2;
    main2 := tt;
  end;
  if (main1 + 2 = main2) then begin
    pr[main1] := 0;
    pr[main2] := 0;
    pr[5 - main1] := 1;
    pr[5 - main2] := 1;
  end else begin
    if (main2 = 4) and (main1 = 1) then begin
      pr[4] := 0;
      pr[1] := 1;
      pr[2] := 2;
      pr[3] := 3;
    end else begin
      pr[main1] := 0;
      pr[main2] := 1;
      pr[main2 mod 4 + 1] := 2;
      pr[(main2 + 1) mod 4 + 1] := 3;
    end;
  end;
  for i := 1 to n do begin
    read(d[i], t[i]);
    assert((1 <= d[i]) and (d[i] <= 4));
    assert((1 <= t[i]) and (t[i] <= 100));
    nn[i] := i;
  end;
  for i := 1 to n do begin
    for j := i + 1 to n do begin
      if (t[i] = t[j]) then
        assert(d[i] <> d[j]);
    end;
  end;
  for i := 1 to n do begin
    for j := i + 1 to n do begin
      if (t[j] < t[i]) then begin
        tt := t[i];
        t[i] := t[j];
        t[j] := tt;
        tt := d[i];
        d[i] := d[j];
        d[j] := tt;
        tt := nn[i];
        nn[i] := nn[j];
        nn[j] := tt;
      end;
    end;
  end;
  i := 1;
  time := 1;
  while (i <= n) or notempty do begin
    while (i <= n) and (t[i] <= time) do begin
      addqueue(d[i], nn[i]);
      i := i + 1;
    end;
    for prior := 0 to 3 do begin
      found := false;
      for j := 1 to 4 do begin
        if (pr[j] = prior) and (bq[j] < eq[j]) then begin
          ans[q[j][bq[j]]] := time;
          bq[j] := bq[j] + 1;
          found := true;
        end;
      end;
      if found then break;
    end;
    time := time + 1;
  end;
  for i := 1 to n do begin
    writeln(ans[i]);
  end;
end.