{
TASK:MIKRO
LANG:PASCAL
}
program mikroschema;
  const PR = 'schema.dat';
        RZ = 'schema.rez';
        MP = 20;
        MG = 20;
        MA = 100;
        MI = 100;
        MAX_E = MP+MA+MI+MG; { maksimalus  elementø skaièius }
        MAX_R = 300;         { maksimalius ryðiø skaièius }


  type elementas = record
                     in1, in2, r: integer; { áëjimø elementø numeriai masyve ir iðëjimo reikðmë}
                     op: char; { operacija }
                   end;
       elem_sar = array [0..MAX_E] of elementas;
       rysys = record
                 op1, op2: char;
                 nr1, nr2: integer;
               end;
       rys_mas = array [1..MAX_R] of rysys;

  { þemiau apraðyti globalieji kintamieji }
  var el_sar: elem_sar;

  function skaiciuoti (ind: integer): integer;
    { rekursinë procedura, skaièiuojanti elemento iðëjimo reikðmæ }
     var r1, r2: integer;
  begin
    if el_sar[ind].r=-1 { jei reikðmë neþinoma }
       then begin
              r1 := skaiciuoti (el_sar[ind].in1);
              r2 := skaiciuoti (el_sar[ind].in2);
              case el_sar[ind].op of
              'A': if r1+r2=0
                      then el_sar[ind].r := 1
                      else el_sar[ind].r := 0;
              'I': if r1*r2 = 1
                      then el_sar[ind].r := 0
                      else el_sar[ind].r := 1;
              'G': el_sar[ind].r := r1;
              end;
            end;
      skaiciuoti := el_sar[ind].r;
   end; { skaièiuoti }

  procedure parengti (Rm: rys_mas; R: integer; { ryðiai, suraðyti á simboliø masyvus }
                      Pdv: string; { áëjimø reikðmës }
                      var el_sar: elem_sar);
   { parengia duomenis, sukuria elementø masyvà }
    var tarpas, i, ind, nm1: integer;
  begin
    el_sar[0].r := 1; { fiktyvus elementas }
    for i := 1 to MAX_E do
     begin
       el_sar[i].r := -1; { elementu reikðmës neþinomos }
       el_sar[i].in1 := 0;
       el_sar[i].in2 := 0;
     end;
    for i := 1 to R do
      begin
        with Rm[i] do
          begin
            if (op1 = 'P') and (el_sar[nr1].r = -1)
               then if Pdv[nr1] = '0' { P elementø reikðmës þinomos }
                    then el_sar[nr1].r := 0
                    else el_sar[nr1].r := 1;
            case op2 of { apskaièiuojame eelemento eilës numerá masyve }
              'A': ind := MP+nr2;
              'I': ind := MP+MA+nr2;
              'G': ind := MP+MA+MI+nr2;
            end;
            case op1 of { apskaièiuojame eelemento eilës numerá masyve }
              'A': nm1 := MP+nr1;
              'I': nm1 := MP+MA+nr1;
              'G': nm1 := MP+MA+MI+nr1;
              'P': nm1 := nr1;
            end;
            el_sar[ind].op := op2;
            if el_sar[ind].in1 = 0
               then el_sar[ind].in1 := nm1
               else el_sar[ind].in2 := nm1
        end;
      end;
  end; { parengti }

  procedure skaityti (var P, G, R: integer; var Pdv: string;
                      var Rm: rys_mas);
    var f: text;
        i: integer;
        tarpas: char;
  begin
    assign (f, PR);
    reset (f);
    readln (f, P, G);
    readln (f, Pdv);
    readln (f, R);
    for i := 1 to R do
        readln (f, Rm[i].op1, Rm[i].nr1, tarpas, Rm[i].op2, Rm[i].nr2);
    close (f);
  end;  { skaityti }

  procedure spausdinti (ind, G: integer);
    var f: text;
        i: integer;
  begin
    assign (f, RZ);
    rewrite (f);
    for i := ind+1 to ind+G do
      write (f, el_sar[i].r);
    writeln (f); 
    writeln (f); 
    close (f);
    
  end; { spausdinti }

  var Rm: rys_mas;
      ind, i, R, P, G: integer;
      Pdv: string;
begin
  skaityti (P, G, R, Pdv, Rm);
  parengti (Rm, R, Pdv, el_sar);
  ind := MP+MA+MI;
  for i := ind+1 to ind + G do { skaièiuojamos visø iðëjimo elementø reikðmës }
    skaiciuoti(i);
  spausdinti(ind, G);
end.