const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => {
        let next;
        do {
            next = (await getLineGen.next());
        } while (!next.done && next.value === '');
        return next.value;
    };
})();

const readInt = async function(){
    return parseInt(await getLine());
}

const readIntArray = async function() {
    return (await getLine()).split(' ').map(num => parseInt(num));
}

let solve = async () => {
    const nsc = await readInt();
    for(let sc = 0; sc < nsc; sc++) {
        const n = await readInt();
        rl.output.write(`? 1\n`);
        await readInt();
        const res = new Array(n + 1);
        let total = 1;
        const cycle = new Array(n + 1);
        let ncycle = 0;
        for (let i = 1; i <= n; i++) {
            if (!res[i]) {
                if (i===n) {
                    res[n] = n;
                    break;
                }
                ncycle = 0;
                const beforeTotal = total;
                while (true) {
                    rl.output.write(`? ${i}\n`);
                    // console.log(`? ${i}`);
                    const val = await readInt();
                    total++;
                    if (res[val])
                        break;
                    res[val] = true;
                    cycle[ncycle] = val;
                    ncycle++;
                }
                let cur = i;
                for (let j = (beforeTotal - 1) % ncycle, cnt = 1; cnt <= ncycle + 1; cnt++, j = (j + 1) % ncycle) {
                    res[cur] = cycle[j];
                    cur = res[cur];
                }
            }
        }

        rl.output.write(`! ${res.slice(1).join(' ')}\n`)
    }
}

solve();

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const g = main();
g.next()
rl.on('line', (input) => {
    // console.log('read', input)
    input.split('\n').forEach(s => s.trim() && g.next(s))
});

function* main() {
    let input

    input = yield 1
    const t = +input
    for (let i = 0; i < t; i++) {
        input = yield 1
        const n = +input
        yield* solve(n)
    }
    process.exit(0)
}

function* solve(n) {
    const ans = Array(n).fill(-1)
    for (let i = 0; i < n; i++) {
        if (ans[i] >= 0) continue

        const map = {}
        const arr = []
        while (1) {
            const x = yield* ask(i + 1)
            if (map[x]) break

            map[x] = 1
            arr.push(x)
        }
        // the next of `x` is `px`
        for (let j = 0; j < arr.length; j++) {
            ans[arr[j] - 1] = arr[(j + 1) % arr.length]
        }
    }
    console.log(`! ${ans.join(' ')}`)
}
function* ask(i) {
    console.log(`? ${i}`)
    const x = yield 1
    return +x
}

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => {
        let next;
        do {
            next = (await getLineGen.next());
        } while (!next.done && next.value === '');
        return next.value;
    };
})();

const readInt = async function(){
    return parseInt(await getLine());
}

const readIntArray = async function() {
    return (await getLine()).split(' ').map(num => parseInt(num));
}

let solve = async () => {
    const nsc = await readInt();
    for(let sc = 0; sc < nsc; sc++) {
        const n = await readInt();
        console.log(`? 1`);
        await readInt();
        const res = new Array(n + 1);
        let total = 1;
        const cycle = new Array(n + 1);
        let ncycle = 0;
        for (let i = 1; i <= n; i++) {
            if (!res[i]) {
                if (i===n) {
                    res[n] = n;
                    break;
                }
                ncycle = 0;
                const beforeTotal = total;
                while (true) {
                    console.log(`? ${i}`);
                    const val = await readInt();
                    total++;
                    if (res[val])
                        break;
                    res[val] = true;
                    cycle[ncycle] = val;
                    ncycle++;
                }
                let cur = i;
                for (let j = (total - 2) % ncycle, cnt = 1; cnt <= ncycle; cnt++, j = (j + 1) % ncycle) {
                    res[cur] = cycle[j];
                    cur = res[cur];
                }
            }
        }

        console.log(`! ${res.slice(1).join(' ')}`)
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => {
        let next;
        do {
            next = (await getLineGen.next());
        } while (!next.done && next.value === '');
        return next.value;
    };
})();

const readInt = async function(){
    return parseInt(await getLine());
}

const readIntArray = async function() {
    return (await getLine()).split(' ').map(num => parseInt(num));
}

