{$apptype console}
{$o-,r+,q+}
uses
  SysUtils, TestLib;

type
  int = longint;

var
  str: string;
  x, m, l, v: int;
  hash, pow, i: int;

begin
  str := ouf.readWord(blanks, blanks);
  x := inf.readInteger;
  m := inf.readInteger;
  l := inf.readInteger;
  v := inf.readInteger;
  if (length(str) <> l) then
  begin
    quit(_wa, 'Expected string length: ' + inttostr(l) + ', found: ' + inttostr(length(str)));
  end;
  hash := 0;
  pow := 1;
  for i := 1 to l do
  begin
    if (str[i] < '0') or (str[i] > '9') then
    begin
      quit(_wa, 'Unexpected ' + inttostr(i) + '-th symbol: ''' + str[i] + ''' was found instead of a digit');
    end;
    hash := (hash + (ord(str[i]) - ord('0')) * pow) mod m;
    pow := (pow * x) mod m;
  end;
  if (hash <> v) then
  begin
    quit(_wa, 'Expected hash: ' + inttostr(v) + ', found hash: ' + inttostr(hash));
  end;
  quit(_ok, 'x = ' + inttostr(x) + ', m = ' + inttostr(m) + ', l = ' + inttostr(l) + ', v = ' + inttostr(v));
end.
