function main() {
    var numBadges = parseInt(readline());

    var badges = readline().split(" ");

    badges.sort(function(a, b) {
        return a - b;
    });
    for(var i = 0; i < badges.length; ++i) {
        badges[i] = parseInt(badges[i]);
    }
    //print(badges);
    var numChanges = 0;
    for(var i = 0; i < badges.length-1; i++) {
        while(badges[i] >= badges[i+1]) {
            ++numChanges;
            ++badges[i+1];
        }
    }
    return numChanges;

}

print(main());

function Stack(){this.a=[]}function Queue(){this.l=new Stack,this.r=new Stack}function Reader(){this.b=[],this.c=0}function Writer(){this.l=[""],this.c=0}Array.prototype.back=function(){return this[this.length-1]},Math.gcd=function(t,i){return i?gcd(i,t%i):t},Math.lcm=function(t,i){return t/gcd(t,i)*i},Math.sign=function(t){return(t>0)-(0>t)},Math.cmp=function(t,i){return(+t>+i)-(+i>+t)},Math.icmp=function(t,i){return(+i>+t)-(+t>+i)},Stack.prototype.empty=function(){return 0==this.a.length},Stack.prototype.push=function(t){this.a.push(t)},Stack.prototype.pop=function(){return this.empty()?void 0:this.a.pop()},Stack.prototype.top=function(){return this.empty()?void 0:this.a[this.a.length-1]},Stack.prototype.clear=function(){this.a=[]},Stack.prototype.size=function(){return this.a.length},Queue.prototype.push=function(t){this.l.push(t)},Queue.prototype.empty=function(){return this.r.empty()&&this.l.empty()},Queue.prototype.pop=function(){if(this.r.empty())for(;!this.l.empty();)this.r.push(this.l.pop());return this.r.empty()?void 0:this.r.pop()},Queue.prototype.front=function(){if(this.r.empty())for(;!this.l.empty();)this.r.push(this.l.pop());return this.r.empty()?void 0:this.r.top()},Queue.prototype.size=function(){return l.size()+r.size()},Queue.prototype.clear=function(){l.clear(),r.clear()},Reader.prototype.next=function(){return this.c==this.b.length&&(this.c=0,this.b=readline().split(" ")),this.b[this.c++]},Writer.prototype.print=function(t){this.l[this.c]+=t},Writer.prototype.println=function(t){this.l[this.c]+=t,this.l.push(""),++this.c},Writer.prototype.flush=function(){print(this.l.join("\n")),this.l=[]};

var cnt = new Array(10000);

function main(cin, cout)
{
    var n = +cin.next();
    var t;
    for(var i = 0; i < 10000; ++i)
        cnt[i] = 0;
    for(var i = 0; i < n; ++i)
    {
        t = +cin.next();
        cnt[t]++;
    }
    var ans = 0;
    for(var i = 1; i <= n; ++i)
    {
        while(cnt[i] > 1)
        {
            var j;
            for(j = i + 1; cnt[j]; ++j);
            --cnt[i];
            ++cnt[j];
            ans += j - i;
        }
    }
    cout.print(ans);
}

var cin = new Reader();
var cout = new Writer();

main(cin, cout);

cout.flush();

/**
 * Created by Det on 25.05.2015.
 */
(function main() {
    var line1 = readline().split(' ');
    var line2 = readline().split(' ');
    var money = 0;
    var sold = [];
    var lish = [];

    for (var i=0; i < line2.length; i++){
        if (sold[line2[i]]) {
            lish.push(sold[line2[i]]);
        } else {
            sold[line2[i]] = line2[i];
        }
    }
    for (var i=0; i < lish.length; i++) {
        for (var j=+lish[i]; j < (sold.length + lish.length); j++) {
            if(!sold[j]){
                sold[j] = 1;
                money += j - lish[i];
                break;
            }
        }
    }

    print(money);
})();

