1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int maxn=1000010+5;
6 int c[maxn];
7 int n,m,k;
8 struct pos
9 {
10 int e,w;
11 bool operator <(const pos &a) const
12 {
13 if(a.w==w) return e>a.e;
14 return w>a.w;
15 }
16 }p[maxn];
17 int lowbit(int x)
18 {
19 return x&-x;
20 }
21 int sum(int x)
22 {
23 int ret=0;
24 while(x>0)
25 {
26 ret+=c[x];
27 x-=lowbit(x);
28 }
29 return ret;
30 }
31 void add(int x,int inc)
32 {
33 while(x<=n)
34 {
35 c[x]+=inc;
36 x+=lowbit(x);
37 }
38 }
39 int main()
40 {
41 int t;
42 scanf("%d",&t);
43 for(int CASE=1;CASE<=t;CASE++)
44 {
45 scanf("%d%d%d",&n,&m,&k);
46 for(int i=0;i<k;i++)
47 scanf("%d%d",&p[i].e,&p[i].w);
48 sort(p,p+k);
49 __int64 ans=0;
50 memset(c,0,sizeof(c));
51 int tmp=sum(p[0].e);
52 ans+=sum(p[0].e);
53 add(p[0].e,1);
54 for(int i=1;i<k;i++)
55 {
56 if(p[i].e==p[i-1].e)
57 ans+=tmp;
58 else
59 {
60 tmp=sum(p[i].e-1);
61 ans+=tmp;
62 }
63 add(p[i].e,1);
64 }
65 printf("Test case %d: %I64d\n",CASE,ans);
66 }
67 return 0;
68 }