大意不再赘述。
思路:简单的快速幂算法。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
LL a, b, m, h;
LL pow_mod(LL a, LL b, LL m)
{
LL ans = 1;
while(b)
{
if(b & 1)
{
ans = ans*a % m;
}
a = a*a % m;
b /= 2;
}
return ans % m;
}
void solve()
{
scanf("%lld", &m);
scanf("%lld", &h);
LL ans = 0;
while(h--)
{
scanf("%lld%lld", &a, &b);
ans = (ans + pow_mod(a, b, m)) % m;
}
printf("%lld\n", ans);
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
solve();
}
return 0;
}