var n;
var x = {};

function main(){
	n = scan();
	rep(n, function(i){
		var s = scan('string');
		var p = scan();
		if(x.hasOwnProperty(s)){
			x[s] += p;
		}
		else{
			x[s] = p;
		}
	});
	var y = [];
	for(var i in x){
		if(x.hasOwnProperty(i)){
			y.push([i, x[i]]);
		}
	}
	y.sort(function(a, b){
		if(a[0].length != b[0].length){
			return a[0].length - b[0].length;
		}
		if(a[0] > b[0]){
			return 1;
		}
		else if(a[0] < b[0]){
			return -1;
		}
		else{
			return 0;
		}
	});
	rep(y.length, function(i){
		print(y[i].join(' '));
	});
}

function rep(a, b, c){
	if(c === undefined){
		c = b;
		b = a;
		a = 0;
	}
	for(var i = a; i < b; ++i){
		if(c(i) === false){
			break;
		}
	}
}
 
process.stdin.resume();
process.stdin.setEncoding('utf8');
 
var input = '';
var input_index = 0;
 
function scan(type){
	if(type === 'string'){
		return input[input_index++];
	}
	else{
		return +input[input_index++];
	}
}
 
function print(val){
	console.log(val);
}
 
process.stdin.on('data', function(chunk){
	input += chunk;
});
process.stdin.on('end', function(){
	input = input.trim().split(/\s+/);
	main();
});