#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
map<pll, int> mp;
pll Arr[20];
int n;
void solve(ll a, ll b, int i){
if(i == n){
ll mn = min(a, b), mx = max(a, b);
mp[pll(mn, mx)]++;
}else{
ll sz = Arr[i].second, p = Arr[i].first, _a, _b;
_a = ((a % mod) * ((ll)pow(p, sz)) % mod) % mod;
for(ll j = 0; j <= sz; j++){
_b = ((b % mod) * ((ll)pow(p, j)) % mod) % mod;
solve(_a, _b, i + 1);
}
_b = ((b % mod) * ((ll)pow(p, sz)) % mod) % mod;
for(ll j = 0; j <= sz; j++){
_a = ((a % mod) * ((ll)pow(p, j)) % mod) % mod;
solve(_a, _b, i + 1);
}
}
}
void solve(int i){
ll sum = 0;
solve(1LL, 1LL, 0);
map<pll, int>:: iterator it;
for(it = mp.begin(); it != mp.end(); it++){
pll pr = it->first;
sum = (sum + (pr.first + pr.second) % mod) % mod;
}
printf("Case %d: %lld\n", i, sum);
}
int main(){
int t;
ll p, cnt;
scanf("%d", &t);
for(int i = 1; i <= t; i++){
scanf("%d", &n);
for(int j = 0; j < n; j++){
scanf("%lld %lld", &p, &cnt);
Arr[j] = pll(p, cnt);
}
mp.clear();
solve(i);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+IAp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBzaWduZWQgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHBhaXI8bGwsIGxsPiBwbGw7CmNvbnN0IGxsIG1vZCA9IDFlOSArIDc7Cm1hcDxwbGwsIGludD4gbXA7CnBsbCBBcnJbMjBdOwppbnQgbjsKCnZvaWQgc29sdmUobGwgYSwgbGwgYiwgaW50IGkpewoJaWYoaSA9PSBuKXsKCQlsbCBtbiA9IG1pbihhLCBiKSwgbXggPSBtYXgoYSwgYik7CgkJbXBbcGxsKG1uLCBteCldKys7Cgl9ZWxzZXsKCQlsbCBzeiA9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