#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL Base=134341;
const LL mod=1e9+7;
const int N=1e5+100;
const LL Mod=998244353;
inline void read(int &x){
	x=0;
	int f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	x*=f;
}
struct Tree{
	struct Front_Star{
		int u,v,nxt;
	}e[N<<2];
	int cnt=0;
	int first[N];
	void add(int u,int v){
		++cnt;
		e[cnt].u=u;
		e[cnt].v=v;
		e[cnt].nxt=first[u];
		first[u]=cnt;
	}
}At,Bt;
LL Fac[N];
LL Inv[N];
LL Quick_Pow(LL x,int k){
	LL ret=1;
	while(k){
		if(k&1)ret=ret*x%mod;
		x=x*x%mod;
		k>>=1;
	}
	return ret;
}
void Pre(){
	Fac[0]=1;
	for(int i=1;i<N;++i)Fac[i]=Fac[i-1]*i%mod;
	Inv[N-1]=Quick_Pow(Fac[N-1],mod-2);
	for(int i=N-2;i>=0;--i)Inv[i]=Inv[i+1]*(i+1)%mod;
}
LL FB[15];
int maxdep[15];
vector<int>Son[15];
ULL Hash(int u,int fat){
	vector<pair<int,ULL> >A;
	maxdep[u]=1;
	for(int i=Bt.first[u];i;i=Bt.e[i].nxt){
		int v=Bt.e[i].v;
		if(v==fat)continue;
		A.push_back(make_pair(maxdep[v],Hash(v,u)));
		Son[u].push_back(v);
		maxdep[u]+=maxdep[v];
	}
	sort(A.begin(),A.end());
	FB[u]=1;
	int tot=1;
	for(int i=1;i<A.size();++i){
		if(A[i]!=A[i-1]){
			FB[u]=FB[u]*Inv[tot]%mod;
			tot=1;
		}
		else ++tot;
	}
	FB[u]=FB[u]*Inv[tot]%mod;
	ULL ret='(';
	for(int i=0;i<A.size();++i)ret=(ret*Base+A[i].second)%Mod;//,cout<<A[i].first<<" "<<A[i].second<<" ";
//	cout<<u<<"::"<<ret<<'\n';
//	cout<<'\n';
	return (ret*666233+')')%Mod;
}
LL F[3000][15];
int n,m,rt;
set<ULL>S;

LL Get(int u,int pre,vector<int> Now){
	LL G[2][5000]={};
	memset(G,0,sizeof(G));
	int cur=0;
	int Mx=(1<<Son[pre].size())-1;
	G[1][0]=1;
	for(int i=0;i<Now.size();++i){
		cur^=1;
		memcpy(G[cur^1],G[cur],sizeof(G[cur^1]));
//		memset(G[cur^1],0,sizeof(G[cur^1]));
//		cout<<"G01 "<<G[1][0]<<" "<<cur<<" ";
		for(int l=0;l<=Mx;++l){
			for(int j=0;j<Son[pre].size();++j){
				if(!(l&(1<<j))){
//					cout<<G[cur^1][l|(1<<j)]<<" ";
					G[cur^1][l|(1<<j)]=(G[cur^1][l|(1<<j)]+F[Now[i]][Son[pre][j]]*G[cur][l]%mod)%mod;
//					cout<<Now[i]<<" "<<Son[pre][j]<<" "<<G[cur^1][l|(1<<j)]<<"\n";
				}
			}
		}
	}
//	cout<<G[cur^1][Mx]<<" "<<Mx<<" "<<FB[pre]<<'\n';
	return G[cur^1][Mx]*FB[pre]%mod;
}
LL Solve(int u,int fat){
	for(int i=1;i<=m;++i)if(!Son[i].size())F[u][i]=1;
	LL ret=0;
	vector<int>Now;
	Now.clear();
	for(int i=At.first[u];i;i=At.e[i].nxt){
		int v=At.e[i].v;
		if(v==fat)continue;
		ret=(ret+Solve(v,u))%mod;
		Now.push_back(v);
	}
	for(int i=1;i<=m;++i){
		if(Son[i].size()){

			F[u][i]=Get(u,i,Now);
//			cout<<F[u][i]<<'\n';
//			cout<<u<<" "<<i<<" "<<F[u][i]<<'\n';
//			cout<<"==========\n";
//			if(u==3&&i==2){
//				exit(0);
//			}
		}
	}
//	cout<<'\n';
	return (ret+F[u][rt])%mod;
}
LL Calc(int rt){
	memset(F,0,sizeof(F));
	return Solve(1,0);
}
int main(){
	Pre();
//	freopen("tree.in","r",stdin);
	read(n);
	for(int i=1;i<n;++i){
		int u,v;
		read(u);read(v);
		At.add(u,v);
		At.add(v,u);
	}
	read(m);
	for(int i=1;i<m;++i){
		int u,v;
		read(u);read(v);
		Bt.add(u,v);
		Bt.add(v,u);
	}
	LL ans=0;
	for(rt=1;rt<=m;++rt){
		for(int j=1;j<=m;++j)Son[j].clear();
		ULL HashB=Hash(rt,0);
//		cout<<HashB<<"\n";
		if(S.count(HashB))continue;
		S.insert(HashB);
		ans=(ans+Calc(rt))%mod;
//		cout<<ans<<" ";
//		cout<<rt<<" \n";
//		exit(0);
	}
	cout<<(ans+mod)%mod;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a),ed=(b);i<=ed;++i)
#define pb(x) push_back((x))
typedef long long ll;
typedef map<int,vector<int> >::iterator mit;
inline int read(){
	register int x=0,f=1;register char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
	while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}
	return f?x:-x;
}

const int N=1e3+10,M=15,mod=1e9+7;
int n,m,num1,num2,head1[N],head2[M],ans;
map<int,vector<int> > _list;
map<int,int> f[N];
struct edge{int to,next;}e1[N<<1],e2[M<<1];
inline void add1(int u,int v){e1[++num1]=(edge){v,head1[u]},head1[u]=num1;}
inline void add2(int u,int v){e2[++num2]=(edge){v,head2[u]},head2[u]=num2;}
inline void pls(int &x,int y){x=x+y>=mod?x+y-mod:x+y;}
inline int length(int x){return 32-__builtin_clz(x);}
inline void combine(int &x,int y){x=x<<length(y)|y;}

inline int Cal(int u,int pa){
	int s=1;vector<int> nw;
	for(int i=head2[u];i;i=e2[i].next){
		if(e2[i].to==pa)continue;
		nw.pb(Cal(e2[i].to,u));
	}
	sort(nw.begin(),nw.end());
	REP(i,0,nw.size()-1)combine(s,nw[i]);
	s<<=1;
	if(!_list.count(s))_list[s]=nw;
	return s;
}

inline void Dfs(int u,int pa){
	static int g[2][1<<M];
	vector<int> son;
	for(int i=head1[u];i;i=e1[i].next){
		if(e1[i].to==pa)continue;
		son.pb(e1[i].to);Dfs(e1[i].to,u);
	}
	for(mit it=_list.begin();it!=_list.end();++it){
		vector<int> nw=(*it).second;
		int s=(*it).first,siz=nw.size(),all=(1<<siz)-1;
		memset(g[0],0,sizeof(g[0]));g[0][0]=1;
		REP(i,0,son.size()-1){
			memcpy(g[1],g[0],sizeof(g[0]));
			for(int j=0;j<siz;++j){
				int val=f[son[i]][nw[j]];
				if(!val)continue;
				for(int k=all;~k;--k)
					if(g[0][k]&&!((k>>j)&1)&&(!j||nw[j]^nw[j-1]||(k>>(j-1))&1))
						pls(g[1][k|(1<<j)],1ll*g[0][k]*val%mod);
			}
			memcpy(g[0],g[1],sizeof(g[1]));
		}
		if(g[0][all]){
			f[u][s]=g[0][all];
			if(length(s)==m+m)pls(ans,g[0][all]);
		}
	}
}

int main(){
	n=read();
	REP(i,2,n){int u=read(),v=read();add1(u,v),add1(v,u);}
	m=read();
	REP(i,2,m){int u=read(),v=read();add2(u,v),add2(v,u);}
	REP(i,1,m)Cal(i,0);
	Dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a),ed=(b);i<=ed;++i)
#define pb(x) push_back((x))
typedef long long ll;
typedef map<int,vector<int> >::iterator mit;
inline int read(){
	register int x=0,f=1;register char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
	while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}
	return f?x:-x;
}

const int N=1e3+10,M=15,mod=1e9+7;
int n,m,num1,num2,head1[N],head2[M],ans;
map<int,vector<int> > _list;
map<int,int> f[N];
struct edge{int to,next;}e1[N<<1],e2[M<<1];
inline void add1(int u,int v){e1[++num1]=(edge){v,head1[u]},head1[u]=num1;}
inline void add2(int u,int v){e2[++num2]=(edge){v,head2[u]},head2[u]=num2;}
inline void pls(int &x,int y){x=x+y>=mod?x+y-mod:x+y;}
inline int length(int x){return 32-__builtin_clz(x);}
inline void combine(int &x,int y){x=x<<length(y)|y;}

