#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll;
#define mem(name,value) memset(name,value,sizeof(name))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=10010,inf=0x3f3f3f3f;
int a[maxn][maxn];
int n,m;
int x,y;
int main()
{
fread((char*)&n, sizeof(int), 1, stdin);
fread((char*)&m, sizeof(int), 1, stdin);
for(int i = 0; i < n; ++i) {
fread((char*)(a + i), sizeof(int), m, stdin);
}
int ans,l,r;
while(true){
fread((char*)&x, sizeof(int), 1, stdin);
fread((char*)&y, sizeof(int), 1, stdin);
if (feof(stdin)) break;
ans=0;
for(int i=0;i<n;i++){
l=lower_bound(a[i],a[i]+m,x)-a[i];
r=upper_bound(a[i],a[i]+m,y)-a[i];
ans+=r-l;
}
printf("%d\n",ans);
}
}