#include <iostream>
#include <cstdio>
#include <cstring>
using namespacestd;
const int maxn =45;
const int maxk =10;
int g[maxn];
int a[maxk],b[maxk];//学分为a的课有b门
int main()
{
int T;
cin >> T;
while (T --) {
memset(g,0,sizeof(g));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int n,k;
scanf("%d%d",&n,&k);
for (int i =1; i <= k; i ++) {
scanf("%d%d",&a[i],&b[i]);
}
g[0] =1;
for (int i =1; i <= k; i ++) {
for (int j = n; j >=a[i]; j --) {
for(int x =1;x <= b[i];x ++){//x从一开始,如果为0,则没有选第i个,dp转移时需要考虑,但是对于整体来说,没有选第i个,是同一可能
if (j - x *a[i] >= 0) {
g[j] +=g[j - x * a[i]];
}
}
}
}
printf("%d\n",g[n]);
}
return0;
}