function ProblemSolver() {
    this.HAS_TEST_CASES = false;
    this.INPUT_FILE_NAME = "test.in";
    this.OUTPUT_FILE_NAME = "out.txt";
    this.DO_OUTPUT_TO_FILE = false;
    this.CLEAR_ARRAY_PER_CASE = false;

    this.solveCase = function() {
        var res , i , j , fl , cn , temp ;
        res = 0 ;
        this.arr = this.arr.sort( function( left , right ) { return left - right ; } ) ;
        prev = -1 ;
        for( i = 0 ; i < this.n ; i++ ) {
        	if( this.arr[ i ] <= prev ) {
        		res += prev + 1 - this.arr[ i ] ;
        		prev = prev + 1 ;
        	}
        	else {
        		prev = this.arr[ i ] ;
        	}
        }
        print( res );
    };

    this.init = function() {
        this.lim1 = 100010;
        this.lim2 = 110;
        this.cc = 1;
        this.ind = 1;
        this.n = 0;
        this.cn = 0;
        this.declareAndFillArrays();
    };

    this.getInput = function( irObj ) {
        var hasMoreInput , i;
        hasMoreInput = true;
        try {
            this.n = irObj.nextInt();
            this.arr = [] ;
            for( i = 0 ; i < this.n ; i++ ) {
                this.arr.push( irObj.nextInt() ) ;
            }
        }
        catch( ex ) {
            hasMoreInput = false;
        }
        return hasMoreInput;
    };

    this.clearArraysPerCase = function() {
        var i;
        this.arr = new Array();
        this.adj_list = new Array();
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.arr.push( 0 );
            this.adj_list.push( new Array() );
        }
    };

    this.clearPerCase = function() {
        this.cn = 0;
        this.cc++;
        if( this.CLEAR_ARRAY_PER_CASE == true ) {
            this.clearArraysPerCase() ;
        }
    };

    this.declareAndFillArrays = function() {
        var i , j;
        this.srr = new Array();
        this.vis = new Array();
        this.arr = new Array();
        this.brr = new Array();
        this.memo = new Array();
        this.done = new Array();
        this.adj_list = new Array();
        for( i = 0 ; i < this.lim1 ; i++ ) {
            this.srr.push( "" );
            this.vis.push( 0 );
            this.arr.push( 0 );
            this.brr.push( 0 );
            this.adj_list.push( new Array() );
        }
        for( i = 0 ; i < this.lim2 ; i++ ) {
            this.memo.push( new Array() );
            this.done.push( new Array() );
            for( j = 0 ; j < this.lim2 ; j++ ) {
                this.memo[ i ].push( -1 );
                this.done[ i ].push( 0 );
            }
        }
    };

    this.init();
}

