def fs(s1,p1,hs) res=0 res+=hs["0"] if hs.member?("0") return res if s1.size<=p1 e=s1[p1] if hs.member?(e) then res+=fs(s1,p1+1,hs[e]) end return res end def f(s1,p1,hs) if s1.size<=p1 then hs["0"]=0 if hs.member?("0")==false hs["0"]+=1 else e=s1[p1] if hs.member?(e) then f(s1,p1+1,hs[e]) else hs[e]={} f(s1,p1+1,hs[e]) end end end s=gets.chomp.split("") hs={} gets.to_i.times{ s2=gets.chomp f(s2,0,hs) } ans=0 s.size.times{|p1| ans+=fs(s,p1,hs) } puts ans