let solve = async () => {
    const nsc = await readInt();
    for(let sc = 0; sc < nsc; sc++) {
        const n = await readInt();
        const res = new Array(n + 1);
        let total = 0;
        const cycle = new Array(n + 1);
        let ncycle = 0;
        for (let i = 1; i <= n; i++) {
            if (!res[i]) {
                ncycle = 0;
                const beforeTotal = total;
                while (true) {
                    console.log(`? ${i}`);
                    const val = await readInt();
                    total++;
                    if (res[val])
                        break;
                    res[val] = true;
                    cycle[ncycle] = val;
                    ncycle++;
                }
                let cur = i;
                for (let j = (total - 1) % ncycle, cnt = 1; cnt <= ncycle; cnt++, j = (j + 1) % ncycle) {
                    res[cur] = cycle[j];
                    cur = res[cur];
                }
            }
        }

        console.log(`! ${res.slice(1).join(' ')}`)
    }
}

solve();

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => {
        let next;
        do {
            next = (await getLineGen.next());
        } while (!next.done && next.value === '');
        return next.value;
    };
})();

const readInt = async function(){
    return parseInt(await getLine());
}

const readIntArray = async function() {
    return (await getLine()).split(' ').map(num => parseInt(num));
}

let solve = async () => {
    const n = await readInt();
    const res = new Array(n+1);
    let total = 0;
    const cycle = new Array(n + 1);
    const was = new Array(n+1);
    let ncycle = 0;
    for(let i = 1; i <= n; i++) {
        if (!res[i]) {
            ncycle = 0;
            const beforeTotal = total;
            while (true) {
                console.log(`? ${i}`);
                const val = await readInt();
                total++;
                if (res[val])
                    break;
                res[val] = true;
                cycle[ncycle] = val;
                ncycle++;
            }
            let cur = i;
            for(let j = (total - 1)%ncycle, cnt=1; cnt<=ncycle; cnt++, j=(j+1)%ncycle) {
               res[cur] = cycle[j];
               cur = res[cur];
            }
        }
    }

    console.log(`! ${res.slice(1).join(' ')}`)
}

solve();


'use strict'

readline();
const x = [ 'a' ], d = [];
readline().split('').forEach(c => {
    let i = 0;
    while (c < x[i]) if (++i === x.length) x.push(c);
    x[i] = c;
    d.push(i+1);
})
print(`${Math.max(...d)}\n${d.join(' ')}`)

'use strict'

readline();
const x = [ 'a' ], d = [];

readline().split('').forEach(c => {
    let i = 0, j = x.length - 1, m;
    if (c < x[j]) j++;
    else if (c >= x[i]) j = i;
    else while(i !== (m = (i + j) >> 1)) if (c < x[m]) i = m; else j = m;

    x[j] = c;
    d.push(j+1);
})
print(`${Math.max(...d)}\n${d.join(' ')}`)


'use strict'

const getLine = () => readline().split(' ')
const getInts = () => getLine().map(n => parseInt(n))
const getInt = () => getInts()[0]
const D = (...args) => { // D({name})
  [...args].forEach(cur => print.apply(print, [JSON.stringify(cur)]))
}

main()

function main() {
  const n = getInt()
  const cost = getInts()
  const words = []
  const wordsRev = []
  const INF = Number.MAX_SAFE_INTEGER

  for (let i = 0; i < n; ++i) {
    const w = getLine()[0]
    words.push(w)
    wordsRev.push(reve(w))
  }

  words.push(words[n - 1])
  wordsRev.push(wordsRev[n - 1])

  const dp = [[cost[0], 0]]
  for (let i = 1; i <= n; ++i) {
    dp.push([INF, INF])

    if (wordsRev[i - 1] <= wordsRev[i]) {
      dp[i][0] = dp[i - 1][0] + cost[i]
    }

    if (wordsRev[i - 1] <= words[i]) {
      dp[i][1] = dp[i - 1][0]
    }

    if (words[i - 1] <= wordsRev[i]) {
      dp[i][0] = Math.min(dp[i - 1][1] + cost[i], dp[i][0])
    }

    if (words[i - 1] <= words[i]) {
      dp[i][1] = Math.min(dp[i][1], dp[i - 1][1])
    }
  }

  let ans = Math.min(dp[n - 1][0], dp[n - 1][1])
  print(ans !== INF ? ans : -1)
}

function reve(w) {
  return w.split('').reverse().join('')
}

'use strict'

const getLine = () => readline().split(' ')
const getInts = () => getLine().map(n => parseInt(n))
const getInt = () => getInts()[0]
const D = (...args) => { // D({name})
  [...args].forEach(cur => print.apply(print, [JSON.stringify(cur)]))
}

main()