function InputReader() {
    var self = this ;
    this.allLines = new Array();
    this.currrentLineNumber = 0;
    this.currrentCharacterIndex = 0;
    this.callBackFunction = null ;
    this.parentContext = null ;

    this.readAllLines = function() {
        var singleLine;
        while( true ) {
            try {
                singleLine = readline();
                if( singleLine == null ) {
                    break;
                }
            }
            catch( ex ) {
                break;
            }
            this.allLines.push( singleLine );
        }
    };

    this.readAllLinesFromRhino = function() {
        var brObj , line ;
        importPackage( java.io ) ;
        importPackage( java.lang ) ;
        brObj = new BufferedReader( new InputStreamReader( System[ 'in' ] ) ) ;
        this.allLines = [] ;
        while( true ) {
            line = brObj.readLine() ;
            if( line == null ) {
                break;
            }
            this.allLines.push( line ) ;
        }
    };

    this.readSingleLinesFromNodeJsJudgeServer = function( chunk ) {
        self.chunkData += chunk ;
    };

    this.readEndFromNodeJsJudgeServer = function() {
        self.parseRawData( self.chunkData ) ;
        self.parentContext.runCases() ;
    };

    this.readAllLinesFromNodeJsJudgeServer = function( parentContext ) {
        this.parentContext = parentContext ;
        process.stdin.resume() ;
        process.stdin.setEncoding( 'utf8' ) ;
        this.chunkData = '' ;
        process.stdin.on( 'data' , this.readSingleLinesFromNodeJsJudgeServer ) ;
        process.stdin.on( 'end' , this.readEndFromNodeJsJudgeServer ) ;
    };

    this.parseRawData = function( data ) {
        var len , i , currentString;
        len = data.length;
        currentString = "";
        this.allLines = [] ;
        for( i = 0 ; i < len ; i++ ) {
            if( data[ i ] == '\r' ) {
            }
            else if( data[ i ] == '\n' ) {
                this.allLines.push( currentString );
                currentString = "";
            }
            else {
                currentString += data[ i ];
            }
        }
        if( currentString != "" ) {
            this.allLines.push( currentString );
        }
    };

    this.readAllLinesFromFile = function( inputFileName ) {
        var currentDirectory , fsObj , inputFilePath , rawData;
        fsObj = require( 'fs' );
        currentDirectory = __dirname;
        inputFilePath = currentDirectory + "\\" + inputFileName;
        rawData = fsObj.readFileSync( inputFilePath , "utf8" );
        this.parseRawData( rawData );
    };

    this.next = function( flag ) {
        var len , i , startIdx , res , endIdx;
        if( flag == 0 ) {
            if( this.currrentCharacterIndex != 0 ) {
                this.currrentLineNumber++;
                this.currrentCharacterIndex = 0;
            }
            res = "";
            len = this.allLines[ this.currrentLineNumber ].length;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                res += '' + this.allLines[ this.currrentLineNumber ].charAt( i ) ;
            }
            this.currrentLineNumber++;
            this.currrentCharacterIndex = 0;
            return res;
        }
        while( true ) {
            if( this.currrentLineNumber >= this.allLines.length ) {
                throw new Error( "No more tokens available!" );
            }
            startIdx = -1;
            len = this.allLines[ this.currrentLineNumber ].length;
            if( typeof( len ) == 'function' ) {
                len = this.allLines[ this.currrentLineNumber ].length() ;
            }
            for( i = this.currrentCharacterIndex ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) != ' '.charCodeAt( 0 ) ) {
                    startIdx = i;
                    break;
                }
            }
            if( startIdx == -1 ) {
                this.currrentLineNumber++;
                this.currrentCharacterIndex = 0;
                continue;
            }
            res = "";
            endIdx = len - 1 ;
            for( i = startIdx ; i < len ; i++ ) {
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) == ' '.charCodeAt( 0 ) ) {
                    endIdx = i;
                    break;
                }
                if( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) >= 48 && this.allLines[ this.currrentLineNumber ].charCodeAt( i ) <= 57 ) {
                    res += '' + ( this.allLines[ this.currrentLineNumber ].charCodeAt( i ) - 48 ) ;
                }
                else {
                    res += '' + this.allLines[ this.currrentLineNumber ].charAt( i );
                }
            }
            this.currrentCharacterIndex = endIdx;
            if( endIdx == len - 1 ) {
                this.currrentLineNumber++;
                this.currrentCharacterIndex = 0;
            }
            return res;
        }
    };

    this.nextInt = function() {
        return parseInt( this.next( 1 ) );
    };

    this.nextDouble = function() {
        return parseFloat( this.next( 1 ) );
    };

    this.nextString = function() {
        return this.next( 1 );
    };

    this.nextLine = function() {
        return this.next( 0 );
    };
}

function FileOutputHandler() {
    this.resultantStringArray = new Array();

    this.addNewLine = function( newString ) {
        this.resultantStringArray.push( newString );
    };

    this.clearPercase = function() {
        this.resultantStringArray = new Array();
    };

    this.flushOutOutputToFile = function( outputFileName ) {
        var i , sz , res , currentDirectory , fsObj , outputFilePath;
        res = "";
        sz = this.resultantStringArray.length;
        for( i = 0 ; i < sz ; i++ ) {
            if( i != 0 ) {
                res += "\n";
            }
            res += this.resultantStringArray[ i ];
        }
        fsObj = require( 'fs' );
        currentDirectory = __dirname;
        outputFilePath = currentDirectory + "\\" + outputFileName;
        fsObj.writeFileSync( outputFilePath , res );
        this.clearPercase();
    };
}

