UVA 12546-LCM Pair Sum
#include<bits/stdc++.h>
#define me(a,x) memset(a,x,sizeof(a))
#define sc scanf
#define itn int
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define STR clock_t startTime = clock();
#define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl;
using namespace std;
const int N=105;
const long long mod=1e9+7;
const long long mod2=998244353;
const int oo=0x7fffffff;
const int sup=0x80000000;
typedef long long ll;
typedef unsigned long long ull;
template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");}
template <typename it>
string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;}
template <typename it>int o(it a){cout<<a<<endl;return 0;}
inline ll mul_64(ll x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;}
ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=(a/b)*x;}
const int inv2=mod+1>>1;
ll f[N];
int main(){
int t;
cin>>t;
for(int cas=1;cas<=t;cas++){
int c;sc("%d",&c);
ll n=1;ll ans=1;
for(int i=1;i<=c;i++){
int p,a;
sc("%d%d",&p,&a);
n=n*ksm(p,a,mod)%mod;
if(a==1)f[i]=4LL*p+2;
else{
f[i]=2;
f[i]+=2LL*(a+1)%mod*ksm(p,a,mod)%mod+2LL*ksm(p-1,mod-2,mod)%mod*p%mod*(ksm(p,a-1,mod)-1)%mod;
f[i]=(f[i]%mod+mod)%mod;
}
}
for(int i=1;i<=c;i++){
ans=ans*f[i]%mod;
}
ans=ans*ksm(ksm(2,c,mod),mod-2,mod)%mod;
ans+=n;
printf("Case %d: %lld\n",cas,(ans+mod)%mod);
}
}