program vigdecip(input,output,infile,outfile);

type
  nodepointer = ^nodetype;
  nodetype    = record
                  ch   :char;
                  next :nodepointer;
                end;
 
var
  firstptr       :nodepointer;
  currentptr     :nodepointer;
  keylength      :integer;
  count          :integer;
  shift          :integer;
  ch             :char;
  infile         :text;
  outfile        :text;
  


procedure getkeyword(var fptr:nodepointer; var keysize:integer);
var
  cptr           :nodepointer;

begin
  writeln('Please enter the vigenere keyword..');
  new(fptr);
  cptr:=fptr;
  keysize:=0;
  while not eoln do
  begin
    read(cptr^.ch);
    keysize:=keysize + 1;
    new(cptr^.next);
    cptr:=cptr^.next;
  end;
end;

procedure shiftletter(chin:char;shift:integer;var chout:char);
var
  capital    :set of char;
  small      :set of char;
 
begin
  capital:= ['A'..'Z'];
  small  := ['a'..'z'];
  if (( chin in capital) and ( (ord(chin) + shift) > ord('Z'))) or
     (( chin in small)   and ( (ord(chin) + shift) > ord('z'))) then
      chout:= chr( ord(chin) - 26 + shift) else
    if (( chin in capital) and ( (ord(chin) + shift) < ord('A'))) or
       (( chin in small)   and ( (ord(chin) + shift) < ord('a'))) then
        chout:= chr( ord(chin) + 26 + shift) else
          chout:=chr( ord(chin) + shift);
end;
  
    
begin
  getkeyword(firstptr,keylength);
  count:=0;
  currentptr:=firstptr;
  reset(infile);
  rewrite(outfile);
  while not eof(infile) do
  begin
    read(infile,ch);
    if ch in ['A'..'Z', 'a'..'z'] then
    begin
      if currentptr^.ch in ['a'..'z'] then 
        shift:= -(ord(currentptr^.ch) - ord('a') + 1)
      else shift:= -(ord(currentptr^.ch) - ord('A') + 1);
      shiftletter(ch,shift,ch);
      count:=count + 1;
      if count=keylength then 
      begin
        count:=0;
        currentptr:=firstptr;
      end 
      else currentptr:=currentptr^.next;
    end;
    write(outfile,ch);
  end;
    
  
end.