function CodeExecutioner() {
    this.irObj = new InputReader();
    this.psObj = new ProblemSolver();
    this.fohObj = new FileOutputHandler();

    this.runCases = function() {
        if( this.psObj.HAS_TEST_CASES == true ) {
            testCases = this.irObj.nextInt();
            for( i = 0 ; i < testCases ; i++ ) {
                this.psObj.clearPerCase();
                this.psObj.getInput( this.irObj );
                this.psObj.solveCase();
            }
        }
        else {
            while( true ) {
                this.psObj.clearPerCase();
                hasMoreTestCases = this.psObj.getInput( this.irObj );
                if( hasMoreTestCases == false ) {
                    break;
                }
                this.psObj.solveCase();
            }
        }
        if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
            this.fohObj.flushOutOutputToFile( this.psObj.OUTPUT_FILE_NAME );
        }
    };

    this.detectEnvironmentType = function() {
        var environmentType = null ;
        try {
            if( importPackage != null ) {
                environmentType = 'rhino' ;
            }
        }
        catch( ex1 ) {
            try {
                //for nodejs local server check
                if( __dirname != null && readline != null && typeof( readline ) != 'function' ) {
                    environmentType = 'local-node-js' ;
                }
            }
            catch( ex2 ) {
                try {
                    if( readline == null || typeof( readline ) != 'function' ) {
                        environmentType = 'server-node-js' ;
                    }
                    else {
                        try {
                            if( Map != null ) {
                                environmentType = 'spider-monkey' ;
                            }
                            else {
                                environmentType = 'javascript-v8' ;
                            }
                        }
                        catch( ex3 ) {
                            environmentType = 'javascript-v8' ;
                        }
                    }
                }
                catch( ex3 ) {
                    environmentType = 'server-node-js' ;
                }
            }
        }
        return environmentType ;
    };

    this.configureStreamsAndReadInput = function() {
        var testCases , i , hasMoreTestCases , isLocal , localContext , isNodeJsJudgeServer , environmentType ;
        isNodeJsJudgeServer = false ;
        environmentType = this.detectEnvironmentType() ;
        if( environmentType == 'local-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log;
                }
            }
            catch( ex ) {
                print = console.log;
            }
            if( this.psObj.DO_OUTPUT_TO_FILE == true ) {
                localContext = this.fohObj;
                print = function() {
                    localContext.addNewLine.apply( localContext , arguments );
                }
            }
            this.irObj.readAllLinesFromFile( this.psObj.INPUT_FILE_NAME );
            this.runCases() ;
        }
        else if( environmentType == 'server-node-js' ) {
            try {
                if( print == null || typeof( print ) != 'function' ) {
                    print = console.log;
                }
            }
            catch( ex ) {
                print = console.log;
            }
            this.irObj.readAllLinesFromNodeJsJudgeServer( this ) ;
        }
        else if( environmentType == 'javascript-v8' ) {
            this.irObj.readAllLines();
            this.runCases() ;
        }
        else if( environmentType == 'rhino' ) {
            this.irObj.readAllLinesFromRhino();
            this.runCases() ;
        }
        else if( environmentType == 'spider-monkey' ) {
            this.irObj.readAllLines();
            this.runCases() ;
        }
    };

    this.configureStreamsAndReadInput();
}

new CodeExecutioner();

function countUniqueElements(input,output){
	for(var i = 0; i < input.length; i++){
		output[+input[i]]++;
	}
}

var n = +readline(), a = readline().split(" ").map(Number);
var signs = {}, cnt = 0, x = 0;

for(i = 1; i <= n*2; i++){
	signs[i] = 0;
}

countUniqueElements(a,signs);

for(i = 1; i <= n; i++){
	if(signs[i] > 1){
		for(j = i; j <= n*2; j++){
			if(signs[j] == 0){
				signs[i]--;
				signs[j]++;
				cnt += j - i;
			}
			if(signs[i] == 1){
				break;
			}
		}
	}
}

write(cnt);

function countUniqueElements(input,output){
	for(var i = 0; i < input.length; i++){
		output[+input[i]]++;
	}
}

var n = +readline(), a = readline().split(" ").map(Number);
var signs = {}, cnt = 0;

