#include <iostream>
#include <vector>
#include <cmath>
#include <unordered_set>
#include <algorithm>
using namespace std;
typedef long long ll;

vector<vector<pair<int, ll>>> adj;
vector<ll> dist;
const int MOD = 1e9 + 7;

int modadd(int a, int b) {
	return (a % MOD + b % MOD) % MOD;
}

int modmul(int a, int b) {
	return (ll) a * b % MOD;
}

void dfs(int u, int p, ll currdist) {
	dist[u] = currdist;
	for (auto v : adj[u]) if (v.first != p) dfs(v.first, u, currdist ^ v.second);
}


int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;
	adj.resize(n + 1);
	dist.resize(n + 1);

	for (int i = 0; i < n - 1; i++) {
		ll u, v, w;
		cin >> u >> v >> w;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}
	dfs(1, 0, 0);
	int ans = 0;

	for (int bitpos = 0; bitpos < 61; bitpos++) {
		ll mask = (1ll << bitpos);
		int ones = 0, zeros = 0;
		for (int u = 1; u <= n; u++) {
			ll currdist = dist[u];
			if (currdist & mask) ones++;
			else zeros++;
		}
		int count = modmul(ones, zeros);
		ans = modadd(ans, modmul(count, mask % MOD));
	}

	cout << ans << endl;

	return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define endl "\n"

int main(){
	int n =0 , m =0 ;
	cin >> n >> m;
	int s =0 , s2=0;
	vector<pair<int , int > > ar;
	for(int i=0;i<m;i++){
		cin >> s >> s2;
		ar.emplace_back(s ,s2 );
	}
	
	int k =0 ;
	cin >> k;
	vector< pair<int , int > > g ;
	for(int i=0;i<k;i++){
		cin >> s >> s2 ;
		g.emplace_back(s,s2);
	}
	int mx =0 ;
	for(int mask =0 ;mask < (1 << k);mask ++ ){
		int pts =0 ;
		
		map<int , bool > tmp;
		for(int i =k-1 ;i>=0;i--){
			if(mask &(1LL << i)){
				
				tmp[g[i].first] = 1;
			}
			else
				tmp[g[i].second] = 1;
		}
		
		for(int i =0 ;i<m;i++){
			if(tmp[ar[i].first] &&tmp[ar[i].second])
				pts ++ ;
		}
		
		mx = max(mx, pts );
	}
	
	cout << mx << endl;
return 0;}