function main() {
  const n = getInt()
  const cost = getInts()
  const words = []
  const INF = Number.MAX_SAFE_INTEGER

  for (let i = 0; i < n; ++i) {
    const w = getLine()[0]
    words.push(w)
  }

  words.push(words[n - 1])

  let ok = true
  const dp = [[cost[0], 0]]
  for (let i = 1; i <= n; ++i) {
    let found = false
    dp.push([INF, INF])

    if (reve(words[i - 1]) <= reve(words[i])) {
      dp[i][0] = dp[i - 1][0] + cost[i]
      found = true
    }

    if (reve(words[i - 1]) <= words[i]) {
      dp[i][1] = dp[i - 1][0]
      found = true
    }

    if (words[i - 1] <= reve(words[i])) {
      dp[i][0] = Math.min(dp[i - 1][1] + cost[i], dp[i][0])
      found = true
    }

    if (words[i - 1] <= words[i]) {
      dp[i][1] = Math.min(dp[i][1], dp[i - 1][1])
      found = true
    }

    if (!found) ok = false
  }

  let ans = Math.min(dp[n - 1][0], dp[n - 1][1])
  print(ok && ans !== INF ? ans : -1)
}

function reve(w) {
  return w.split('').reverse().join('')
}

'use strict'

const getLine = () => readline().split(' ')
const getInts = () => getLine().map(n => parseInt(n))
const getInt = () => getInts()[0]
const D = (...args) => { // D({name})
  [...args].forEach(cur => print.apply(print, [JSON.stringify(cur)]))
}

main()

function main() {
  const n = getInt()
  const cost = getInts()
  const words = []

  for (let i = 0; i < n; ++i) {
    const w = getLine()[0]
    words.push(w)
  }

  words.push(words[n - 1])

  let ok = true
  const dp = [[cost[0], 0]]
  for (let i = 1; i <= n; ++i) {
    let found = false
    dp.push([1e10, 1e10])

    if (reve(words[i - 1]) <= reve(words[i])) {
      dp[i][0] = dp[i - 1][0] + cost[i]
      found = true
    }

    if (reve(words[i - 1]) <= words[i]) {
      dp[i][1] = dp[i - 1][0]
      found = true
    }

    if (words[i - 1] <= reve(words[i])) {
      dp[i][0] = Math.min(dp[i - 1][1] + cost[i], dp[i][0])
      found = true
    }

    if (words[i - 1] <= words[i]) {
      dp[i][1] = Math.min(dp[i][1], dp[i - 1][1])
      found = true
    }

    if (!found) ok = false
  }

  let ans = Math.min(dp[n - 1][0], dp[n - 1][1])
  print(ok ? ans : -1)
}

function reve(w) {
  return w.split('').reverse().join('')
}

'use strict'

const getLine = () => readline().split(' ')
const getInts = () => getLine().map(n => parseInt(n))
const getInt = () => getInts()[0]
const D = (...args) => { // D({name})
  [...args].forEach(cur => print.apply(print, [JSON.stringify(cur)]))
}

main()

function main() {
  const n = getInt()
  const cost = getInts()
  const words = []
  const INF = Number.MAX_SAFE_INTEGER

  for (let i = 0; i < n; ++i) {
    const w = getLine()[0]
    words.push(w)
  }

  words.push(words[n - 1])

  let ok = true
  const dp = [[cost[0], 0]]
  for (let i = 1; i <= n; ++i) {
    let found = false
    dp.push([INF, INF])

    if (reve(words[i - 1]) <= reve(words[i])) {
      dp[i][0] = dp[i - 1][0] + cost[i]
      found = true
    }

    if (reve(words[i - 1]) <= words[i]) {
      dp[i][1] = dp[i - 1][0]
      found = true
    }

    if (words[i - 1] <= reve(words[i])) {
      dp[i][0] = Math.min(dp[i - 1][1] + cost[i], dp[i][0])
      found = true
    }

    if (words[i - 1] <= words[i]) {
      dp[i][1] = Math.min(dp[i][1], dp[i - 1][1])
      found = true
    }

    if (!found) ok = false
  }

  let ans = Math.min(dp[n - 1][0], dp[n - 1][1])
  print(ok ? ans : -1)
}

function reve(w) {
  return w.split('').reverse().join('')
}


// var inputString = "5\n2 4\n2 2\n2 3\n2 3\n2 1\n";

// var readline = (function () {
//   var regex = /(.+)\s/g,
//       i = 0;

//   return function () {
//     return inputString.match(regex)[i++].replace(/\n/g, "");
//   }
// })();

