stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

L E=70;W S(V<W>p){if(!E){return{1,0};}V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1){T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}}for(int i;i<2;i++){L c=0;for(W x:T[!i]){L Q=(2*x[0]+x[1])%3;c^=Q+1;}if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p){v[0]++;}W A=S(p);A[0]--;return A;}int main(){L M=2e17,n,a,b;cin>>n;V<W>p(n);for(int i;i<n;i++){cin>>a>>b;p[i]={a+M,b+M};}W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.1373003223596576x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

#define R(n)for(L i;i<n;i++){L c

L E=70;W S(V<W>p){if(!E){return{1,0};}V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1){T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}}R(2)=0;for(W x:T[!i]){L Q=(2*x[0]+x[1])%3;c^=Q+1;}if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p){v[0]++;}W A=S(p);A[0]--;return A;}int main(){L M=2e17,n,b;cin>>n;V<W>p(n);R(n);cin>>c>>b;p[i]={c+M,b+M};}W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.135610737167709x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

L E=70;W S(V<W>p){if(!E){return{1,0};}V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1){T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}}L i=2;while(i--){L c=0;for(W x:T[!i]){L Q=(2*x[0]+x[1])%3;c^=Q+1;}if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p){v[0]++;}W A=S(p);A[0]--;return A;}int main(){L M=2e17,n,a,b;cin>>n;V<W>p(n);while(n--){cin>>a>>b;p[n]={a+M,b+M};}W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.136179118025264x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

L E=70,M=2e17,n,a,b;W S(V<W>p){if(!E){return{1,0};}V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1){T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}}for(L i;i<2;i++){L c=0;for(W x:T[!i]){L Q=(2*x[0]+x[1])%3;c^=Q+1;}if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p){v[0]++;}W A=S(p);A[0]--;return A;}int main(){cin>>n;V<W>p(n);while(n--){cin>>a>>b;p[n]={a+M,b+M};}W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.1356152970896431x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

L E=70,M=2e17,i,a,b;W S(V<W>p){if(!E){return{1,0};}V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1){T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}}for(L i;i<2;i++){L c=0;for(W x:T[!i])c^=(2*x[0]+x[1])%3+1;if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p)v[0]++;W A=S(p);A[0]--;return A;}int main(){cin>>i;V<W>p(i);while(i--,cin>>a>>b)p[i]={a+M,b+M};W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.1327135773553203x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define V vector

#define L long

#define W V<L>

L E=70,M=2e17,i,a,b;W S(V<W>p){if(!E)return{1,0};V<V<W>>T(2);for(W x:p){T[x[1]&1].push_back({x[0]/2,x[1]/2});if(x[0]&1)T[(x[1]+1)&1].push_back({x[0]/2,(x[1]+1)/2});}for(L i;i<2;i++){L c=0;for(W x:T[!i])c^=(2*x[0]+x[1])%3+1;if(!c){E--;W A=S(T[i]);A[0]*=2;A[1]*=2;A[1]+=i;return A;}}for(W&v:p)v[0]++;W A=S(p);A[0]--;return A;}int main(){cin>>i;V<W>p(i);while(i--,cin>>a>>b)p[i]={a+M,b+M};W x=S(p);cout<<x[0]-M<<" "<<x[1]-M;}

// Faster C++ solution for the same problem (measured speedup: 1.1327902913360948x):
#include<iostream>

#include<vector>

using namespace std;

#define int long long

#define V vector

#define vel V<int>

#define rep(i,n) for(int i=0;i<n;i++)

#define mkp make_pair

#define pin pair<int,int>

pin solve(V<pin>& ab, int ex) {

	if (!ex) { return mkp(0, 0); }

	V<V<pin>> qab(2);

	int sz = ab.size();

	qab[0].reserve(sz);

	qab[1].reserve(sz);

	for (pin& x : ab) {

		qab[x.second & 1].push_back(mkp(x.first / 2, x.second / 2));

		if (x.first & 1) { qab[(x.second + 1) & 1].push_back(mkp(x.first / 2, (x.second + 1) / 2)); }

	}

	int c = 0;

	for (pin& x : qab[0]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

	if (!c) {

		pin ans = solve(qab[1], ex - 1);

		ans.first *= 2; ans.second *= 2; ans.second++;

		return ans;

	}

	pin ans = solve(qab[0], ex - 1);

  	c=0;

  	for (pin& x : qab[1]) {

		int qc = (2 * x.first + x.second) % 3;

		c ^= (qc + 1);

	}

  	if (!c) {

		ans.first *= 2; ans.second *= 2;

		return ans;

	}

	c = 0;

	for (pin& x : qab[1]) {

		int w = ans.second - x.second-1;

		if ((w & x.first) == w) { c ^= 1; }

	}

	ans.first *= 2; ans.first++; ans.second *= 2; ans.second -= c;

	return ans;

}

signed main() {

	cin.tie(0); ios::sync_with_stdio(false);

	long long inf = 1e17;

	int n; cin >> n;

	V<pin> ab(n);

	rep(i, n) {

		long long a, b; cin >> a >> b;

		ab[i] = { a + inf,b + inf };

	}

	pin ret = solve(ab, 59);

	cout << ret.first - inf << " " << ret.second - inf << "\n";

}
// Slower C++ solution:
#include<vector>

#include<bitset>

#include<cstdio>

#include<iostream>

#include<algorithm>

#define pb push_back

#define nc getchar

using namespace std;

inline void read(int &x) {

	char b = nc(); x = 0;

	for (; !isdigit(b); b = nc());

	for (; isdigit(b); b = nc()) x = x * 10 + b - '0';

}

struct Node {

	int x, y, id;	

	inline bool operator<(const Node &o) const {

		return x < o.x || (x == o.x && y < o.y);

	}

} a[100005];

int n, s, t, D, f[100005];

long long ans;

vector < int > g[100005];

bitset < 100005 > vis;

inline int ABS(int x) {return x > 0 ? x : -x;}

inline void ae(int u, int v) {

	g[u].pb(v); g[v].pb(u);	

}

void build(int k) {

	sort(a + 1, a + 1 + n); int i, j, l, r;

	for (i = j = l = r = 1; i <= n; ++i) {

		while (a[l].x < a[i].x - D || (a[l].x == a[i].x - D && a[i].y - a[l].y > k)) ++l;

		while (a[r].x < a[i].x - D || (a[r].x == a[i].x - D && a[r].y - a[i].y <= k)) ++r;

		f[a[i].id] += r - l; j = max(j, l); if (j < r) ae(a[i].id, a[j].id);

		for (; j + 1 < r; ++j) ae(a[j].id, a[j+1].id);

	}

}

void dfs(int u) {

	if (vis[u]) return; vis[u] = 1; ans += f[u];

	for (int i = 0; i < g[u].size(); ++i) dfs(g[u][i]);

}

int main() {

	read(n); read(s); read(t);

	for (int x, y, i = 1; i <= n; ++i)

		read(x), read(y), a[i] = (Node){x + y, x - y, i};

	D = max(ABS(a[s].x - a[t].x), ABS(a[s].y - a[t].y));

	build(D);

	for (int i = 1; i <= n; ++i) swap(a[i].x, a[i].y);

	build(D-1);

	dfs(s);

	printf("%lld\n", ans);

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.5689359780563548x):
#include<vector>

#include<bitset>

#include<cstdio>

#include<iostream>

#include<algorithm>

#define pb push_back

#define nc getchar

using namespace std;

inline void read(int &x) {

	char b = nc(); x = 0;

	for (; !isdigit(b); b = nc());

	for (; isdigit(b); b = nc()) x = x * 10 + b - '0';

}

struct Node {

	int x, y, id;	

	inline bool operator<(const Node &o) const {

		return x < o.x || (x == o.x && y < o.y);

	}

} a[100005];

int n, s, t, D, f[100005], fa[100005];

int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}

long long ans;

bitset < 100005 > vis;

inline int ABS(int x) {return x > 0 ? x : -x;}

inline void ae(int u, int v) {

	if ((u = find(u)) != (v = find(v))) fa[v] = u;

}

void build(int k) {

	sort(a + 1, a + 1 + n); int i, j, l, r;

	for (i = j = l = r = 1; i <= n; ++i) {

		while (a[l].x < a[i].x - D || (a[l].x == a[i].x - D && a[i].y - a[l].y > k)) ++l;

		while (a[r].x < a[i].x - D || (a[r].x == a[i].x - D && a[r].y - a[i].y <= k)) ++r;

		f[a[i].id] += r - l; j = max(j, l); if (j < r) ae(a[i].id, a[j].id);

		for (; j + 1 < r; ++j) ae(a[j].id, a[j+1].id);

	}

}

int main() {

	read(n); read(s); read(t);

	for (int i = 1; i <= n; ++i) fa[i] = i;

	for (int x, y, i = 1; i <= n; ++i)

		read(x), read(y), a[i] = (Node){x + y, x - y, i};

	D = max(ABS(a[s].x - a[t].x), ABS(a[s].y - a[t].y));

	build(D);

	for (int i = 1; i <= n; ++i) swap(a[i].x, a[i].y);

	build(D-1);

	for (int i = 1; i <= n; ++i)

		if (find(i) == find(s)) ans += f[i];

	printf("%lld\n", ans);

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <string>

#include <map>

#include <set>

#include <cstdlib>

using namespace std;

map <string,int> cost;

multimap <string,string>  rule;

set <string> his,his2;

int flg;



void update(string a){

  his2.insert(a);

  multimap<string,string>::iterator it=rule.find(a);

  while(it != rule.end() && it->first == a) {

    if(his2.count(it->second)==0&&cost[it->second]>cost[a])cost[it->second] =cost[a],update(it->second);

    it++;

  }

}



int  mk_rule(string a){

  if(his.count(a) == 0){

    his.insert(a);    

    multimap<string,string>::iterator it=rule.find(a);

    while(it != rule.end() && it->first == a) {

      cost[a] = min(cost[a],mk_rule(it->second));

      it++;

    }

    his2.clear();

    update(a);

    it=rule.find(a);

    while(it != rule.end() && it->first == a) {

      if(cost[it->second]  > cost[a])exit(1);

      it++;

    }



  }

  return cost[a];

}



int main(){

  int n,m,b;

  cin >> n;

  string a,c,ls[5002];

  for(int i=0;i<n;i++) {

    cin >> a >> b;

    cost[a] = b , ls[i] = a;

  }

  cin >> m;

  for(int i=0;i<m;i++){

    cin >> a >> c;

    rule.insert(pair<string,string>(a,c));

    rule.insert(pair<string,string>(c,a));

  }

  int ans=0;

  for(int i=0;i<n;i++) ans += mk_rule(ls[i]);

  cout << ans <<endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1630805935571882x):
#include <iostream>

#include <string>

#include <algorithm>

#include <map>

using namespace std;

map <string,string> mp;

map <string,int> in;



string find(string  x){

  if(mp[x] != x) mp[x] = find(mp[x]);

  return mp[x];

}



void unite(string x,string y){

  x=find(x),y=find(y);

  if(in[x]<in[y]) swap(x,y);

  mp[x] = mp[y];

}



int main() {

  int n,m,a;

  string str,str2;

  cin >> n;

  for(int i=0;i<n;i++) {

    cin >> str >> a;

    in[str] = a;

    mp[str] = str;

  } 



  cin >> m;

  for(int i=0;i<m;i++)cin >> str >> str2,unite(str,str2);



  int ans =0;

  map<string,int> :: iterator it;

  for(it=in.begin();it!=in.end();it++) ans += in[find(it->first)];

  cout << ans <<endl;

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <string>

#include <map>

#include <set>

using namespace std;

map <string,int> cost;

multimap <string,string>  rule;

set <string> his,his2;

int flg;



void update(string a){

  his2.insert(a);

  multimap<string,string>::iterator it=rule.find(a);

  while(it != rule.end() && it->first == a) {

    if(his2.count(it->second)==0&&cost[it->second]>cost[a])cost[it->second] =cost[a],update(it->second);

    it++;

  }

}

int  mk_rule(string a){

  if(his.count(a) == 0){

    his.insert(a);    

    multimap<string,string>::iterator it=rule.find(a);

    while(it != rule.end() && it->first == a) {

      cost[a] = min(cost[a],mk_rule(it->second));

      it++;

    }

    his2.clear() , update(a);

  }

  return cost[a];

}



int main(){

  int n,m,b;

  cin >> n;

  string a,c,ls[5002];

  for(int i=0;i<n;i++) {

    cin >> a >> b;

    cost[a] = b , ls[i] = a;

  }

  cin >> m;

  for(int i=0;i<m;i++){

    cin >> a >> c;

    rule.insert(pair<string,string>(a,c));

    rule.insert(pair<string,string>(c,a));

  }

  int ans=0;

  for(int i=0;i<n;i++) ans += mk_rule(ls[i]);

  cout << ans <<endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1630021477904366x):
#include <iostream>

#include <string>

#include <algorithm>

#include <map>

using namespace std;

map <string,string> mp;

map <string,int> in;



string find(string  x){

  if(mp[x] != x) mp[x] = find(mp[x]);

  return mp[x];

}



void unite(string x,string y){

  x=find(x),y=find(y);

  if(in[x]<in[y]) swap(x,y);

  mp[x] = mp[y];

}



int main() {

  int n,m,a;

  string str,str2;

  cin >> n;

  for(int i=0;i<n;i++) {

    cin >> str >> a;

    in[str] = a;

    mp[str] = str;

  } 



  cin >> m;

  for(int i=0;i<m;i++)cin >> str >> str2,unite(str,str2);



  int ans =0;

  map<string,int> :: iterator it;

  for(it=in.begin();it!=in.end();it++) ans += in[find(it->first)];

  cout << ans <<endl;

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <string>

#include <map>

#include <set>

using namespace std;

map <string,int> cost;

multimap <string,string>  rule;

set <string> his;

int mk_rule(string a){

  if(his.count(a) == 0){

    his.insert(a);    

    multimap<string,string>::iterator it=rule.find(a);

    while(it != rule.end() && it->first == a) cost[a]= min(cost[a],mk_rule(it->second)),it++;

  }

  return cost[a];

}



int main(){

  int n,m,b;

  cin >> n;

  string a,c,ls[5002];

  for(int i=0;i<n;i++) cin >> ls[i] >> b, cost[ls[i]] = b;

  

  cin >> m;

  for(int i=0;i<m;i++){

    cin >> a >> c;

    rule.insert(pair<string,string>(a,c));

    rule.insert(pair<string,string>(c,a));

  }

  int ans=0;

  for(int i=0;i<n;i++)his.clear(), ans += mk_rule(ls[i]);

  cout << ans <<endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1609459401773832x):
#include <iostream>

#include <string>

#include <algorithm>

#include <map>

using namespace std;

map <string,string> mp;

map <string,int> in;



string find(string  x){

  if(mp[x] != x) mp[x] = find(mp[x]);

  return mp[x];

}



void unite(string x,string y){

  x=find(x),y=find(y);

  if(in[x]<in[y]) swap(x,y);

  mp[x] = mp[y];

}



int main() {

  int n,m,a;

  string str,str2;

  cin >> n;

  for(int i=0;i<n;i++) {

    cin >> str >> a;

    in[str] = a;

    mp[str] = str;

  } 



  cin >> m;

  for(int i=0;i<m;i++)cin >> str >> str2,unite(str,str2);



  int ans =0;

  map<string,int> :: iterator it;

  for(it=in.begin();it!=in.end();it++) ans += in[find(it->first)];

  cout << ans <<endl;

  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <string>

#include <map>

#include <set>

using namespace std;

map <string,int> cost;

multimap <string,string>  rule;

set <string> his;

int mk_rule(string a,int mae){

  if(his.count(a) == 0||cost[a]>mae){

    his.insert(a);    

    multimap<string,string>::iterator it=rule.find(a);

    while(it != rule.end() && it->first == a) cost[a]= min(cost[a],mk_rule(it->second,cost[a])),it++;

    it=rule.find(a);

    while(it != rule.end() && it->first == a) cost[a]= min(cost[a],mk_rule(it->second,cost[a])),it++;

  }

  return cost[a];

}

int main(){

  int n,m,b;

  cin >> n;

  string a,c,ls[5002];

  for(int i=0;i<n;i++) cin >> ls[i] >> b, cost[ls[i]] = b;



  cin >> m;

  for(int i=0;i<m;i++){

    cin >> a >> c;

    rule.insert(pair<string,string>(a,c));

    rule.insert(pair<string,string>(c,a));

  }

  int ans=0;

  for(int i=0;i<n;i++) ans += mk_rule(ls[i],cost[ls[i]]);

  cout << ans <<endl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1636315672070334x):
#include <iostream>

#include <string>

#include <algorithm>

#include <map>

using namespace std;

map <string,string> mp;

map <string,int> in;



string find(string  x){

  if(mp[x] != x) mp[x] = find(mp[x]);

  return mp[x];

}



void unite(string x,string y){

  x=find(x),y=find(y);

  if(in[x]<in[y]) swap(x,y);

  mp[x] = mp[y];

}



int main() {

  int n,m,a;

  string str,str2;

  cin >> n;

  for(int i=0;i<n;i++) {

    cin >> str >> a;

    in[str] = a;

    mp[str] = str;

  } 



  cin >> m;

  for(int i=0;i<m;i++)cin >> str >> str2,unite(str,str2);



  int ans =0;

  map<string,int> :: iterator it;

  for(it=in.begin();it!=in.end();it++) ans += in[find(it->first)];

  cout << ans <<endl;

  return 0;

}