for(i = 1; i <= n; i++){
	signs[i] = 0;
}

countUniqueElements(a,signs);

for(sign in signs){
	if(signs[sign] == 0){
		cnt++;
	}
}

write(cnt);

function countUniqueElements(input,output){
	for(var i = 0; i < input.length; i++){
		output[+input[i]]++;
	}
}

var n = +readline(), a = readline().split(" ").map(Number);
var signs = {}, cnt = 0;

for(i = 1; i <= n; i++){
	signs[i] = 0;
}

countUniqueElements(a,signs);

for(i = 1; i <= n; i++){
	if(signs[i] > 1){
		for(j = signs[i]; j <= n; j++){
			if(signs[j] == 0){
				signs[i]--;
				signs[j]++;
				cnt += j - i;
			}
		}
	}
}

write(cnt);

function main() {
    var numBadges = parseInt(readline());

    var badges = readline().split(" ");
    badges.map(parseInt);

    badges.sort();
    var numChanges = 0;
    for(var i = 0; i < badges.length-1; i++) {
        if(badges[i] == badges[i+1]) {
            ++numChanges;
            ++badges[i+1];
        }
    }
    return numChanges;

}

print(main());

function main() {
    var numBadges = parseInt(readline());

    var badges = readline().split(" ");
    badges.map(parseInt);

    badges.sort(function(a, b) {
        return a - b;
    });
    var numChanges = 0;
    for(var i = 0; i < badges.length-1; i++) {
        if(badges[i] == badges[i+1]) {
            ++numChanges;
            ++badges[i+1];
        }
    }
    return numChanges;

}

print(main());

function main() {
    var numBadges = parseInt(readline());

    var badges = readline().split(" ");
    badges.map(parseInt);

    badges.sort(function(a, b) {
        return a - b;
    });
    //print(badges);
    var numChanges = 0;
    for(var i = 0; i < badges.length-1; i++) {
        while(badges[i] >= badges[i+1]) {
            ++numChanges;
            ++badges[i+1];
        }
    }
    return numChanges;

}

print(main());


// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;

  function* genMain() {
    let n = yield;
    if (n == 1) {
      console.log('! 1');
      process.exit();
    }
    let a = Array(n + 1);
    let i = 1;
    let j = 2;
    while (1) {
      while (i <= n && a[i] || i == j) i++;
      while (j <= n && a[j] || i == j) j++;
      if (i > n || j > n) break;
      console.log('?', i, j);
      let mij = yield;
      console.log('?', j, i);
      let mji = yield;
      if (mij > mji) a[i++] = mij;
      else a[j++] = mji;
    }
    For(1, n, 1, i => a[i] ? null : a[i] = n);
    console.log('!', a.slice(1).join(' '));
    process.exit();
  }
  let main = genMain();
  main.next();
  process.stdin.on('data', c => main.next(+c));

})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;

  let inputResolve;
  let inputNext = i => new Promise(resolve => inputResolve = resolve);
  process.stdin.on('data', c => inputResolve(+c));
  let n = await inputNext();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  // setTimeout(() => {
  //   process.stdout.write(`! ${a.filter(v => v).length} ${Arr(n - 1, 1, 0, i => i).join(' ')}\n`);
  //   process.exit();
  // }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await inputNext()
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await inputNext()
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;

  let inputResolve;
  let inputNext = i => new Promise(resolve => inputResolve = resolve);
  process.stdin.on('data', c => inputResolve(+c));
  let n = await inputNext();
  if (n == 1) {
    console.log('! 1');
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  // setTimeout(() => {
  //   process.stdout.write(`! ${a.filter(v => v).length} ${Arr(n - 1, 1, 0, i => i).join(' ')}\n`);
  //   process.exit();
  // }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    console.log('?', i, j);
    let mij = await inputNext()
    console.log('?', j, i);
    let mji = await inputNext()
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  console.log('!', a.slice(1).join(' '));
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;
  let next = i => new Promise(resolve => process.stdin.on('data', c => resolve(+c)));
  let n = await next();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  setTimeout(() => {
    process.stdout.write(`! ${a.filter(v => v).length}\n`);
    process.exit();
  }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await next();
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await next();
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;
  let next = i => new Promise(resolve => process.stdin.on('data', c => resolve(+c)));
  let n = await next();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  setTimeout(() => {
    process.stdout.write(`! ${Arr(n, 1, 0, i => i).join(' ')}\n`);
    process.exit();
  }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await next();
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await next();
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;
  let next = i => new Promise(resolve => process.stdin.on('data', c => resolve(+c)));
  let n = await next();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  setTimeout(() => {
    process.stdout.write(`! ${a.filter(v => v).length} ${Arr(n - 1, 1, 0, i => i).join(' ')}\n`);
    process.exit();
  }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await next();
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await next();
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;
  let next = i => new Promise(resolve => process.stdin.on('data', c => resolve(+c)));
  let n = await next();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  setTimeout(() => {
    process.stdout.write(`! ${a.filter(v => v).length} ${a.slice(1).join(' ')}\n`);
    process.exit();
  }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await next();
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await next();
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();

// let input = '';
// let inputResolve;
// let inputPromise = new Promise(resolve => inputResolve = resolve);
// process.stdin.on('data', c => input += c);
// process.stdin.on('end', () => inputResolve());

function For(a, b, step, fn) {
  let res;
  for (let i = a; i <= b; i += step) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

(async () => {
  // await inputPromise;
  // let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
  //   out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a;
  let next = i => new Promise(resolve => process.stdin.on('data', c => resolve(+c)));
  let n = await next();
  if (n == 1) {
    process.stdout.write(`! 1\n`);
    process.exit();
  }
  let a = Array(n + 1);
  let i = 1;
  let j = 2;
  setTimeout(() => {
    process.stdout.write(`! ${Arr(n, 1, -1, i => i).join(' ')}\n`);
    process.exit();
  }, 800);
  while (1) {
    while (i <= n && a[i] || i == j) i++;
    while (j <= n && a[j] || i == j) j++;
    if (i > n || j > n) break;
    process.stdout.write(`? ${i} ${j}\n`);
    let mij = await next();
    process.stdout.write(`? ${j} ${i}\n`);
    let mji = await next();
    if (mij > mji) a[i++] = mij;
    else a[j++] = mji;
  }
  For(1, n, 1, i => a[i] ? null : a[i] = n);
  process.stdout.write(`! ${a.slice(1).join(' ')}\n`);
  process.exit();
})();


const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const [n, m, k, q] = lines[l++].trim().split(' ').map(Number)
        const qs = lines.slice(l, l + q).map(str => str.trim().split(' ').map(Number))
        l += q
        output[i] = solve(n, m, k, qs)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, m, k, qs) {
    const rows = {}
    const cols = {}
    let cr = 0
    let cc = 0
    let count = 0
    for (let i = qs.length - 1; i >= 0; i--) {
        let [x, y] = qs[i]
        x--; y--
        //
        let ok = 0
        if (!rows[x] && cc < m) {
            ok = 1
        }
        if (!cols[y] && cr < n) {
            ok = 1
        }
        if (ok) count++
        // if (n > 1 && m > 1) {
        // } else if (n > 1) {
        //     if (!rows[x] && cc < m) {
        //         count++
        //     }
        // // } else if (m > 1) {
        // } else {
        //     if (!cols[y] && cr < n) {
        //         count++
        //     }
        // }
        //
        if (!rows[x]) {
            rows[x] = 1
            cr++
        }
        if (!cols[y]) {
            cols[y] = 1
            cc++
        }
        // console.log(cr, cc)
    }
    return pow(k, count)
}

const MOD = 998244353
const MOD_CUT = ((1 << 20) * (1 << 20)) % MOD
function add(a, b) {
    return (a + b) % MOD
}
function minus(a, b) {
    return add(add(a, -b), MOD)
}
function mul(a, b) {
    let r = (a >> 20) * (b >> 20) * MOD_CUT
        + (a & 0xfff00000) * (b & 0xfffff)
        + (a & 0xfffff) * b
    return r % MOD
}
function pow(a, b) {
    let r = 1
    let base = a
    while (b) {
        if (b & 1) {
            r = mul(r, base)
        }
        b >>= 1
        base = mul(base, base)
    }
    return r
}

'use strict';

function solve(a, b) {
  let [n, m, k, q] = a;
  let visr = new Set(),
      visc = new Set(),
      res = 1,
      vx = new Set(),
      vy = new Set(),
      sx = 0,
      sy = 0;
  for (let i = q - 1; i >= 0; i--) {
    let [x, y] = b[i];
    x--, y--;
    if (!visr.has(x) && sx < m || !visc.has(y) && sy < n) {
      res = mul(res, k);
    }
    if (sx === m && sy === n) break;
    if (!visr.has(x)) visr.add(x);
    if (!visc.has(y)) visc.add(y);
    if (!vx.has(y)) {
      vx.add(y);
      sx++;
    }
    if (!vy.has(x)) {
      vy.add(x);
      sy++;
    }
  }
  return res;
}
const MOD = 998244353;
function mul(...args) {
  if (args.length === 0) throw new Error('参数不能为空');
  if (args.length === 1) return args[0];
  const [a, b] = args;
  if (args.length > 2) return mul(mul(a, b), ...args.slice(2));
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const a = (await read()).split(' ').map(Number);
      const b = [];
      let tmp = a[3];
      while (tmp--) {
        b.push((await read()).split(' ').map(Number));
      }
      res[i] = solve(a, b);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(a, b) {
  let [n, m, k, q] = a;
  let visr = new Set(),
      visc = new Set(),
      res = 0,
  vx = new Set(),
      vy = new Set(),
      sx = 0,
      sy = 0;
  for (let i = q - 1; i >= 0; i--) {
    let [x, y] = b[i];
    x--, y--;
    if (!visr.has(x) && sx < m || !visc.has(y) && sy < n) {
      res++;
    }
    if (sx === m && sy === n) break;
    if (!visr.has(x)) visr.add(x);
    if (!visc.has(y)) visc.add(y);
    if (!vx.has(y)) {
      vx.add(y);
      sx++;
    }
    if (!vy.has(x)) {
      vy.add(x);
      sy++;
    }
  }
  return power(k, res);
}
const MOD = 998244353;
function power(a, b) {
  let res = 1;
  while (b) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b = b >> 1;
  }
  return res;
}
function mul(...args) {
  if (args.length === 0) throw new Error('参数不能为空');
  if (args.length === 1) return args[0];
  const [a, b] = args;
  if (args.length > 2) return mul(mul(a, b), ...args.slice(2));
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const a = (await read()).split(' ').map(Number);
      const b = [];
      let tmp = a[3];
      while (tmp--) {
        b.push((await read()).split(' ').map(Number));
      }
      res[i] = solve(a, b);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function solve(a, b) {
  let [n, m, k, q] = a;
  b = b.map(arr => arr.map(num => num - 1));
  let visr = new Array(n),
      visc = new Array(n),
      res = 1,
      v1 = new Array(n),
      v2 = new Array(n),
      sum1 = 0,
      sum2 = 0;
  for (let i = q - 1; i >= 0; i--) {
    const [x, y] = b[i];
    if (!visr[x] && sum1 < n || !visc[y] && sum2 < m) {
      res = mul(res, k);
    }
    visr[x] = 1;
    visc[y] = 1;
    if (!v1[y]) {
      v1[y] = 1;
      sum1++;
    }
    if (!v2[x]) {
      v2[x] = 1;
      sum2++;
    }
  }
  return res;
}
const MOD = 998244353;
function mul(...args) {
  if (args.length === 0) throw new Error('参数不能为空');
  if (args.length === 1) return args[0];
  const [a, b] = args;
  if (args.length > 2) return mul(mul(a, b), ...args.slice(2));
  return ((a >> 16) * b % MOD * 65536 + (a & 65535) * b) % MOD;
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const a = (await read()).split(' ').map(Number);
      const b = [];
      let tmp = a[3];
      while (tmp--) {
        b.push((await read()).split(' ').map(Number));
      }
      res[i] = solve(a, b);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});