inline int Cal(int u,int pa){
	int s=1;vector<int> nw;
	for(int i=head2[u];i;i=e2[i].next){
		if(e2[i].to==pa)continue;
		nw.pb(Cal(e2[i].to,u));
	}
	sort(nw.begin(),nw.end());
	REP(i,0,nw.size()-1)combine(s,nw[i]);
	s<<=1;
	if(!_list.count(s))_list[s]=nw;
	return s;
}

inline void Dfs(int u,int pa){
	static int g[2][1<<M];
	vector<int> son;
	for(int i=head1[u];i;i=e1[i].next){
		if(e1[i].to==pa)continue;
		son.pb(e1[i].to);Dfs(e1[i].to,u);
	}
	for(mit it=_list.begin();it!=_list.end();++it){
		vector<int> nw=(*it).second;
		int s=(*it).first,siz=nw.size(),all=(1<<siz)-1;
		memset(g[0],0,sizeof(g[0]));g[0][0]=1;
		REP(i,0,son.size()-1){
			memcpy(g[1],g[0],sizeof(g[0]));
			for(int j=0;j<siz;++j){
				int val=f[son[i]][nw[j]];
				if(!val)continue;
				for(int k=all;~k;--k)
					if(g[0][k]&&!((k>>j)&1)&&(!j||nw[j]^nw[j-1]||(k>>(j-1))&1))
						pls(g[1][k|(1<<j)],1ll*g[0][k]*val%mod);
			}
			memcpy(g[0],g[1],sizeof(g[1]));
		}
		if(g[0][all]){
			f[u][s]=g[0][all];
			if(length(s)==m+m)pls(ans,g[0][all]);
		}
	}
}

int main(){
	n=read(),m=read();
	REP(i,2,n){int u=read(),v=read();add1(u,v),add1(v,u);}
	REP(i,2,m){int u=read(),v=read();add2(u,v),add2(v,u);}
	REP(i,1,m)Cal(i,0);
	Dfs(1,0);
	printf("%d\n",ans);
	return 0;
}


#include <cstring>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
inline int read(int f = 1, int x = 0, char  ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e3+5, M = 13, S = 1<<12|5, P = 1e9+7;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int x, int y) { _add(x, y), _add(y, x); }
int n, m, e[M], lg2[S], f[N][M][S], g[M][S], ans, vis[N], ifac[M];
int64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
void dfs(int x, int fa)
{
    for(int i = 0; i < m; ++i) f[x][i][1<<i] = 1;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == fa) continue;
        dfs(y, x);
        for(int p = 0; p < m; ++p)
            for(int s = 0; s < 1<<m; ++s)
                g[p][s] = f[x][p][s];
        for(int p = 0, a = (1<<m)-1; p < m; ++p)
            for(int s = 1; s < a; ++s)
                if(f[y][p][s])
                    for(int u = e[p]; u; u -= u&-u)
                    {
                        int q = lg2[u&-u];
                        for(int t = s^a, o = s^a; t; t = (t-1)&o)
                            f[x][q][s|t] = (f[x][q][s|t]+1ll*g[q][t]*f[y][p][s]%P)%P;
                    }
    }
    for(int i = 0; i < m; ++i) ans = (ans+f[x][i][(1<<m)-1])%P;
}
const uint64 B = time((time_t*)head) % 100000000 + 100000000;
const uint64 R =  time((time_t*)f) % 100000000 + 100000000;
uint64 h[N];
uint64 build(int x, int fa)
{
	uint64 res = R; vector<uint64> s;
    for(int u = e[x]; u; u -= u&-u)
    {
        int y = lg2[u&-u]; if(y == fa) continue;
        s.push_back(build(y, x));
    }
	sort(s.begin() , s.end());
	for(int i = 0 ; i < s.size() ; ++i) res = res*B+s[i];
	return res;
}
int main()
{
    lg2[0] = -1, ifac[0] = 1, n = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        add(x, y);
    }
    m = read();
    for(int i = 1; i < m; ++i)
    {
        int x = read()-1, y = read()-1;
        e[x] |= 1<<y, e[y] |= 1<<x;
    }
    for(int i = 1; i < 1<<m; ++i) lg2[i] = lg2[i>>1]+1;
    for(int i = 1; i <= m; ++i) ifac[i] = ifac[i-1]*inv(i)%P;
    for(int i = 0; i < m; ++i)
    {
        if(e[i] != (e[i]&-e[i])) continue;
        h[i] = build(i, i), vis[i] = 1;
        for(int j = 0; j < i; ++j) if(h[i] == h[j]) vis[i] = 0, ++vis[j];
    }
    dfs(1, 0);
    for(int i = 0; i < m; ++i) ans = 1ll*ans*ifac[vis[i]]%P;
    printf("%d\n", ans);
    return 0;
}


#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int64;
inline int read(int f = 1, int x = 0, char  ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e3+5, M = 13, S = 1<<12|5, P = 1e9+7;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N];
int tot, hs[N], ht[N];
void _add(int *head, int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int *head, int x, int y) { _add(head, x, y), _add(head, y, x); }
int n, m, e[M], lg2[S], f[N][M][S], g[M][S], ans, res;
int64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
void dfs(int x, int fa, int &ans, int *head)
{
    for(int i = 0; i < m; ++i) f[x][i][1<<i] = 1;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == fa) continue;
        dfs(y, x, ans, head);
        for(int p = 0; p < m; ++p)
            for(int s = 0; s < 1<<m; ++s)
                g[p][s] = f[x][p][s];
        for(int p = 0, a = (1<<m)-1; p < m; ++p)
            for(int s = 1; s < a; ++s)
                if(f[y][p][s])
                    for(int u = e[p]; u; u -= u&-u)
                    {
                        int q = lg2[u&-u];
                        for(int t = s^a, o = s^a; t; t = (t-1)&o)
                            f[x][q][s|t] = (f[x][q][s|t]+1ll*g[q][t]*f[y][p][s]%P)%P;
                    }
    }
    for(int i = 0; i < m; ++i) ans = (ans+f[x][i][(1<<m)-1])%P;
}
int main()
{
    lg2[0] = -1, n = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        add(hs, x, y);
    }
    m = read();
    for(int i = 1; i < m; ++i)
    {
        int x = read()-1, y = read()-1;
        e[x] |= 1<<y, e[y] |= 1<<x, add(ht, x+1, y+1);
    }
    for(int i = 1; i < 1<<m; ++i) lg2[i] = lg2[i>>1]+1;
    dfs(1, 0, ans, hs), memset(f, 0, sizeof(f)), dfs(1, 0, res, ht);
    printf("%lld\n", ans*inv(res)%P);
    return 0;
}


#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int64;
inline int read(int f = 1, int x = 0, char  ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e3+5, M = 13, S = 1<<12|5, P = 1e9+7;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[(N+M)<<1];
int tot, hs[N], ht[N];
void _add(int *head, int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int *head, int x, int y) { _add(head, x, y), _add(head, y, x); }
int n, m, e[M], lg2[S], f[N][M][S], g[M][S], ans, res;
int64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
void dfs(int x, int fa, int &ans, int *head)
{
    for(int i = 0; i < m; ++i) f[x][i][1<<i] = 1;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == fa) continue;
        dfs(y, x, ans, head);
        for(int p = 0; p < m; ++p)
            for(int s = 0; s < 1<<m; ++s)
                g[p][s] = f[x][p][s];
        for(int p = 0, a = (1<<m)-1; p < m; ++p)
            for(int s = 1; s < a; ++s)
                if(f[y][p][s])
                    for(int u = e[p]; u; u -= u&-u)
                    {
                        int q = lg2[u&-u];
                        for(int t = s^a, o = s^a; t; t = (t-1)&o)
                            f[x][q][s|t] = (f[x][q][s|t]+1ll*g[q][t]*f[y][p][s]%P)%P;
                    }
    }
    for(int i = 0; i < m; ++i) ans = (ans+f[x][i][(1<<m)-1])%P;
}
int main()
{
    lg2[0] = -1, n = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        add(hs, x, y);
    }
    m = read();
    for(int i = 1; i < m; ++i)
    {
        int x = read()-1, y = read()-1;
        e[x] |= 1<<y, e[y] |= 1<<x, add(ht, x+1, y+1);
    }
    for(int i = 1; i < 1<<m; ++i) lg2[i] = lg2[i>>1]+1;
    dfs(1, 0, ans, hs), memset(f, 0, sizeof(f)), dfs(1, 0, res, ht);
    printf("%lld\n", ans*inv(res)%P);
    return 0;
}


