Program fantscrin;
const n = 100;
Var a: array [1..n] of integer;
i,j,k,s:integer;
Begin
k:=0;
for i:=1 to n do
begin
readln(a[i]);
if (a[i] = 0) then break;
k:=k+1;
end;
for i:=1 to k do
write(a[i], ' ');
for i:=1 to k do
if ((a[i] mod 10 = 3)and(a[i] mod 7 = 0)) then s:=s+a[i];
writeln('');
writeln(s);
End.