#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <fstream>
#include <bitset>
#include <time.h>
#include <assert.h>
#include <random>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14


using namespace std;


struct Edge {
    int from;
    int to;
    int cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    bool operator<(const Edge& edge) const {
        return this->cost < edge.cost;
    }
    bool operator>(const Edge& edge) const {
        return this->cost > edge.cost;
    }
};

class UnionFind {
public:
    vector<int> parent;
    vector<int> setSize;

    UnionFind(unsigned int size) {
        parent.resize(size + 1);
        iota(parent.begin(), parent.end(), 0);
        setSize.resize(size + 1, 1);
    }

    // x??¨y???????????????????±??????????
    bool isSameSet(int x, int y) {
        return findRoot(x) == findRoot(y);
    }

    // x??¨y????±??????????????????????
    void unionSet(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (x == y) { return; }

        parent[x] = y;
        setSize[y] += setSize[x];
    }

private:
    // ??¨??????????±???????
    int findRoot(int x) {
        if (parent[x] == x) {
            return x;
        }
        else {
            return parent[x] = findRoot(parent[x]);
        }
    }
};

/**
 * ??°?????????????°???¨?????¨????±??????????O(|E| log|V|)
 * @param num_of_node ???????????°
 * @param edges?????¨?????????????????°????????????u -> v??¨v -> u??????????????\??????
 * @return
 */
long long kruskal(unsigned int num_of_node, vector<Edge> edges) {
    sort(edges.begin(), edges.end());

    UnionFind uf(num_of_node);

    long long ans = 0;
    for (Edge e : edges) {
        if (!uf.isSameSet(e.from, e.to)) {
            uf.unionSet(e.from, e.to);
            ans += e.cost;
        }
    }
    return ans;
}

int main(int argc, char *argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int V, E;
    cin >> V >> E;

    vector<Edge> edges;
    FOR(i, 0, E) {
        int s, t, w;
        cin >> s >> t >> w;
        edges.emplace_back(Edge(s, t, w));
        edges.emplace_back(Edge(t, s, w));
    }

    cout << kruskal(V, edges) << endl;

    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

class UnionFind{ //連結判定
private:
    std::vector<int> parent; //parent[i] = parent of node i
    std::vector<int> rank;
    std::vector<int> size;
public:
    //constructor
    UnionFind(int N){
        for(int i=0; i<N; i++) parent.push_back(i); //parent = [0,1,..,N-1]
        for(int i=0; i<N; i++) rank.push_back(0); //rank = [0,0,..,0]
        for(int i=0; i<N; i++) size.push_back(1);
    }
    //initialize
    void init(int N){
        parent.clear();
        rank.clear();
        size.clear();
        for(int i=0; i<N; i++) parent.push_back(i); //parent = [0,1,..,N-1]
        for(int i=0; i<N; i++) rank.push_back(0); //rank = [0,0,..,0]
        for(int i=0; i<N; i++) size.push_back(1);
    }
    int root(int a){
        if(parent[a] == a) return a;
        return parent[a] = root(parent[a]); //親が自分自身になるまで再帰を呼び出す & 経路圧縮 
    }
    void unite(int a, int b){
        int rootA = root(a);
        int rootB = root(b);
        if(rootA == rootB) return; //ノードa, bがすでに連結しているとき
        //次数の大きい方を親として連結
        if(rank[rootA] < rank[rootB]){
            size[rootB] += size[rootA];
            parent[rootA] = rootB;
        }
        else{
            size[rootA] += size[rootB];
            parent[rootB] = rootA;
            if(rank[rootA] == rank[rootB]) rank[rootA]++;
        }
    }
    bool same(int a, int b){
        return root(a) == root(b); //ノードa, bが同じ木に属するかどうか
    }
    int get_size(int a){
        return size[root(a)];
    }
};

int main()
{
    using P = std::pair<int, int>;

    int N, K, L;
    std::cin >> N >> K >> L;
    std::vector<P> route(K), rail(L);
    for(int i=0; i<K; i++){
        scanf(" %d %d", &(route[i].first), &(route[i].second));
        route[i].first--; route[i].second--;
    }
    for(int i=0; i<L; i++){
        scanf(" %d %d", &(rail[i].first), &(rail[i].second));
        rail[i].first--; rail[i].second--;
    }

    UnionFind uf_route(N), uf_rail(N);
    for(int i=0; i<K; i++){
        int u = route[i].first, v = route[i].second;
        uf_route.unite(u, v);
    }
    for(int i=0; i<L; i++){
        int u = rail[i].first, v = rail[i].second;
        uf_rail.unite(u, v);
    }

    std::vector<std::pair<P, int> > root;
    for(int i=0; i<N; i++){
        int a = uf_route.root(i), b = uf_rail.root(i);
        root.push_back(std::pair<P, int>(P(a, b), i));
    }
    std::sort(root.begin(), root.end());
    std::vector<int> tmp, count(N);
    for(int i=0; i<N; i++){
        tmp.push_back(root[i].second);
        if(i == N-1 || (i != N-1 && root[i].first != root[i+1].first)){
            int M = tmp.size();
            while(!tmp.empty()){
                count[tmp.back()] = M;
                tmp.pop_back();
            }
        }
    }
    
    for(int i=0; i<N; i++){
        printf("%d ", count[i]);
    }
    printf("\n");
}