#include <cstring>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
inline int read(int f = 1, int x = 0, char  ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e3+5, M = 13, S = 1<<12|5, P = 1e9+7;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int x, int y) { _add(x, y), _add(y, x); }
int n, m, e[M], lg2[S], f[N][M][S], g[M][S], ans, vis[N], ifac[M];
int64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
void dfs(int x, int fa)
{
    for(int i = 0; i < m; ++i) f[x][i][1<<i] = 1;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == fa) continue;
        dfs(y, x);
        for(int p = 0; p < m; ++p)
            for(int s = 0; s < 1<<m; ++s)
                g[p][s] = f[x][p][s];
        for(int p = 0, a = (1<<m)-1; p < m; ++p)
            for(int s = 1; s < a; ++s)
                if(f[y][p][s])
                    for(int u = e[p]; u; u -= u&-u)
                    {
                        int q = lg2[u&-u];
                        for(int t = s^a, o = s^a; t; t = (t-1)&o)
                            f[x][q][s|t] = (f[x][q][s|t]+1ll*g[q][t]*f[y][p][s]%P)%P;
                    }
    }
    for(int i = 0; i < m; ++i) ans = (ans+f[x][i][(1<<m)-1])%P;
}
const uint64 B1 = time((time_t*)head) % 100000000 + 100000000;
const uint64 R =  time((time_t*)f) % 100000000 + 100000000;
uint64 h[N];
uint64 build(int x , int fa)
{
	uint64 res = R; vector<uint64> s;
    for(int u = e[x]; u; u -= u&-u)
    {
        int y = lg2[u&-u]; if(y == fa) continue;
        s.push_back(build(y, x));
    }
	sort(s.begin() , s.end());
	for(int i = 0 ; i < s.size() ; i ++) res = res*B1+s[i];
	return res;
}
int main()
{
    lg2[0] = -1, ifac[0] = 1, n = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        add(x, y);
    }
    m = read();
    for(int i = 1; i < m; ++i)
    {
        int x = read()-1, y = read()-1;
        e[x] |= 1<<y, e[y] |= 1<<x;
    }
    for(int i = 1; i < 1<<m; ++i) lg2[i] = lg2[i>>1]+1;
    for(int i = 1; i <= m; ++i) ifac[i] = ifac[i-1]*inv(i)%P;
    for(int i = 0; i < m; ++i)
    {
        h[i] = build(i, 0), vis[i] = 1;
        for(int j = 0; j < i; ++j) if(h[i] == h[j]) vis[i] = 0, ++vis[j];
    }
    dfs(1, 0);
    for(int i = 0; i < m; ++i) ans = 1ll*ans*ifac[vis[i]]%P;
    printf("%d\n", ans);
    return 0;
}


#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int64;
inline int read(int f = 1, int x = 0, char  ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e3+5, M = 13, S = 1<<12|5, P = 1e9+7, Q = 1e9+9;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }
void add(int x, int y) { _add(x, y), _add(y, x); }
int n, m, e[M], lg2[S], f[N][M][S], g[M][S], ans, vis[N], ifac[M]; pair<int64, int64> h[N];
int64 inv(int i) { return i == 1?1:P-(P/i)*inv(P%i)%P; }
void dfs(int x, int fa)
{
    for(int i = 0; i < m; ++i) f[x][i][1<<i] = 1;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == fa) continue;
        dfs(y, x);
        for(int p = 0; p < m; ++p)
            for(int s = 0; s < 1<<m; ++s)
                g[p][s] = f[x][p][s];
        for(int p = 0, a = (1<<m)-1; p < m; ++p)
            for(int s = 1; s < a; ++s)
                if(f[y][p][s])
                    for(int u = e[p]; u; u -= u&-u)
                    {
                        int q = lg2[u&-u];
                        for(int t = s^a, o = s^a; t; t = (t-1)&o)
                            f[x][q][s|t] = (f[x][q][s|t]+1ll*g[q][t]*f[y][p][s]%P)%P;
                    }
    }
    for(int i = 0; i < m; ++i) ans = (ans+f[x][i][(1<<m)-1])%P;
}
pair<int64, int64> build(int x, int fa)
{
    vector<pair<int64, int64> > s; int64 p = 0, q = 0;
    for(int u = e[x]; u; u -= u&-u)
    {
        int y = lg2[u&-u]; if(y == fa) continue;
        s.push_back(build(y, x));
    }
    sort(s.begin(), s.end());
    for(int i = 0; i < s.size(); ++i) p = (p*233%P+s[i].first)%P, q = (q*239%Q+s[i].second)%Q;
    return make_pair((p*2333%P+19260817)%P, (q*2339%Q+1+n+19491001)%Q);
}
int main()
{
    lg2[0] = -1, ifac[0] = 1, n = read();
    for(int i = 1; i < n; ++i)
    {
        int x = read(), y = read();
        add(x, y);
    }
    m = read();
    for(int i = 1; i < m; ++i)
    {
        int x = read()-1, y = read()-1;
        e[x] |= 1<<y, e[y] |= 1<<x;
    }
    for(int i = 1; i < 1<<m; ++i) lg2[i] = lg2[i>>1]+1;
    for(int i = 1; i <= m; ++i) ifac[i] = ifac[i-1]*inv(i)%P;
    for(int i = 0; i < m; ++i)
    {
        if(e[i] != (e[i]&-e[i])) continue;
        h[i] = build(i, i), vis[i] = 1;
        for(int j = 0; j < i; ++j) if(h[i] == h[j]) vis[i] = 0, ++vis[j];
    }
    dfs(1, 0); for(int i = 0; i < m; ++i) ans = 1ll*ans*ifac[vis[i]]%P;
    printf("%d\n", ans);
    return 0;
}


#include<bits/stdc++.h>
#define mo 1000000007
#define N 1005
using namespace std;
int f[N][(1<<12)+5];
struct edge{int to,next;}e[N*2];
struct Tree{
	int c[N][N],head[N];
	int sz[N],son[N],hsh[N];
	int n,tot;
	edge e[N*2];
	void add(int x,int y){
		e[++tot]=(edge){y,head[x]};
		head[x]=tot;
	}
	void init(){
		scanf("%d",&n);
		for (int i=2;i<=n;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y); add(y,x);
		}
	}
	void dfs(int x,int fa){
		son[x]=hsh[x]=0;
		for (int i=head[x];i;i=e[i].next)
			if (e[i].to!=fa){
				c[x][son[x]++]=e[i].to;
				dfs(e[i].to,x);
				hsh[x]|=1<<(e[i].to-1);
			}
	}
}a,b;
int dfs(int x,int y,int v){
	if (!y) return !v;
	int &ans=f[a.c[x][y-1]][v];
	if (ans) return ans-1;
	ans=dfs(x,y-1,v);
	for (int to=a.c[x][y-1],k=0;k<b.n;k++)
		if ((v>>k)&1)
			ans=(ans+1ll*dfs(x,y-1,v^(1<<k))*
						dfs(to,a.son[to],b.hsh[k+1]))%mo;
	return ans++;
}
int Inv(int x){
	return x<=1?x:1ll*(mo-mo/x)*Inv(mo%x)%mo;
}
int main(){
	a.init(); b.init();
	a.dfs(1,0);
	int ans=0,Ans=0;
	for (int i=1;i<=b.n;i++){
		b.dfs(i,0);
		for (int j=1;j<=a.n;j++)
			ans=(ans+dfs(j,a.son[j],b.hsh[i]))%mo;
		memset(f,0,sizeof(f));
	}
	a=b;
	a.dfs(1,0);
	for (int i=1;i<=b.n;i++){
		b.dfs(i,0);
		for (int j=1;j<=a.n;j++)
			Ans=(Ans+dfs(j,a.son[j],b.hsh[i]))%mo;
		memset(f,0,sizeof(f));
	}
	printf("%d",ans*1ll*Inv(Ans)%mo);
}


#include <bits/stdc++.h>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
#define PI 3.14159265358979323846
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

// Modular operations
const int MOD = 1E9 + 7;

int Add(int a, int b, int m = MOD){
  a += b;
  if(a >= m) a -= m;
  return a;
}

int Sub(int a, int b, int m = MOD){
  a -= b;
  if(a < 0) a += m;
  return a;
}

int AddTo(int& to, int w, int m = MOD){
  to += w;
  if(to >= m) to -= m;
  return to;
}

int SubTo(int& to, int w, int m = MOD){
  to -= w;
  if(to < 0) to += m;
  return to;
}

int Mult(int a, int b, int m = MOD){
  return (a * 1LL * b) % m;
}

int Pow(int a, ll deg, int m = MOD){
  int res = 1 % m;
  for(;deg > 0;deg >>= 1, a = Mult(a, a, m))
    if(deg & 1) res = Mult(res, a, m);
  return res;
}

int Oem(int to, int m = MOD){
  return Pow(to, m - 2, m);
}

int Div(int a, int b, int m = MOD){
  return Mult(a, Oem(b, m), m);
}

// Graph
typedef vector<vi> Graph;

int ns, nt;
Graph s, t;

void AddEdge(Graph& g, int f, int t){
  g[f].pb(t), g[t].pb(f);
}

void RemEdge(Graph& g, int f, int t){
  if(f == -1 || t == -1) return;
  g[f].erase(find(all(g[f]), t));
}

// Counting automorphisms
int na;
Graph a;

