import java.io.IOException;
import java.util.Scanner;
public class Main
{
static int N=1010;
static int[] st=new int[N];
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int Q = scan.nextInt();
for(int i=1;i<=n;i++)
{
int a=0,b=0,c=0,x=0;
String s = scan.next();
if(s.contains("+"))
{
String[] sp1 = s.split("\\+");
if(sp1[0].length()==1)
{
a=1;
}
else
{
a= Integer.parseInt(sp1[0].substring(0,sp1[0].length()-1));
}
String[] sp2 = sp1[1].split("=");
b= Integer.parseInt(sp2[0]);
c= Integer.parseInt(sp2[1]);
x=(c-b)/a;
st[x]++;
}
else if(s.contains("-"))
{
String[] sp1 = s.split("\\-");
if(sp1[0].length()==1)
{
a=1;
}
else
{
a= Integer.parseInt(sp1[0].substring(0,sp1[0].length()-1));
}
String[] sp2 = sp1[1].split("=");
b= -Integer.parseInt(sp2[0]);
c= Integer.parseInt(sp2[1]);
x=(c-b)/a;
st[x]++;
}
}
while(Q-->0)
{
int res=0;
int L=scan.nextInt();
int R=scan.nextInt();
for(int i=L;i<=R;i++)
{
if(st[i]!=0)
{
res++;
}
}
System.out.println(res);
}
}
}