#include<iostream>
#include<cstdio>
#include<cstdlib>
typedef long long LL;
using namespace std;
LL pow(LL a,LL p,LL m){
LL d=1,t=a;
while(p>0){
if(p&1==1){
d=(d*t)%m;
}
t=(t*t)%m;
p=p>>1;
}
return d;
}
int main()
{
int c,m,h,a,p;
LL sum;
scanf("%d",&c);
while(c--){
sum=0;
scanf("%d%d",&m,&h);
for(int i=1;i<=h;i++){
scanf("%d%d",&a,&p);
sum=(sum+pow(a,p,m))%m;
}
printf("%d\n",sum);
}
return 0;
}
poj1995
最新推荐文章于 2019-02-28 22:40:31 发布