vi best_p, path;
void Dfs(int vr, int par){
  path.pb(vr);
  if(sz(path) > sz(best_p)) best_p = path;
  for(int nxt : t[vr])
    if(nxt != par) Dfs(nxt, vr);
  path.pop_back();
}

pii Center(){
  for(int i = 0;i < sz(t);++i) Dfs(i, -1);
  int m = sz(best_p) / 2;
  if(sz(best_p) & 1) return mp(best_p[m], best_p[m]);
  else return mp(best_p[m - 1], best_p[m]);
}

map<vi, int> names;
int Name(const vi& l){
  if(names.count(l)) return names[l];
  return names[l] = sz(names) - 1;
}

int iso = 1;
int fact[15];
int AutoDfs(int vr, int par = -1){
  vi ch;
  for(int nxt : a[vr])
    if(nxt != par)
      ch.pb(AutoDfs(nxt, vr));
  sort(all(ch));

  for(int i = 0;i < sz(ch);){
    int j = i;
    while(j < sz(ch) && ch[i] == ch[j]) ++j;
    iso = Mult(iso, fact[j - i]);
    i = j;
  }

  return Name(ch);
}

int Auto(){
  fact[0] = 1;
  for(int i = 1;i < 15;++i)
    fact[i] = Mult(i, fact[i - 1]);

  auto c = Center();

  a = t;
  int root = c.fi;
  if(c.fi != c.se){
    RemEdge(a, c.fi, c.se);
    RemEdge(a, c.se, c.fi);
    a.pb(vi());
    AddEdge(a, sz(a) - 1, c.fi);
    AddEdge(a, sz(a) - 1, c.se);
    root = sz(a) - 1;
  }

  AutoDfs(root);
  return iso;
}

// DP
Graph base, put;
int dp[1000][12];

void Orient(Graph& g, int vr, int par = -1){
  RemEdge(g, vr, par);
  for(int nxt : g[vr]) Orient(g, nxt, vr);
}

int Eval(int bvr, int pvr){
  if(dp[bvr][pvr] != -1) return dp[bvr][pvr];

  int c = sz(put[pvr]);
  vi cdp(1 << c, 0);
  cdp[0] = 1;

  for(int nxt : base[bvr]){
    for(int msk = (1 << c) - 1;msk > 0;--msk){
      for(int i = 0;i < c;++i)
        if(msk & (1 << i))
          AddTo(cdp[msk], Mult(cdp[msk ^ (1 << i)],
                               Eval(nxt, put[pvr][i])));
    }
  }

  return dp[bvr][pvr] = cdp[(1 << c) - 1];
}

int Count(int root){
  memset(dp, -1, sizeof dp);
  base = s, put = t;
  Orient(base, root);
  Orient(put, 0);
  return Eval(root, 0);
}

int main(){
  scanf("%d", &ns);
  s.resize(ns);
  for(int i = 1, x, y;i < ns;++i)
    scanf("%d%d", &x, &y),
    AddEdge(s, x - 1, y - 1);

  scanf("%d", &nt);
  t.resize(nt);
  for(int i = 1, x, y;i < nt;++i)
    scanf("%d%d", &x, &y),
    AddEdge(t, x - 1, y - 1);

  Auto();

  int total = 0;
  for(int i = 0;i < ns;++i)
    AddTo(total, Count(i));

  printf("%d\n", Div(total, iso));

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define null NULL
#define mp make_pair
#define pb(a) push_back(a)
#define sz(a) ((int)(a).size())
#define all(a) a.begin() , a.end()
#define fi first
#define se second
#define relaxMin(a , b) (a) = min((a),(b))
#define relaxMax(a , b) (a) = max((a),(b))
#define SQR(a) ((a)*(a))
#define PI 3.14159265358979323846
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

// Modular operations
const int MOD = 1E9 + 7;

int Add(int a, int b, int m = MOD){
  a += b;
  if(a >= m) a -= m;
  return a;
}

int Sub(int a, int b, int m = MOD){
  a -= b;
  if(a < 0) a += m;
  return a;
}

int AddTo(int& to, int w, int m = MOD){
  to += w;
  if(to >= m) to -= m;
  return to;
}

int SubTo(int& to, int w, int m = MOD){
  to -= w;
  if(to < 0) to += m;
  return to;
}

int Mult(int a, int b, int m = MOD){
  return (a * 1LL * b) % m;
}

int Pow(int a, ll deg, int m = MOD){
  int res = 1 % m;
  for(;deg > 0;deg >>= 1, a = Mult(a, a, m))
    if(deg & 1) res = Mult(res, a, m);
  return res;
}

int Oem(int to, int m = MOD){
  return Pow(to, m - 2, m);
}

int Div(int a, int b, int m = MOD){
  return Mult(a, Oem(b, m), m);
}

// Graph
typedef vector<vi> Graph;

int ns, nt;
Graph s, t;

void AddEdge(Graph& g, int f, int t){
  g[f].pb(t), g[t].pb(f);
}

void RemEdge(Graph& g, int f, int t){
  if(f == -1 || t == -1) return;
  g[f].erase(find(all(g[f]), t));
}

// Counting automorphisms
int na;
Graph a;

vi best_p, path;
void Dfs(int vr, int par){
  path.pb(vr);
  if(sz(path) > sz(best_p)) best_p = path;
  for(int nxt : t[vr])
    if(nxt != par) Dfs(nxt, vr);
  path.pop_back();
}

pii Center(){
  for(int i = 0;i < sz(t);++i) Dfs(i, -1);
  int m = sz(best_p) / 2;
  if(sz(best_p) & 1) return mp(best_p[m], best_p[m]);
  else return mp(best_p[m - 1], best_p[m]);
}

map<vi, int> names;
int Name(const vi& l){
  if(names.count(l)) return names[l];
  return names[l] = sz(names) - 1;
}

int iso = 1;
int fact[15];
int AutoDfs(int vr, int par = -1){
  vi ch;
  for(int nxt : a[vr])
    if(nxt != par)
      ch.pb(AutoDfs(nxt, vr));
  sort(all(ch));

  for(int i = 0;i < sz(ch);){
    int j = i;
    while(j < sz(ch) && ch[i] == ch[j]) ++j;
    iso = Mult(iso, fact[j - i]);
    i = j;
  }

  return Name(ch);
}

int Auto(){
  fact[0] = 1;
  for(int i = 1;i < 15;++i)
    fact[i] = Mult(i, fact[i - 1]);

  auto c = Center();

  a = t;
  int root = c.fi;
  if(c.fi != c.se){
    RemEdge(a, c.fi, c.se);
    RemEdge(a, c.se, c.fi);
    a.pb(vi());
    AddEdge(a, sz(a) - 1, c.fi);
    AddEdge(a, sz(a) - 1, c.se);
    root = sz(a) - 1;
  }

  AutoDfs(root);
  return iso;
}

// DP
Graph base, put;
int dp[1000][12];

void Orient(Graph& g, int vr, int par = -1){
  RemEdge(g, vr, par);
  for(int nxt : g[vr]) Orient(g, nxt, vr);
}

int Eval(int bvr, int pvr){
  if(dp[bvr][pvr] != -1) return dp[bvr][pvr];

  int c = sz(put[pvr]);
  vi cdp(1 << c, 0);
  cdp[0] = 1;

  for(int nxt : base[bvr]){
    for(int msk = (1 << c) - 1;msk > 0;--msk){
      for(int i = 0;i < c;++i)
        if((msk & (1 << i)) && cdp[msk ^ (1 << i)] != 0)
          AddTo(cdp[msk], Mult(cdp[msk ^ (1 << i)],
                               Eval(nxt, put[pvr][i])));
    }
  }

  return dp[bvr][pvr] = cdp[(1 << c) - 1];
}

int Count(int root){
  memset(dp, -1, sizeof dp);
  base = s, put = t;

  int proot = 0;
  for(int i = 1;i < sz(put);++i)
    if(sz(put[proot]) > sz(put[i]))
      proot = i;

  Orient(base, root);
  Orient(put, proot);
  return Eval(root, proot);
}

int main(){
  scanf("%d", &ns);
  s.resize(ns);
  for(int i = 1, x, y;i < ns;++i)
    scanf("%d%d", &x, &y),
    AddEdge(s, x - 1, y - 1);

  scanf("%d", &nt);
  t.resize(nt);
  for(int i = 1, x, y;i < nt;++i)
    scanf("%d%d", &x, &y),
    AddEdge(t, x - 1, y - 1);

  Auto();

  int total = 0;
  for(int i = 0;i < ns;++i)
    AddTo(total, Count(i));

  printf("%d\n", Div(total, iso));

  return 0;
}


#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <sstream>
#include <deque>
#include <queue>
#include <random>
#include <complex>
#include <cassert>

using namespace std;

#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FIXED cout << fixed << setprecision(12);
#define RANDOM srand(94385)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 998244353
#define inf 3000000000000000007ll
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define shuffle(a) \
    for (int i = 0; i < sz(a); ++i) \
        swap(a[i], a[rand() % sz(a)])

template<class T, class U> inline void checkmin(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void checkmax(T &x, U y) { if (y > x) x = y; }
template<class T, class U> inline bool ifmax(T &x, U y) { if (y > x) return x = y, true; return false; }
template<class T, class U> inline bool ifmin(T &x, U y) { if (y < x) return x = y, true; return false; }
template<class T> inline void sort(T &a) { sort(all(a)); }
template<class T> inline void rsort(T &a) { sort(rall(a)); }
template<class T> inline void reverse(T &a) { reverse(all(a)); }
template<class T, class U> inline istream& operator>>(istream& str, pair<T, U> &p) { return str >> p.f >> p.s; }
template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; }

const int N = 5e5 + 10;
const int B = 350;

signed main() {
    FAST; FIXED; RANDOM;
    vector<vector<ll>> a(B, vector<ll>(B));
    vector<int> curr(N);
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int mode, x, y;
        cin >> mode >> x >> y;
        if (mode == 1) {
            --x;
            curr[x] += y;
            for (int d = 1; d < B; ++d)
                a[d][x % d] += y;
        } else {
            y += x - 1;
            y %= x;
            if (x >= B) {
                ll ans = 0;
                for (; y < N; y += x) ans += curr[y];
                cout << ans << '\n';
            } else {
                cout << a[x][y] << '\n';
            }
        }
    }
    return 0;
}


