With your help, Heidi has prepared a plan of trap placement and defence. Yet suddenly, the Doctor popped out of the TARDIS and told her that he had spied on the Daleks' preparations, and there is more of them than ever. Desperate times require desperate measures, so Heidi is going to risk meeting with the Daleks and she will consider placing a trap along any Corridor.

This means she needs your help again in calculating E_{max}(c) – the largest e ≤ 10^9 such that if we changed the energy requirement of c to e, then the Daleks might use c in their invasion – but this time for all Time Corridors.

Input

First line: number n of destinations, number m of corridors (2 ≤ n ≤ 10^5, n - 1 ≤ m ≤ 10^6). The next m lines: destinations a, b and energy e (1 ≤ a, b ≤ n, a ≠ b, 0 ≤ e ≤ 10^9).

No pair \\{a, b\} will repeat. The graph is guaranteed to be connected. It is not guaranteed that all energy requirements e are distinct, or that the minimum spanning tree is unique.

Output

Output m lines, each containing one integer: E_{max}(c_i) for the i-th Corridor c_i from the input.

Example

Input

3 3
1 2 8
2 3 3
3 1 4


Output

4
8
8
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
 
 
public class q5 {
	static HashMap<Integer,Integer>[] adj;
	static int[][] par, min,max;
	static int[] depth;
	static int d;
	static int ans;
	
	static void dfs(int x, int p) {
		for(int j:adj[x].keySet()) {
			if(j!=p) {
				par[0][j]=x;
				min[0][j]=adj[x].get(j);
				depth[j]=depth[x]+1;
				dfs(j,x);
			}
		}
	}
	static int walk(int i, int steps) {
        for(int k=0;k<=d && i!=-1;k++) {
            if(((1<<k)&steps)>0) {
            	ans=Math.max(ans, min[k][i]);
            	//int a=ans;
                i=par[k][i];
            }
        }
        return i;
    }
    
    static int lca(int i, int j) {
        if(depth[i]>depth[j]) {
            i=walk(i,depth[i]-depth[j]);
        }
        else if(depth[j]>depth[i]) j=walk(j,depth[j]-depth[i]);
        
        if(i==j) return i;
        //if(i==-1 || j==-1) return 0;
        for(int k=d;k>=0;k--) {
            if(par[k][i]!=par[k][j]) {
            	ans=Math.max(ans,min[k][i]);
            	ans=Math.max(ans, min[k][j]);
                i=par[k][i];
                j=par[k][j];
            }
        }
        ans=Math.max(ans, min[0][i]);
        ans=Math.max(ans, min[0][j]);
        return par[0][i];
    }
    
    static int update(int x, int p,int v) {
    	int ans=(int)1e9;
    	for(int i=d;i>=0 && x!=-1;i--) {
    		if(depth[x]-depth[p]>=(1<<i)) {
    			max[i][x]=Math.min(max[i][x], v);
    			ans=Math.min(max[i][x], ans);
    			x=par[i][x];
    		}
    	}
    	return ans;
    }
    
    
	
 
	public static void main(String[] args) throws IOException {
		
	Reader.init(System.in);
	PrintWriter out=new PrintWriter(System.out);
	int n=Reader.nextInt();
	int m=Reader.nextInt();
	adj=new HashMap[n];
	d=Integer.numberOfTrailingZeros(Integer.highestOneBit(n));
	par=new int[d+1][n];min=new int[d+1][n];max=new int[d+1][n];
	for(int i=0;i<=d;i++) {
		Arrays.fill(par[i],-1);
		Arrays.fill(max[i], (int)1e9);
	}
	depth=new int[n];
	for(int i=0;i<n;i++) {
		adj[i]=new HashMap<Integer,Integer>();
		//adj2[i]=new HashMap<Integer,Integer>();
	}
	int[] included=new int[m],included2=new int[m];
	NoD[] arr=new NoD[m];
	NoD[] arr2=new NoD[m];
	for(int i=0;i<m;i++) {
		NoD c=new NoD(Reader.nextInt(),Reader.nextInt(),Reader.nextInt(),i);
		arr[i]=c;
		arr2[i]=c;
		
	}
	
	//int low=0,high=(int)1e9;
	Arrays.sort(arr,new Comparator<NoD>() {
			public int compare(NoD a,NoD b) {
				return a.z-b.z;
			}
	});
		
	DisJoint set=new DisJoint(n);
	for(NoD c:arr) {
		if(set.find(c.x)!=set.find(c.y)) {
				set.union(c.x, c.y);
				included[c.i]=1;
				adj[c.x-1].put(c.y-1, c.z);
				adj[c.y-1].put(c.x-1, c.z);
		}
	}
	dfs(0,-1);
	for(int i=1;i<=d;i++) {
		for(int j=0;j<n;j++) {
			int mid=par[i-1][j];
			if(mid!=-1) {
				par[i][j]=par[i-1][mid];
				min[i][j]=Math.max(min[i-1][mid], min[i-1][j]);
			}
		}
	}
	int[] aa=new int[m];
	
	
	for(int i=0;i<m;i++) {
		if(included[i]==0) {
			//HashMap<Integer,Integer>[] a2=adj;
			ans=Integer.MIN_VALUE;
			int a=arr2[i].x,b=arr2[i].y;
			a--;b--;
			int r=lca(a,b);
			aa[i]=ans;
			q5.update(a, r, arr2[i].z);
			q5.update(b, r, arr2[i].z);
		}
		
	}
	int[][] al2=max;
	for(int i=d;i>0;i--) {
		for(int j=0;j<n;j++) {
			max[i-1][j]=Math.min(max[i-1][j], max[i][j]);
			int mid=par[i-1][j];
			if(mid!=-1) {
				max[i-1][mid]=Math.min(max[i-1][mid], max[i][j]);
			}
			
		}
	}
	
	
	for(int i=0;i<m;i++) {
		if(included[i]==1) {
			int a=arr2[i].x,b=arr2[i].y;
			a--;b--;
			int r=lca(a,b);
			int ans=q5.update(a, r, (int)1e9);
			ans=Math.min(ans, q5.update(b, r, (int)1e9));
			aa[i]=ans;
		}
	}
	
	for(int i=0;i<m;i++) out.println(aa[i]);
	
	
	
	
	out.flush();
		
	}
}
 
class NoD{
	int x,y,z,i;
	NoD(int a,int b,int c,int d){
		x=a;y=b;z=c;i=d;
	}
}
 
class DisJoint {
	int[] arr;
	int[] parent;
	int[] rank;
	//TreeMap<Integer,Integer> min[];
	DisJoint(int n){
		arr=new int[n+1];
		parent=new int[n+1];
		rank=new int[n+1];
	//	min=new TreeMap[n+1];
		for(int i=1;i<=n;i++) {
			arr[i]=i;
			parent[i]=i;
			rank[i]=0;
			//min[i]=new TreeMap<>();
			
		}
	}
 
	int find(int value) {
		int par=parent[value];
		if(par==value)
			return par;
		parent[value]=find(par);
		return parent[value];
	}
	
	void union(int data1,int data2) {
		int parent1=find(data1);
		int parent2=find(data2);
		if(parent1!=parent2) {
			if(rank[parent1]>=rank[parent2]) {
				parent[parent2]=parent1;
				if(rank[parent1]==rank[parent2])
					rank[parent1]++;
			}
			else {
				parent[parent1]=parent2;
				
			}
		}
		
	}
	
}
 
 
 
 
 
 
 
 
 
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
    	 reader = new BufferedReader(
                 new FileReader("pattern.in") );
    tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
    	return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}