// function write (token) {
//   console.log(token);
// }
// function print (token) {
//   console.log(token);
// }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

var days = parseInt(readline(), 10);

var meatNeed = [],
    price = [];

var tempInput = "";
for (var i = 0; i < days; i++) {
    tempInput = readline().split(" ");
    price.push(parseInt(tempInput.pop(), 10));
    meatNeed.push(parseInt(tempInput.pop(), 10));
}

var minPrice = {};
minPrice.price = Math.min.apply(null, price);
minPrice.day = price.indexOf(minPrice.price);

var minSum = 0;

var i = 0;
while (i < days) {
    if (price[i] < price[i+1]) {
        var j = i;
        while (price[i] <= price[j]) {
            minSum += price[i] * meatNeed[j];
            j++;
        }
        i = j;
    } else {
        minSum += price[i] * meatNeed[i];
        i++;
    }
}

write(minSum);

a=[],p=[], mm=[]
n=Number(readline())
for (var i=0; i<n; ++i) {
    X=readline().split(' ').map(Number)
    a.push(X[0])
    p.push(X[1])
}
mm.push(p[0])
for (var i=1; i<n; ++i) {
    mm[i]=Math.min(mm[i-1],p[i])
}
r=0
for (var i=0; i<n; ++i) {
    r+=mm[i]*a[i]
}
print(r)

n=Number(readline())
a=new Array(n), p=new Array(n), mm=new Array(n)
for (var i=0; i<n; ++i) {
    X=readline().split(' ').map(Number)
    a[i]=X[0]
    p[i]=X[1]
}
mm[0]=p[0]
for (var i=1; i<n; ++i) {
    mm[i]=Math.min(mm[i-1],p[i])
}
r=0
for (var i=0; i<n; ++i) {
    r+=mm[i]*a[i]
}
print(r)

if (!readline) {
    var _i = 0;
    var readline = function() {
        _i ++;

        switch (_i) {
            case 1: return '3';
            case 2: return '1 3';
            case 3: return '2 1';
            case 4: return '3 2';
        }
    }

    var write = console.log;
}

var n = +readline();

var a, p;

var s = 0;
var m;

for (var i = 0; i < n; i ++) {
    var x = readline().split(' ');

    a = +x[0];
    p = +x[1];

    if (m === undefined || m > p) {
        m = p;
    }

    s += a * m;
}

write(s);

"use strict";

var n = +readline();

var result = 0;
for (let i = 0, pmin = +Infinity; i < n; ++i) {
    let input = readline().split(' ').map(value => +value);
    let a = input[0];
    let p = input[1];

    pmin = Math.min(pmin, p);

    result += a * pmin;
}

write(result);

var n = Number(readline());
var inp;
var min = 101;
var sum = 0;
for(var i = 0; i < n ; i++){
    inp = readline().split(" ").map(cur => Number(cur));
    (inp[1] < min) ? min = inp[1] : null;
    sum += min*inp[0];
}

print(sum);

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let ans = 0;
let minPrice = Infinity;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  let [a, p] = d.split(' ').map(Number);

  if (a && p) {
    minPrice = Math.min(minPrice, p);
    ans += a * minPrice;
  }

  c++;
});

rl.on('close', () => {
  console.log(ans);
});

const input = [];

const RL = require('readline').createInterface({
   input : process.stdin,
   output : process.stdout
});

RL.on('line', (line) => {
    input.push(line);
});

RL.on('close', () => {
  const n = input[0].split(' ').map(x => parseInt(x));

  let [meat, cost] = input[1].split(' ').map(x => parseInt(x));
  let answer = meat*cost;

  for (let i = 2; i <= n; i += 1) {
    const pair = input[i].split(' ').map(x => parseInt(x));
    if (pair[1] < cost) {
      cost = pair[1];
    }
    answer += pair[0] * cost;
  }

  console.log(answer);
});

//Input
//                      ai pi          1 <= ai, pi <= 100
// var n = "3"; var arr = ["1 3", "2 2", "3 1"];       // 10
// var n = "3"; var arr = ["1 3", "2 2", "3 2"];       // 8
// var n = "1"; var arr = ["39 52"];                   // 2028
// var n = "2"; var arr = ["125 56", "94 17"];         // 2998
// var n = "5"; var arr = ["39 213", "95 89", "70 90", "9 55", "85 32"];       // 6321
// var n = "12"; var arr = ["70 11",  "74 27",  "32 11",  "26 83",  "57 18",  "97 28",  "75 43",  "75 21",  "84 29",  "16 2",  "89 63",  "21 88"];       // 6742