#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <sstream>
#include <deque>
#include <queue>
#include <random>
#include <complex>
#include <cassert>

using namespace std;

#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FIXED cout << fixed << setprecision(12);
#define RANDOM srand(94385)
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 998244353
#define inf 3000000000000000007ll
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define shuffle(a) \
    for (int i = 0; i < sz(a); ++i) \
        swap(a[i], a[rand() % sz(a)])

template<class T, class U> inline void checkmin(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void checkmax(T &x, U y) { if (y > x) x = y; }
template<class T, class U> inline bool ifmax(T &x, U y) { if (y > x) return x = y, true; return false; }
template<class T, class U> inline bool ifmin(T &x, U y) { if (y < x) return x = y, true; return false; }
template<class T> inline void sort(T &a) { sort(all(a)); }
template<class T> inline void rsort(T &a) { sort(rall(a)); }
template<class T> inline void reverse(T &a) { reverse(all(a)); }
template<class T, class U> inline istream& operator>>(istream& str, pair<T, U> &p) { return str >> p.f >> p.s; }
template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; }

const int N = 5e5 + 10;
const int B = sqrt(N);

signed main() {
    FAST; FIXED; RANDOM;
    vector<vector<ll>> a(B, vector<ll>(B));
    vector<int> curr(N);
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int mode, x, y;
        cin >> mode >> x >> y;
        if (mode == 1) {
            --x;
            curr[x] += y;
            for (int d = 1; d < B; ++d)
                a[x % d][d] += y;
        } else {
            y += x - 1;
            y %= x;
            if (x >= B) {
                ll ans = 0;
                for (int i = y; i < N; i += x) ans += curr[i];
                cout << ans << '\n';
            } else {
                cout << a[y][x] << '\n';
            }
        }
    }
    return 0;
}


#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <sstream>
#include <deque>
#include <queue>
#include <random>
#include <complex>
#include <cassert>

using namespace std;

#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FIXED cout << fixed << setprecision(12);
#define RANDOM srand(94385)
#define ll int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 998244353
#define inf 3000000000000000007ll
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define shuffle(a) \
    for (int i = 0; i < sz(a); ++i) \
        swap(a[i], a[rand() % sz(a)])

template<class T, class U> inline void checkmin(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void checkmax(T &x, U y) { if (y > x) x = y; }
template<class T, class U> inline bool ifmax(T &x, U y) { if (y > x) return x = y, true; return false; }
template<class T, class U> inline bool ifmin(T &x, U y) { if (y < x) return x = y, true; return false; }
template<class T> inline void sort(T &a) { sort(all(a)); }
template<class T> inline void rsort(T &a) { sort(rall(a)); }
template<class T> inline void reverse(T &a) { reverse(all(a)); }
template<class T, class U> inline istream& operator>>(istream& str, pair<T, U> &p) { return str >> p.f >> p.s; }
template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; }

const int N = 5e5 + 10;
const int B = 300;

void read(int &a) {
    static char c;
    c = getchar();
    if (c == '-') {
        a = 0;
        while (c = getchar(), c >= '0')
            a = (a << 3) + (a << 1) + (c - '0');
        a = -a;
    } else {
        a = c - '0';
        while (c = getchar(), c >= '0')
            a = (a << 3) + (a << 1) + (c - '0');
    }
}

void read(char &c) {
    c = getchar();
    getchar();
}

void read(string &s) {
    s = "";
    static char c;
    while (c = getchar(), c >= 'a')
        s += c;
}

void print(int x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) print(x / 10);
    putchar(x % 10 + '0');
}

void print(char c) {
    putchar(c);
}

signed main() {
    FAST;
    vector<vector<ll>> a(B, vector<ll>(B));
    vector<int> curr(N);
    int q;
    read(q);
    for (int i = 0; i < q; ++i) {
        int mode, x, y;
        read(mode);
        read(x);
        read(y);
        if (mode == 1) {
            --x;
            curr[x] += y;
            for (int d = 1; d < B; ++d)
                a[d][x % d] += y;
        } else {
            y += x - 1;
            y %= x;
            if (x >= B) {
                ll ans = 0;
                for (; y < N; y += x) ans += curr[y];
                print(ans);
                putchar('\n');
            } else {
                print(a[x][y]);
                putchar('\n');
            }
        }
    }
    return 0;
}


#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <sstream>
#include <deque>
#include <queue>
#include <random>
#include <complex>
#include <cassert>

using namespace std;

#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FIXED cout << fixed << setprecision(12);
#define RANDOM srand(94385)
#define ll int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define graph vector<vector<int>>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define f first
#define s second
#define hashmap unordered_map
#define hashset unordered_set
#define eps 1e-9
#define mod 998244353
#define inf 3000000000000000007ll
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define shuffle(a) \
    for (int i = 0; i < sz(a); ++i) \
        swap(a[i], a[rand() % sz(a)])

template<class T, class U> inline void checkmin(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void checkmax(T &x, U y) { if (y > x) x = y; }
template<class T, class U> inline bool ifmax(T &x, U y) { if (y > x) return x = y, true; return false; }
template<class T, class U> inline bool ifmin(T &x, U y) { if (y < x) return x = y, true; return false; }
template<class T> inline void sort(T &a) { sort(all(a)); }
template<class T> inline void rsort(T &a) { sort(rall(a)); }
template<class T> inline void reverse(T &a) { reverse(all(a)); }
template<class T, class U> inline istream& operator>>(istream& str, pair<T, U> &p) { return str >> p.f >> p.s; }
template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; }

const int N = 5e5 + 10;
const int B = 300;

void read(int &x) {
    static char c;
    c = getchar();
    if (c == '-') {
        x = 0;
        while (c = getchar(), c >= '0')
            x = (x << 3) + (x << 1) + (c - '0');
        x = -x;
    } else {
        x = c - '0';
        while (c = getchar(), c >= '0')
            x = (x << 3) + (x << 1) + (c - '0');
    }
}

