1
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5
6 struct node
7 {
8 int x;
9 int f;
10 int c;
11 }a[101];
12
13 bool cmp(node x,node y)
14 {
15 return x.c<y.c;
16 }
17
18 int main()
19 {
20 int i,j;
21 int K,E,N;
22 int m;
23 cin>>m;
24 for(i = 0;i<m;++i)
25 {
26 cin>>K>>E>>N;
27 for(j = 0;j<N;++j)
28 {
29 cin>>a[j].x>>a[j].f>>a[j].c;
30 a[j].c+=(E - a[j].x);
31 }
32
33 sort(a,a+N,cmp);
34 int sum = 0;
35 for(j= 0;j<N;++j)
36 {
37 if(K==0)break;
38 if(K<=a[j].f)
39 {
40 sum += K * a[j].c;
41 break;
42 }
43 else if(K>a[j].f)
44 {
45 sum += a[j].f * a[j].c;
46 K -= a[j].f;
47 }
48 }
49 cout<<sum<<endl;
50 }
51 // system("pause");
52 return 0;
53 }
54
55
56
57
58
59