var n = readline();

// retorno = eval(n, arr);
retorno = eval(n);

//Solution
// function eval(n, arr){
function eval(n){
    n = parseInt(n);
    var daysCosts = new Array(n+1).join("").split("");

    var arr = "";
    for(var i = 0 ; i < n ; i++){
        arr = readline();
        daysCosts[i] = arr.split(" ").map(function(x){ return parseInt(x)});
        // daysCosts[i] = arr[i].split(" ").map(function(x){ return parseInt(x)});
    };
    write(giveTheLowestCost(n, daysCosts));
    // console.log(giveTheLowestCost(n, daysCosts));
};

function giveTheLowestCost(n, daysCosts){
    var answer;
    var minimumCost = 0;
    var totalCost = 0;
    //console.log(daysCosts[0]);
    for(var i = 0 ; i < n ; i++){
        if(i == 0){
            minimumCost = daysCosts[i][1];
            totalCost = daysCosts[i][0]*daysCosts[i][1];
            //console.log("minimumCost: " + minimumCost + "    totalCost: " + totalCost);
            //console.log("dayCosts[" + i + "][1]: " + daysCosts[i][1] + "    minimumCost: " + minimumCost);
        } else{
            if(minimumCost > daysCosts[i][1]){
                minimumCost = daysCosts[i][1];
            };
            totalCost += daysCosts[i][0]*minimumCost;
            //console.log("dayCosts[" + i + "][1]: " + daysCosts[i][1] + "    minimumCost: " + minimumCost);
        };
    };

    //console.log("minimumCost: " + minimumCost + "    totalCost: " + totalCost);
    return totalCost;
};

function cmp(x){
	return parseInt(x);
}
var n = parseInt(readline());
	var a = [];
	for(var i = 0; i < n; i++)
	{
		a[i] = readline().split(" ").map(cmp);
	}
	//var a = [[1,3],[2,1],[3,2]];
	var minimum = 200;
	var sum = 0;
	for(var j = 0; j < n; j ++)
	{
		if(a[j][1] < minimum)
		minimum = a[j][1];
		sum += minimum*a[j][0];
	}
	print(sum);

var n =  readline();///.split(" ");
var str = [];  //readline().split(" "); "3 3".split(" "),"3 3".split(" "),"3 3".split(" ")
var min = 100;
var sum = 0;
		for (var i=0; i<+n; i++){
			str.push(readline().split(" "));
		}

		for (var i=0; i<+n; i++){
		    if (+str[i][1]<min){
			sum += +str[i][1]*str[i][0];
			 min = +str[i][1];}
			else {
				sum +=str[i][0]*min;
			}

		}
	 print(sum);

var d = parseInt(readline());
var lines = [];
var f = function(elem){
    return parseInt(elem);
};
for(var i = 0; i < d; i++)
    lines.push(readline().split(" ").map(f));

    var m = lines[0];
    var c = m[0] * m[1];
    for(var i = 1; i < lines.length;i++){
        if(lines[i][1] < m[1]) m = lines[i];
        c += lines[i][0] * m[1];
    }

    print(c);

var n = Number(readline());

var price = Infinity, result = 0;
for (var i = 0; i<n; i++) {
    var s = readline().split(' ').map(Number);
    price = Math.min(price, s[1]);
    result += s[0] * price;
}
print(result);

var n, a = [], p = [], args = [], result;
var total = 0, prev = 0;

function main() {
    n = +readline();

    for (var i = 0; i < n; i++) {
        args = readline().split(" ").map(function(v) {return parseInt(v, 10)});
        a.push(args[0]);
        total += a[i];
        p.push(args[1]);
    }

    result = total * p[0];
    total -= a[0];
    prev = 0;
    for (i = 1; i < n; i++) {
        if (result > result - total * p[prev] + total * p[i]) {
            result = result - total * p[prev] + total * p[i];
            prev = i;
        }
        total -= a[i];
    }

    print(result);
}

main();

n=Number(readline())
a=new Array(n), p=new Array(n), mm=new Array(n)
for (var i=0; i<n; ++i) {
    X=readline().split(' ').map(Number)
    a[i]=X[0]
    p[i]=X[1]
}
mm[0]=p[0]
r=p[0]*a[0]
for (var i=1; i<n; ++i) {
    mm[i]=Math.min(mm[i-1],p[i])
    r+=mm[i]*a[i]
}

print(r)

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let ans = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  let [a, p] = d.split(' ').map(Number);

  ans += a * p;
  c++;
});

