传送门:http://poj.org/problem?id=1995
#include<queue>
#include<stack>
#include<cmath>
#include<cstring>
#include <cstdio>
#include<iostream>
#define ll long long
using namespace std;
int pow(ll x,ll y,ll m)
{
ll ans=1;
for(;y!=0;y>>=1)
{
if(y&1) ans=(ans*x)%m;
x=(x*x)%m;
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ll ans=0;
ll m,n,x,y;
cin>>m>>n;
for(int i=0;i<n;i++)
{
cin>>x>>y;
ans=(ans+pow(x,y,m))%m;
}
cout<<ans<<endl;
}
return 0;
}