void print(int x) {
    if (x >= 10) print(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    FAST;
    vector<vector<ll>> a(B, vector<ll>(B));
    vector<int> curr(N);
    int q;
    read(q);
    for (int i = 0; i < q; ++i) {
        int mode, x, y;
        read(mode);
        read(x);
        read(y);
        if (mode == 1) {
            --x;
            curr[x] += y;
            for (int d = 1; d < B; ++d)
                a[d][x % d] += y;
        } else {
            y += x - 1;
            y %= x;
            if (x >= B) {
                ll ans = 0;
                for (; y < N; y += x) ans += curr[y];
                print(ans);
                putchar('\n');
            } else {
                print(a[x][y]);
                putchar('\n');
            }
        }
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int N=500000;
    int K=750;
    int arr=new int[N];
    int sum[K][K];
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t,x,y;
        if (t==1){
            arr[x]+=y;
            for (int j=1;j<K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        else{
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int t=y;t<500001;t+=x){
                    if(t!=0){
                        w+=arr[t];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;
const int N=500000,K=750;
int arr[N],sum[K][K];
int main()
{
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x-1]+=y;
            for (int j=1;j<=K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        if (t==2){
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int m=y;m<500001;m+=x){
                    if(m!=0){
                        w+=arr[m-1];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int N=500000;
    int K=750;
    int arr[N];
    int sum[K][K];
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x-1]+=y;
            for (int j=1;j<=K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        if (t==2){
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int m=y;m<500001;m+=x){
                    if(m!=0){
                        w+=arr[m-1];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;
const int N=5e5,K=710;
int arr[N+1],sum[K+1][K];
int main()
{
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x]+=y;
            for (int j=1;j<=K;j++){
                sum[j][x%j]+=y;
            }
        }
        if (t==2){
            if (x<=K){
                cout<<sum[x][y]<<endl;
            }
            else{
                int w=0;
                for (int m=y;m<=N;m+=x){
                    if(m!=0){
                        w+=arr[m];
                    }
                }
                cout<<w<<endl;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int N=500000;
    int K=750;
    int arr[N];
    int sum[K][K];
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t,x,y;
        if (t==1){
            arr[x]+=y;
            for (int j=1;j<K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        else{
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int t=y;t<500001;t+=x){
                    if(t!=0){
                        w+=arr[t];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;
const int N=500000,K=750;
int arr[N],sum[K][K];
int main()
{
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x-1]+=y;
            for (int j=1;j<=K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        if (t==2){
            if (x<=K){
                cout<<sum[y][x-1]<<endl;
            }
            else{
                int w=0;
                for (int m=y;m<500001;m+=x){
                    if(m!=0){
                        w+=arr[m-1];
                    }
                }
                cout<<w<<endl;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;
int arr[N],sum[K][K];
int main()
{
    int N=500000;
    int K=750;
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x-1]+=y;
            for (int j=1;j<=K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        if (t==2){
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int m=y;m<500001;m+=x){
                    if(m!=0){
                        w+=arr[m-1];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int N=500000;
    int K=750;
    int arr[N];
    int sum[K][K];
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t>>x>>y;
        if (t==1){
            arr[x]+=y;
            for (int j=1;j<K;j++){
                sum[x%j][j-1]+=y;
            }
        }
        else{
            if (x<=K){
                cout<<sum[y][x-1];
            }
            else{
                int w=0;
                for (int t=y;t<500001;t+=x){
                    if(t!=0){
                        w+=arr[t];
                    }
                }
                cout<<w;
            }
        }
    }

    return 0;
}


#include <iostream>

using namespace std;
const int N=5e5,K=710;
int arr[N+1],sum[K+1][K];
int main()
{
    int q;
    cin>>q;
    for (int i=0;i<q;i++){
        int t,x,y;
        cin>>t,x,y;
        if (t==1){
            arr[x]+=y;
            for (int j=1;j<=K;j++){
                sum[j][x%j]+=y;
            }
        }
        else{
            if (x<=K){
                cout<<sum[x][y]<<endl;
            }
            else{
                int w=0;
                for (int t=y;t<=N;t+=x){
                    if(t!=0){
                        w+=arr[t];
                    }
                }
                cout<<w<<endl;
            }
        }
    }

    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<1000000000;i++);
        printf("s");
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int q;
int f[800][800];
int a[500000+5];

int main(){
    scanf("%d",&q);
    while(q--){
        int u,v,op;
        scanf("%d%d%d",&op,&u,&v);
        if(op==1){
            a[u]+=v;
            for(int i=1;i<=750;i++)
                f[i][u%i]+=v;
        }
        if(op==2){
            if(u>750){
                int ans=0;
                for(int i=v;i<=500000;i+=u)
                    ans+=a[i];
                printf("%d\n",ans);
            }
            else printf("%d\n",f[u][v]);
        }
    }
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<100000000;i++)
        printf("%d",i);
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<100000000;i++)
        cout<<i<<endl;
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<100000000;i++);
        printf("s",);
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<100000000;i++);
        printf("s");
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    int l1=0,l2=0;
    for(int i=0;i<1000000000;i++){
        if(i<5555555) l1++;
        else l2++;
    }
        printf("s");
    return 0;
}


# include <bits/stdc++.h>
using namespace std;

int main(){
    for(int i=0;i<1000000000;i++)
        cout<<i<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define maxn 500021
#define Mod 998244353ll
using namespace std;
LL n, a[maxn], ans[1021][1021];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    LL pos, x, y;
    for(LL i = 1; i <= n; i++){
        cin>>pos>>x>>y;
        if(pos == 1){
            a[x] += y;
            for(LL i = 1; i <= 1000; i++)ans[i][x % i] += y;
        }else{
            if(x <= 1000)cout<<ans[x][y]<<endl;
            else{
                LL now = y, sum = 0;
                while(now <= 500000){
                    sum += now;
                    now += x;
                }
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define maxn 500021
#define Mod 998244353ll
using namespace std;
int n, a[maxn], ans[1021][1021];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    int pos, x, y;
    for(int i = 1; i <= n; i++){
        cin>>pos>>x>>y;
        if(pos == 1){
            a[x] += y;
            for(int i = 1; i <= 1000; i++)ans[i][x % i] += y;
        }else{
            if(x <= 1000)cout<<ans[x][y]<<endl;
            else{
                int now = y, sum = 0;
                while(now <= n){
                    sum += now;
                    now += x;
                }
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define maxn 500021
#define Mod 998244353ll
using namespace std;
int n, a[maxn], ans[1021][1021];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    int pos, x, y;
    for(int i = 1; i <= n; i++){
        cin>>pos>>x>>y;
        if(pos == 1){
            a[x] += y;
            for(int i = 1; i <= 710; i++)ans[i][x % i] += y;
        }else{
            if(x <= 710)cout<<ans[x][y]<<endl;
            else{
                int now = y, sum = 0;
                while(now <= 500000){
                    sum += a[now];
                    now += x;
                }
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define maxn 500021
#define Mod 998244353ll
using namespace std;
int n, a[maxn], ans[1021][1021];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    int pos, x, y;
    for(int i = 1; i <= n; i++){
        cin>>pos>>x>>y;
        if(pos == 1){
            a[x] += y;
            for(int i = 1; i <= 710; i++)ans[i][x % i] += y;
        }else{
            if(x <= 710)cout<<ans[x][y]<<endl;
            else{
                int now = y, sum = 0;
                while(now <= 500000){
                    sum += a[now];
                    now += x;
                }
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define maxn 500021
#define Mod 998244353ll
using namespace std;
LL n, a[maxn], ans[1021][1021];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    LL pos, x, y;
    for(LL i = 1; i <= n; i++){
        cin>>pos>>x>>y;
        if(pos == 1){
            a[x] += y;
            for(LL i = 1; i <= 1000; i++)ans[i][x % i] += y;
        }else{
            if(x <= 1000)cout<<ans[x][y]<<endl;
            else{
                LL now = y, sum = 0;
                while(now <= 500000){
                    sum += a[now];
                    now += x;
                }
                cout<<sum<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<utility>
#include<map>

int a[500001];

int K = 710;

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    std::map<std::pair<int, int>, int> small_solutions;

    for(int i=1; i<=K; ++i) {
        for(int j=0; j<i; ++j) {
            small_solutions.insert({{i, j}, 0});
        }
    }

    int q;
    std::cin >>q;

    for(; q>0; --q) {
        int t, x, y;
        std::cin >>t >>x >>y;

        if(t == 1) {
            a[x] += y;

            for(int i=1; i<=K; ++i) {
                std::pair<int, int> key = {i, x%i};
                std::map<std::pair<int, int>, int>::iterator it =  small_solutions.find(key);
                it->second += y;
            }

        } else {
            if(x <= K) {
                std::pair<int, int> key = {x, y};
                std::map<std::pair<int, int>, int>::iterator it =  small_solutions.find(key);

                std::cout <<it->second <<"\n";
            } else {
                int sol = 0;
                for(int i=y; i<=500000; i=i+x) {
                    sol += a[i];
                }

                std:: cout <<sol <<"\n";
            }
        }
    }

    return 0;
}


#include<iostream>
#include<utility>
#include<map>

#define K 710

int a[500001];

int small_solutions[K][K];

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int q;
    std::cin >>q;

    for(; q>0; --q) {
        int t, x, y;
        std::cin >>t >>x >>y;

        if(t == 1) {
            a[x] += y;

            for(int i=1; i<=K; ++i) {
                small_solutions[i][x%i] += y;
            }

        } else {
            if(x <= K) {
                std::cout <<small_solutions[x][y] <<"\n";
            } else {
                int sol = 0;
                for(int i=y; i<=500000; i=i+x) {
                    sol += a[i];
                }

                std:: cout <<sol <<"\n";
            }
        }
    }

    return 0;
}


#include<iostream>
#include<utility>
#include<map>

int a[500001];

int K = 710;

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    std::map<std::pair<int, int>, int> small_solutions;

    for(int i=1; i<=K; ++i) {
        for(int j=0; j<i; ++j) {
            small_solutions.insert({{i, j}, 0});
        }
    }

    int q;
    std::cin >>q;

    for(; q>0; --q) {
        int t, x, y;
        std::cin >>t >>x >>y;

        if(t == 1) {
            a[x] += y;

            for(int i=1; i<=K; ++i) {
                std::pair<int, int> key = {i, x%i};
                std::map<std::pair<int, int>, int>::iterator it =  small_solutions.find(key);
                it->second += y;
            }

        } else {
            if(x <= K) {
                std::pair<int, int> key = {x, y};
                std::map<std::pair<int, int>, int>::iterator it =  small_solutions.find(key);

                std::cout <<it->second <<"\n";
            } else {
                int sol = 0;
                for(int i=y; i<=500000; ++i) {
                    sol += a[i];
                }

                std:: cout <<sol <<"\n";
            }
        }
    }

    return 0;
}


#include<iostream>
#include<utility>
#include<map>

#define K 710

int a[500001];

int small_solutions[K+1][K+1];

int main() {

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int q;
    std::cin >>q;

    for(; q>0; --q) {
        int t, x, y;
        std::cin >>t >>x >>y;

        if(t == 1) {
            a[x] += y;

            for(int i=1; i<=K; ++i) {
                small_solutions[i][x%i] += y;
            }

        } else {
            if(x <= K) {
                std::cout <<small_solutions[x][y] <<"\n";
            } else {
                int sol = 0;
                for(int i=y; i<=500000; i=i+x) {
                    sol += a[i];
                }

                std:: cout <<sol <<"\n";
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n=500001,a[500001],d[777][777];

void Main() {
  ll T;
  R T;
  while(T--) {
    ll x,y,z;
    cin >> z >> x >> y;
    if(z==1) {
      a[x]+=y;
      REP(i,1,700) d[i][x%i]+=y;
    } else if(x>=700) {
      ll ans=0;
      for(int i=y; i<n; i+=x) ans+=a[i];
      pr(ans);
    } else pr(d[x][y]);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n=500001,a[500001],d[1111][1111];

void Main() {
  ll T;
  R T;
  while(T--) {
    ll x,y,z;
    cin >> z >> x >> y;
    if(z==1) {
      a[x]+=y;
      REP(i,1,1000) d[i][x%i]+=y;
    } else if(x>=1000) {
      ll ans=0;
      for(int i=y; i<n; i+=x) ans+=a[i];
      pr(ans);
    } else pr(d[x][y]);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n=500001,a[500001],d[777][777];

void Main() {
  ll T;
  R T;
  while(T--) {
    ll x,y,z;
    cin >> z >> x >> y;
    if(z==1) {
      a[x]+=y;
      REP(i,1,700) d[i][(int)x%(int)i]+=y;
    } else if(x>=700) {
      ll ans=0;
      for(int i=y; i<n; i+=x) ans+=a[i];
      pr(ans);
    } else pr(d[x][y]);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n=500001,a[500001],d[777][777];

void Main() {
  ll T;
  R T;
  while(T--) {
    ll x,y,z;
    cin >> z >> x >> y;
    if(z==1) {
      a[x]+=y;
      REP(i,1,750) d[i][x%i]+=y;
    } else if(x>=750) {
      ll ans=0;
      for(int i=y; i<n; i+=x) ans+=a[i];
      pr(ans);
    } else pr(d[x][y]);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n=500001,a[500001],d[777][777];

void Main() {
  ll T;
  R T;
  while(T--) {
    ll x,y,z;
    cin >> z >> x >> y;
    if(z==1) {
      a[x]+=y;
      REP(i,1,750) d[i][(int)x%(int)i]+=y;
    } else if(x>=750) {
      ll ans=0;
      for(int i=y; i<n; i+=x) ans+=a[i];
      pr(ans);
    } else pr(d[x][y]);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include<bits/stdc++.h>
long long a[500001], sum[700][700]

int main() {
    int q;
    cin >> q;
    for (int k = 0; k < q; k++) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            a[x] += y;
            for (int i = 1; i < 700)
                sum[i][x % i] += y;
        } else {
            long long ans = 0;
            if (x >= 700) {
                int i = y;
                while (i <= n) {
                    ans += a[i];
                    i += x;
                }

            } else {
                ans = sum[x][y];
            }

        }

    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
#define f first;
#define si second;
typedef long long ll;
long long a[500001], sum[700][700];

int main(int argc, const char * argv[]) {

        int q;
        cin >> q;
    int n;
    n = 500000;
        for (int k = 0; k < q; k++) {
            int t, x, y;
            cin >> t >> x >> y;
            if (t == 1) {
                a[x] += y;
                for (int i = 1; i < 700;i++)
                    sum[i][x % i] += y;
            } else {
                long long ans = 0;
                if (x >= 700) {
                    int i = y;
                    while (i <= n) {
                        ans += a[i];
                        i += x;
                    }

                } else {
                    ans = sum[x][y];
                }

            }

        }
        return 0;

    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
#define f first;
#define si second;
typedef long long ll;
long long a[500001], sum[700][700];

int main(int argc, const char * argv[]) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
        int q;
        cin >> q;
    int n;
    n = 500000;
        for (int k = 0; k < q; k++) {
            int t, x, y;
            cin >> t >> x >> y;
            if (t == 1) {
                a[x] += y;
                for (int i = 1; i < 700;i++)
                    sum[i][x % i] += y;
            } else {
                long long ans = 0;
                if (x >= 700) {
                    int i = y;
                    while (i <= n) {
                        ans += a[i];
                        i += x;
                    }

                } else {
                    ans = sum[x][y];
                }
                cout<<ans<<endl;
            }

        }
        return 0;

}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main(int argc, const char * argv[]) {
    int n;
    cin>>n;
    long long s[500001];
    for(int i = 0;i<500001;i++){
        s[i] = 0;
    }
    vector<long long> k;
    for(int i = 0;i<n;i++){
        long long t,x,y;
        cin>>t>>x>>y;
        if(t == 1){
            if(s[x] == 0){
                k.push_back(x);
            }
            s[x]+=y;
        }
        else{
            long long ans;
            ans = 0;
            for(int j = 0;j<k.size();j++){
                if(k[j]%x == y){
                    ans+=s[k[j]];
                }
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
#define f first;
#define si second;
typedef long long ll;
long long a[500001], sum[700][700];

int main(int argc, const char * argv[]) {

        int q;
        cin >> q;
    int n;
    n = 500000;
        for (int k = 0; k < q; k++) {
            int t, x, y;
            cin >> t >> x >> y;
            if (t == 1) {
                a[x] += y;
                for (int i = 1; i < 700;i++)
                    sum[i][x % i] += y;
            } else {
                long long ans = 0;
                if (x >= 700) {
                    int i = y;
                    while (i <= n) {
                        ans += a[i];
                        i += x;
                    }

                } else {
                    ans = sum[x][y];
                }
                cout<<ans<<endl;
            }

        }
        return 0;

}


#include<bits/stdc++.h>
using namespace std;
long long a[500001], sum[700][700]

int main() {
    int q;
    cin >> q;
    for (int k = 0; k < q; k++) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            a[x] += y;
            for (int i = 1; i < 700)
                sum[i][x % i] += y;
        } else {
            long long ans = 0;
            if (x >= 700) {
                int i = y;
                while (i <= n) {
                    ans += a[i];
                    i += x;
                }

            } else {
                ans = sum[x][y];
            }

        }

    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
#define f first;
#define si second;
typedef long long ll;
long long a[500001], sum[700][700];

int main(int argc, const char * argv[]) {

        int q;
        cin >> q;
    int n;
    n = 500000;
        for (int k = 0; k < q; k++) {
            int t, x, y;
            cin >> t >> x >> y;
            if (t == 1) {
                a[x] += y;
                for (int i = 1; i < 700;i++)
                    sum[i][x % i] += y;
            } else {
                long long ans = 0;
                if (x >= 700) {
                    int i = y;
                    while (i <= n) {
                        ans += a[i];
                        i += x;
                    }

                } else {
                    ans = sum[x][y];
                }
                cout<<ans;
            }

        }
        return 0;

    return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex(int q) {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		if (x < cost) {
			for (int j = 1; j < cost; j++)
				pred[j][x % j] += y;
		}

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex(i);
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	system("pause");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 650;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	scanf("%d", &a);
	scanf("%d", &x);
	scanf("%d", &y);

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			  printf("%d\n", sum);
		}
		else {
		  printf("%d\n", pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex(int q) {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		if (x < cost) {
			for (int j = 1; j < cost; j++)
				pred[j][x % j] += y;
		}

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex(i);
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex(int q) {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex(i);
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 675;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			cout << sum;
		}
		else {
			cout << pred[x][y];
		}
	}
}
signed main() {
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 750;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	system("pause");
	return 0;
}


#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = 705;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int q;
	cin >> q;
	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 689;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
int cost = round(sqrt(500001));
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			cout << sum << endl;
		}
		else {
			cout << pred[x][y] << endl;
		}
	}
}
signed main() {
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	//system("pause");
	return 0;
}


#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;

vector<int> arr(500001);
int cost = 705;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int q;
	cin >> q;
	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 700;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
//#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 650;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	scanf("%d", &a);
	scanf("%d", &x);
	scanf("%d", &y);

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			  otv.push_back(sum);
		}
		else {
		  otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		printf("%d\n", otv[i]);
	//system("pause");
	return 0;
}


#include <vector>
#include<algorithm>
#include <iostream>
#include<string>
#include<bitset>
#include<set>
#include<queue>

using namespace std;
#define int long long

vector<int> arr(500001);
//int cost = round(sqrt(500001));
int cost = 650;
vector<vector<int> > pred;
vector<int> otv;

void flex() {
	int a, x, y;
	cin >> a >> x >> y;

	if (a == 1) {
		arr[x] += y;

		for (int j = 1; j < cost; j++)
			pred[j][x % j] += y;

	}
	else {
		if (x >= cost) {
			int sum = 0;
			for (int i = y; i < arr.size(); i += x)
				sum += arr[i];
			otv.push_back(sum);
		}
		else {
			otv.push_back(pred[x][y]);
		}
	}
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int q;
	cin >> q;

	pred.assign(cost, vector<int>(cost));

	for (int i = 0; i < q; i++) {
		flex();
	}

	for (int i = 0; i < otv.size(); i++)
		cout << otv[i] << endl;

	//system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int d = 700;
ll ans[710][710];
int kkkk;
ll a[500010];
int main() {
    ios :: sync_with_stdio(0);
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int opt, x, y;
        cin >> opt >> x >> y;
        if(opt == 1) {
            a[x] += y;
            for(int i = 1; i <= d; i++) ans[i][x % i] += y;
        }
        else {
            if(x <= d) cout << ans[x][y] << endl;
            else {
                ll answ = 0;
                for(int j = y; j <= 500000; j += x) answ += a[j];
                cout << answ << endl;
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.5e5+10;
ll ans[5010][5010];
ll n,m,k,l,r;
ll a[maxn];
string s;
int main(){
	//freopen(".in","r",stdin);
	//freopen("kkk.out","w",stdout);
	scanf("%I64d",&m);
	n=500000;
	k=sqrt(n+0.5);
	for (ll i=1;i<=m;++i){
		char opt; ll x,y;
		cin >> opt;
		scanf("%I64d%d",&x,&y);
		//printf("%c %I64d %I64d\n",opt,x,y);
		if (opt=='1'){
			for (ll i=1;i<k;++i) ans[i][x%i]=ans[i][x%i]+y;
			a[x]+=y;
		} else
		if (opt=='2'){
			if (x<k) printf("%I64d\n",ans[x][y]);
			else {
				ll ans=0;
				for (ll i=y;i<=n;i+=x){
					ans+=a[i];
					//printf("%I64d %I64d\n",ans,i);
				}
				printf("%I64d\n",ans);
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.5e5+10;
ll ans[5010][5010];
ll n,m,k,l,r;
ll a[maxn];
string s;
int main(){
	//freopen(".in","r",stdin);
	//freopen("kkk.out","w",stdout);
	scanf("%I64d",&m);
	n=500000*1ll;
	k=sqrt(n+0.5*1ll);
	for (ll i=1;i<=m;++i){
		char opt; ll x,y;
		cin >> opt;
		scanf("%I64d%I64d",&x,&y);
		//printf("%c %I64d %I64d\n",opt,x,y);
		if (opt=='1'){
			for (ll i=1;i<k;++i) ans[i][x%i]=ans[i][x%i]+y;
			a[x]+=y;
		} else
		if (opt=='2'){
			if (x<k) printf("%I64d\n",ans[x][y]);
			else {
				ll anss=0;
				for (ll i=y;i<=n;i+=x){
					anss+=a[i];
					//printf("%I64d %I64d\n",ans,i);
				}
				printf("%I64d\n",anss);
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.5e6+10;
ll ans[5010][5010];
ll n,m,k,l,r;
ll a[maxn];
string s;
int main(){
	ios :: sync_with_stdio(0);
	cin >> m;
	n=500000*1ll;
	k=800;
	for (ll i=1;i<=m;++i){
		ll x,y,opt;
		cin >> opt >> x >> y;
		if (opt==1){
			for (ll i=1;i<=k;++i) ans[i][x%i]+=y;
			a[x]+=y;
		} else
		if (opt==2){
			if (x<=k) cout << ans[x][y] << endl;
			else {
				ll anss=0;
				for (ll i=y;i<=n;i+=x) anss+=a[i];
				cout << anss << endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int k=700;
ll ans[710][710];
int n;
ll a[500010];
int main(){
	ios :: sync_with_stdio(0);
	cin >> n;
	for (int i=1;i<=n;++i){
		int opt,x,y;
		cin >> opt >> x >> y;
		if (opt==1){
			a[x]+=y;
			for (int i=1;i<=k;++i) ans[i][x%i]+=y;
		} else
		if (opt==2){
			if (x<=k) cout << ans[x][y] << endl;
			else {
				ll anss=0;
				for (int i=y;i<=500000;i+=x) anss+=a[i];
				cout << anss << endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int k=700;
ll ans[1010][1010];
int n;
ll a[500010];
int main(){
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i=1;i<=n;++i){
		ll opt,x,y;
		cin >> opt >> x >> y;
		if (opt==1){
			a[x]+=y;
			for (int i=1;i<=k;++i) ans[i][x%i]+=y;
		} else
		if (opt==2){
			if (x<=k) cout << ans[x][y] << endl;
			else {
				ll anss=0;
				for (int i=y;i<=500000;i+=x) anss+=a[i];
				cout << anss << endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.5e6+10;
const ll k=700;
ll ans[1010][1010];
ll n,m,l,r;
ll a[maxn];
string s;
int main(){
	ios :: sync_with_stdio(0);
	cin >> n;
	for (int i=1;i<=n;++i){
		ll x,y,opt;
		cin >> opt >> x >> y;
		if (opt==1){
			a[x]+=y;
			for (int i=1;i<=k;++i) ans[i][x%i]+=y;
		} else
		if (opt==2){
			if (x<=k) cout << ans[x][y] << endl;
			else {
				ll anss=0;
				for (int i=y;i<=500000;i+=x) anss+=a[i];
				cout << anss << endl;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1.5e5+10;
int ans[5010][5010];
int n,m,k,l,r;
int a[maxn];
string s;
int main(){
	//freopen(".in","r",stdin);
	//freopen("kkk.out","w",stdout);
	scanf("%d",&m);
	n=500000;
	k=sqrt(n+0.5);
	for (int i=1;i<=m;++i){
		char opt; int x,y;
		cin >> opt;
		scanf("%d%d",&x,&y);
		//printf("%c %d %d\n",opt,x,y);
		if (opt=='1'){
			for (int i=1;i<k;++i) ans[i][x%i]=ans[i][x%i]+y;
			a[x]=y;
		} else
		if (opt=='2'){
			if (x<k) printf("%d\n",ans[x][y]);
			else {
				int ans=0;
				for (int i=y;i<=n;i+=x){
					ans+=a[i];
					//printf("%d %d\n",ans,i);
				}
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.5e5+10;
ll ans[5010][5010];
ll n,m,k,l,r;
ll a[maxn];
string s;
int main(){
	ios :: sync_with_stdio(0);
	cin >> m;
	n=500000*1ll;
	k=800;
	for (ll i=1;i<=m;++i){
		ll x,y,opt;
		cin >> opt >> x >> y;
		if (opt==1){
			for (ll i=1;i<=k;++i) ans[i][x%i]+=y;
			a[x]+=y;
		} else
		if (opt==2){
			if (x<=k) cout << ans[x][y] << endl;
			else {
				ll anss=0;
				for (ll i=y;i<=n;i+=x) anss+=a[i];
				cout << anss << endl;
			}
		}
	}
	return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[758][758],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,750)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=750)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[707][707],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,700)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=700)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[798][798],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,795)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=795)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[808][808],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,805)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=805)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[798][798],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,790)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=790)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
using namespace std;

ll a[500500],h,x,y,ans,m=1;
ll qry[707][707],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,700)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=700)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[828][828],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,820)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=820)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[798][798],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,790)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=790)
        {
            cout<<qry[x][y]<<'/n';
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<'/n';
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define ll long long int

using namespace std;

ll a[500500],h,x,y,ans,m=1;
ll qry[707][707],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,700)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=700)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[808][808],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,800)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<=800)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}


// DONT try this at home or school or any where else ..
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i=0;i<n;i++)
#define repp(i,n) for(long long int i=1;i<=n;i++)
#define R return
#define B break
#define C continue
#define S second
#define F first
#define ll long long int
#define pa pair<long long int,long long int>
#define SI size()
#define pb push_back
using namespace std;
ll dif (ll a,ll b)
{
    R ((a/b)+(a%b!=0));
}

ll poww(ll a,ll b,ll mod){
    ll res=1;if(b<0)b=(b%(mod-1)+mod-1)%(mod-1);
    for(;b;b>>=1,a=1ll*a*a%mod)
      if(b&1)res=1ll*res*a%mod;
    return res;
}

ll a[500500],h,x,y,ans,m=1;
ll qry[707][707],q;

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

cin>>q;

while (q--)
{
    cin>>h>>x>>y;
    if(h==1)
    {
        a[x]+=y;
        repp(i,700)
        qry[i][x%i]+=y;
        m=max(m,x);
    }
    else
    {
        if(x<700)
        {
            cout<<qry[x][y]<<endl;
        }
        else
        {
            ans=0;

            for(ll i=y;i<=m;i+=x)
            {
                ans+=a[i];
            }
            cout<<ans<<endl;
        }
    }
}

return 0;
}