rl.on('close', () => {
  console.log(ans);
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let ans = 0;
let minPrice = Infinity;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  let [a, p] = d.split(' ').map(Number);
  minPrice = Math.min(minPrice, p);

  ans += a * minPrice;
  c++;
});

rl.on('close', () => {
  console.log(ans);
});

var n = parseInt(readline());
	var a = [];
	for(var i = 0; i < n; i++)
	{
		a[i] = readline().split(" ").map(function(elment){return parseInt(elment);});
	}

	//var a=[[1,3],[2,1],[3,2]];
	var minimum = 200;
	var temp;
	for(var j = 0; j < n; j++)
	{
		minimum = Math.min(minimum,a[j][1]);
	}
	for(var k = 0; k < n; k++)
	{
		if(minimum === a[k][1])
		{
			temp = k;
			break;
		}
	}
	var ans=0;
	for(var l = 0; l <= temp; l++)
	{
		ans += a[l][0]*a[l][1];
	}
	for(var u = temp+1; u < n; u++)
	{
		ans += a[u][0]*minimum;
	}
	print(ans);

var n =  readline();///.split(" ");
var str = [];  //readline().split(" ");
var min = 100;
var sum = 0;
		for (var i=0; i<+n; i++){
			str.push([readline().split(" ")]);
		}

		for (var i=0; i<+n; i++){
		    if (+str[i][1]<min){
			sum += +str[i][1]*str[i][0];
			 min = +str[i][1];}
			else {
				sum +=str[i][0]*min;
			}

		}
	 print(sum);

var n, a = [], p = [], args = [], result;
var total = 0;

function main() {
    n = +readline();

    for (var i = 0; i < n; i++) {
        args = readline().split(" ").map(function(v) {return parseInt(v, 10)});
        a.push(args[0]);
        total += a[i];
        p.push(args[1]);
    }

    result = total * p[0];
    total -= a[0];
    for (i = 1; i < n; i++) {
        if (result > result - total * p[i - 1] + total * p[i]) {
            result = result - total * p[i - 1] + total * p[i];
        }
        total -= a[i];
    }

    print(result);
}

main();

// var inputString = "3\n1 3\n2 1\n3 2\n";

// var readline = (function () {
//   var regex = /(.+)\s/g,
//       i = 0;

//   return function () {
//     return inputString.match(regex)[i++].replace(/\n/g, "");
//   }
// })();

// function write (token) {
//   console.log(token);
// }
// function print (token) {
//   console.log(token);
// }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

var days = parseInt(readline(), 10);

var meatNeed = [],
    price = [];

var tempInput = "";
for (var i = 0; i < days; i++) {
    tempInput = readline().split(" ");
    price.push(parseInt(tempInput.pop(), 10));
    meatNeed.push(parseInt(tempInput.pop(), 10));
}

var minPrice = {};
minPrice.price = Math.min.apply(null, price);
minPrice.day = price.indexOf(minPrice.price);

var minSum = 0;

for (var i = 0; i < days; i++) {
    if (i < minPrice.day) {
        minSum += meatNeed.shift() * price.shift();
    } else {
        minSum += meatNeed.reduce(function (sum, cur) {
            return sum + cur * minPrice.price;
        }, 0) * minPrice.price;
        break;
    }
}

write(minSum);

// var inputString = "5\n5 5\n4 4\n3 3\n2 2\n2 2\n";

// var readline = (function () {
//   var regex = /(.+)\s/g,
//       i = 0;

//   return function () {
//     return inputString.match(regex)[i++].replace(/\n/g, "");
//   }
// })();

// function write (token) {
//   console.log(token);
// }
// function print (token) {
//   console.log(token);
// }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

var days = parseInt(readline(), 10);

var meatNeed = [],
    price = [];

var tempInput = "";
for (var i = 0; i < days; i++) {
    tempInput = readline().split(" ");
    price.push(parseInt(tempInput.pop(), 10));
    meatNeed.push(parseInt(tempInput.pop(), 10));
}

var minPrice = {};
minPrice.price = Math.min.apply(null, price);
minPrice.day = price.indexOf(minPrice.price);

var minSum = 0;

for (var i = 0; i < days; i++) {
    if (i < minPrice.day) {
        minSum += meatNeed.shift() * price.shift();
    } else {
        minSum += meatNeed.reduce(function (sum, cur) {
            return sum + cur * minPrice.price;
        }, 0);
        break;
    }
}

write(minSum);