def g;gets.split.map &:to_i;end
while(x,y,z=g;x>0)
v=g
e=[]
r=(1..60).map{Hash.new{0}}
r[0][0]=1
z.times{n,*a=g;e[n]=a}
y.times{|i|o=d=0
t,u=e[i]
t&&(t<2?(o=u):(d=t<3?u:-u))
r[i].map{|k,w|k+=d;v.map{|j|r[i+j+o][k<0?0:k]+=w.quo x}}
}
p r[y..-1].flat_map{|h|h.map{|k,w|k*w}}.inject(:+).to_i
end