[Question]
  [
Output the infinite list of pairs of integers `(a, b)`, where both \$ a > 1 \$ and \$ b > 1 \$, ordered by the value of \$ a^b \$. When there are multiple pairs where \$ a^b \$ is equal, they should be ordered lexicographically.
For example, \$ 2^4 = 4^2 = 16 \$, but `(2, 4)` should come before `(4, 2)`, because it is lexicographically earlier.
This sequence starts:
```
2, 2
2, 3
3, 2
2, 4
4, 2
5, 2
3, 3
2, 5
6, 2
7, 2
```
Here are the first 100,000 pairs: <https://gist.github.com/pxeger/0974c59c38ce78a632701535181ccab4>
## Rules
* As with standard [sequence](/questions/tagged/sequence "show questions tagged 'sequence'") challenges, you may choose to either:
	+ Take an input \$ n \$ and output the \$ n \$th pair in the sequence
	+ Take an input \$ n \$ and output the first \$ n \$ pairs
	+ Output the sequence indefinitely, e.g. using a generator
* You may use \$ 0 \$- or \$ 1 \$-indexing
* You may use any [standard I/O method](https://codegolf.meta.stackexchange.com/q/2447)
* [Standard loopholes](https://codegolf.meta.stackexchange.com/q/1061) are forbidden
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins
  
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog) (v2), 12 bytes
```
⟦+₂ᵐgj∋ᵐ.^?∧
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/qG3Do6bGh9sWHNr5qHHD/0fzl2k/amp6uHVCetajjm4grRdn/6hj@f///6MA "Brachylog – Try It Online")
Function submission, a generator that generates all solutions. The TIO link uses a header + a command-line argument to print the first ten.
## Explanation
```
⟦+₂ᵐgj∋ᵐ.^?∧
⟦              Form a range from 0 to ? inclusive, for some number ?
 +₂            Add 2 to
   ᵐ             each element of the range
     j         Make two copies of the range
    g            as two separate lists (don't concatenate them)
      ∋        Generate all ways to select one element
       ᵐ         from each list
         ^       such that the first element to the power of the second
          ?      is ?
        .  ∧   and output the selected elements
```
Not *explicitly* specified is the ordering, but `⟦` will try possible values of `?` in order (which in this case are positive integers), and `ᵐ` will process the first element before the second (so it'll try 2⁴ before 4² because it's checking all possible exponents with base 2, then all possible exponents with base 3, etc.)
This algorithm is, of course, very inefficient. A reversed exponentiation, `~^`, seems like it could be an efficient way to solve the problem, and potentially shorter too – but adding the requirement that the base and exponent are ≥2 is nontrivial when writing the program in that form, and more importantly, Brachylog seems to enter an infinite loop when trying to calculate what value the base and exponent have in that situation. So the Jelly-ish algorithm written here is probably the best available option in the current version of Brachylog.
[Answer]
# [JavaScript (V8)](https://v8.dev/), 58 bytes
```
for(i=3;k=++i;)for(j=1;k=k<i?k:j++<i;)j**++k-i||print(j,k)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPT1tg621ZbO9NaE8TNsjUEcrNtMu2zrbK0tW2AwllaWtra2bqZNTUFRZl5JRpZOtma//8DAA "JavaScript (V8) – Try It Online")
Outputs indefinitely.
This code is shorter than it would be were I to remove the third for-loop and instead use logarithms.
Thanks to @Arnauld for -2 bytes and to @tsh for -2 more.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 9 bytes
```
ɾ›:Ẋµƒe;Ẏ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLJvuKAujrhuorCtcaSZTvhuo4iLCIiLCIxMSJd)
```
ɾ›        # 2..n+1
  :Ẋ      # Cartesian product with self
    µ  ;  # Sort by...
     ƒ    # Reduce by...
      e   # Exponentiation.
        Ẏ # Get the first n.
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~70 60~~ 52 bytes
```
2.step{|x|x.times{|y|x.times{|z|x==y**z&&p([y,z])}}}
```
[Try it online!](https://tio.run/##KypNqvz/30ivuCS1oLqmoqZCryQzN7W4uqYSwayqqbC1rdTSqlJTK9CIrtSpitWsra39/x8A "Ruby – Try It Online")
### How actually?
For any given x (starting from 2), check if there is a pair of values y,z in the range (0,x-1) which satisfies y^z==x
y and z will be greater than 2 because:
* If z==0, the result will be 1
* If y==0, the result will be 0 (or 1)
* If y==1, the result will be 1
* If z==1, the result will be y, which is always less than x
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~75~~ 67 bytes
*Thanks [@pxeger](https://codegolf.stackexchange.com/users/97857/pxeger) for -8 bytes*
```
i,=r=2,
while[j**k-i or print(j,k)for j in r for k in r]:i+=1;r+=i,
```
[Try it online!](https://tio.run/##K6gsycjPM7YoKPr/P1PHtsjWSIerPCMzJzU6S0srWzdTIb9IoaAoM69EI0snWzMNyMtSyMxTKFIAMbPBzFirTG1bQ@sibdtMnf//AQ "Python 3.8 (pre-release) – Try It Online")
Prints indefinitely.
Port of [@ophact](https://codegolf.stackexchange.com/users/106710/ophact)'s [Javascript answer](https://codegolf.stackexchange.com/a/243993/96039). Can probably still be golfed considerably, cuz I'm a noob at golfing in Python :P
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
‘Ḋ*þ`ZŒỤ‘ḣ
```
[Try it online!](https://tio.run/##y0rNyan8//9Rw4yHO7q0Du9LiDo66eHuJWD@4v///xsaAAA "Jelly – Try It Online")
This works either as a full program or as a function. Takes an argument *n* and returns the first *n* pairs.
## Explanation
```
‘Ḋ*þ`ZŒỤ‘ḣ
 Ḋ            Form an inclusive range from 2 to
‘               {n} + 1
   þ          Form an                table
  *                   exponentiation
    `           using {that range} and itself
     Z        Transpose (so that (x+1)^(y+1) is at index [x,y])
      ŒỤ      Produce a list of all coordinates (index pairs) in
                {the table}, sorted by the values of the table
        ‘     Increment {both halves of each coordinate pair}
         ḣ    {Output} the first {n} {coordinate pairs}
```
The basic idea is that given an input *n*, we calculate all possible exponentiations of numbers in the range 2 to *n*+1 inclusive, then return the inputs that produced the *n* lowest results. Jelly's tiebreak for `ŒỤ` happens to match the tiebreaking behaviour required by the question, but we need to transpose the table (and add 1 to the coordinates – Jelly uses 1-based coordinates but the exponentiation table conceptually uses 2-based coordinates) to make the coordinate system also match the format required by the question.
The range 2 to *n*+1 is sufficient because any *x**y* which is out of this range would have to be larger than 22 … 2*n*+1 or than 22 … (*n*+1)2, i.e. larger than *n* different elements, so it can't appear in the portion of the sequence we're outputting.
[Answer]
# [J](http://jsoftware.com/), ~~26~~ 23 bytes
Returns the first n pairs.
```
{.&(/:^/&>)&,[:{@;~2+i.
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/q/XUNPSt4vTV7DTVdKKtqh2s64y0M/X@a3JxpSZn5CukKRga/AcA "J – Try It Online")
`2+i.` Integers from 2 to n+1.  
`{@;~` Table of all pairs of these integers.  
`(/:^/&>)&,` Flatten into a list of pairs and sort by the value of the exponentiation.  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
L>ãΣ`m}I£
```
Outputs the first \$n\$ pairs.
Port of [*@emanresuA*'s Vyxal answer](https://codegolf.stackexchange.com/a/244004/52210).
[Try it online.](https://tio.run/##yy9OTMpM/f/fx@7w4nOLE3JrPQ8t/v/f0BQA)
Could alternatively be the 1-based \$n^{th}\$ value by replacing `£` with `è`: [try it online](https://tio.run/##yy9OTMpM/f/fx@7w4nOLE3Jr/Q6v@P/fEAA).  
The infinite sequence would be 2 bytes longer: `∞εL>ãΣ`m}Nè` - [try it online](https://tio.run/##yy9OTMpM/f//Uce8c1t97A4vPrc4IbfW7/CK//8B).
**Explanation:**
```
L       # Push a list in the range [1, (implicit) input]
 >      # Increase each by 1 to make the range [2,input+1]
  ã     # Pop and push the cartesian product of itself to get all pairs
   Σ    # Sort this list of pairs by:
    `   #  Pop and push both values separated to the stack
     m  #  Exponentiation
   }I£  # After the sort-by: leave the first input amount of pairs
        # (which is output implicitly as result)
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~75~~ 76 bytes
Outputs the first `n` pairs. (A previous version of this answer was one byte shorter by returning just the `n`th pair, but it raised an error for n <= 2.)
Not as short as [@Aidan Chow's Python answer](https://codegolf.stackexchange.com/a/243996/91685), but I wanted to see how well `sorted` would do. Using Python 2's lambda argument destructuring is a bit shorter than `lambda p:p[0]**p[1]` in Python 3.
```
lambda n:sorted([(i/n+2,i%n+2)for i in range(n*n)],key=lambda(i,j):i**j)[:n]
```
[Try it online!](https://tio.run/##JctBDoMgEAXQq7BpMoMkpixJPIm6wAjtUP0YZMPpaZNu3u5drb4zbI/T0g9/brtXcHcuNew0k4wYrJHHT465KFECVTxegaDBq/mENv0biUnsROvEs8ParyKoFOnJ3L8 "Python 2 – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 45 bytes
```
f=[(y,z)|x<-[2..],y<-[2..x],z<-[2..x],y^z==x]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P802WqNSp0qzpsJGN9pITy9WpxLCqIjVqYKzKuOqbG0rYv/nJmbmKdgqFBRl5pUoqCiUJGanKhgZKKT9N9JRMOICEsZcxlCWCZcJiGUKIoxBEkAxUy4zENccSAAA "Haskell – Try It Online")
*f* is the infinite sequence.
Inspired by @G B [answer](https://codegolf.stackexchange.com/a/244007/84844)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
p`‘*/Þḣ
```
[Try it online!](https://tio.run/##y0rNyan8/78g4VHDDC39w/Me7lj8//9/QwMA "Jelly – Try It Online")
Port of my Vyxal. -1 thanks to ovs.
```
p`       Cartesian product with (implicit) range from 1 to self
  ‘      Increment anything
     Þ   Sort by...
    /    Reduce by...
   *     Exponentiation
      ḣ  Get first n.
```
[Answer]
# [R](https://www.r-project.org), ~~57~~ ~~55~~ ~~50~~ 46 bytes
```
\(n)cbind(a<-n:1+1,b<-a%x%!!a)[order(a^b)[n],]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72waMGSNAUb3aWlJWm6Fjf1YjTyNJOTMvNSNBJtdPOsDLUNdZJsdBNVK1QVFRM1o_OLUlKLNBLjkjSj82J1YiG61hUnFhTkVGoYWhka6KRpQgQXLIDQAA)
Returns the \$n^\text{th}\$ value.
-2 bytes thanks to pajonk, then -4 more bytes thanks to pajonk. -1 byte thanks to Dominic van Essen.
[Answer]
# APL+WIN, 27 bytes
Prompts for desired term i.
```
(v[⍋*/¨v←,n∘.,n←1+⍳i])[i←⎕]
```
[Try it online! thanks to Dyalog APL Classic](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qvz6O2CcZcQJanP5BlyPWooz3tv0ZZ9KPebi39QyvKgII6eY86ZugBSaC89qPezZmxmtGZQA5QU@x/oHqu/2lchlxpXEZAbAzEJkBsCsSGBmDCgAsA "APL (Dyalog Classic) – Try It Online")
[Answer]
# Java 8, 91 bytes
```
n->{for(int t=4,i;;t++)for(i=1;i++<t*t;)if(Math.pow(i/t,i%t)==t&&--n<0)return i/t+","+i%t;}
```
Outputs the 0-based \$n^{th}\$ value.
[Try it online.](https://tio.run/##NY/BbsJADETvfIUVqWi3mwSoyqWb7R/AhSPisISkmAYn2jhUFcq3BxPgYskztubNyV98cjr8Dnnl2xZWHuk6AUDiIpQ@L2B9XwE2HJB@IFfiAGkrYj@R0bJnzGENBG6g5Pta1mG8YfcZo7VsjB4lt7BoTMbvbDWWauX5mDb1n8IZx/jG2jmeTpOEsrkOBXeBQBwTxZER1/aDvac13b6StGfopcYDnIVYPei2O68ftC8IdHMLmH0sZQrI6EmX/5aLc1p3nDbyxxUpNNEXRIZSKaif7frhBg)
Outputting the infinite sequence is 6 bytes longer:
```
n->{for(int t=4,i;;t++)for(i=1;i++<t*t;)if(Math.pow(i/t,i%t)==t)System.out.println(i/t+","+i%t);}
```
[Try it online](https://tio.run/##LY7BDoIwDIbvPkVjYrI5wJh4m/MN4ELixXiYE7Q4BoGCMYZnx029tOnf/v2/So86rq6P2Vjd95BqdO8FADoqulKbArIwAowNXsGwY2iOS69NC1960oQGMnCgZhcf3mXTMW8GUrsIpSQh@FdSW4lC7GlNkmPJUk33pG2eDDcU4Yq4UsTzV09FnTQDJW3nn1gX1mIZLUU4kdMsQ2Y7XKzP/Ed/wWqPzXLyptvprPkP2SWGucHaP@00fwA)
**Explanation:**
```
n->{                   // Method with integer parameter and String return-type
  for(int t=4,         //  Target-integer, starting at 4
          i;           //  Output integer, uninitialized
      ;                //  Loop indefinitely:
       t++)            //    Increase `t` by 1 after every iteration
    for(i=1;i++<t*t;)  //   Inner loop `i` in the range (1,t*t):
      if(Math.pow(i/t, //    If `i` integer-divided by `t`
                  i%t) //    to the power `i` modulo-`t`
          ==t&&        //    Is equal to `t`:
         --n           //     First decrease the input by 1
            <0)        //     And if it's now negative:
        return i/t+","+i%t;}
                       //      Return the divmod of `i,t` as result
```
[Answer]
# [Julia 1.0](http://julialang.org/), 49 bytes
```
!n=sort([a^b=>a=>b for a=2:n+1,b=2:n+1][:])[n][2]
```
[Try it online!](https://tio.run/##JcVBCoAgEADAr@htpRD1KKwfkQ30UBiyhhn0ezs0lzmfWpJ955SMd@sDYtoyhoQhi711kdB5Xuya/yl6UpEpOppXLzwqa9AScjnAKm@NUWp@ "Julia 1.0 – Try It Online")
output is the n-th pair in the sequence (1-indexed). For n>14, a `BigInt` is expected to avoid overflow
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 98 bytes
```
.+
*
L$`_
$=$.($`__
%L$`_(?=_*(.+))
:$1,$.($`__
%~`:(.+),(.+)
^¶$$.($2*$($1$*
N`.+
"$+"+0`.+:
A`:
```
[Try it online!](https://tio.run/##K0otycxLNPz/X0@bS4vLRyUhnkvFVkVPA8iI51IF8TXsbeO1NPS0NTW5rFQMdeBydQlWIFEdEMEVd2ibCkjGSEtFQ8VQRYvLLwFooJKKtpK2AZBlxcXlmGD1/7@hAQA "Retina – Try It Online") Outputs the first `n` terms. Explanation: Port of my Charcoal answer inspired by @ais523's answer.
```
.+
*
```
Convert `n` to unary.
```
L$`_
$=$.($`__
```
Create the numbers from `2` to `n+1` in decimal, each prefixed with `n` in unary.
```
%L$`_(?=_*(.+))
:$1,$.($`__
```
Create the Cartesian product of the range `2` to `n+1` with itself.
```
%~`:(.+),(.+)
^¶$$.($2*$($1$*
```
Precede each product with its exponentiation, computed by constructing a repeated multiplication and evaluating it.
```
N`.+
```
Sort the exponentiations, resolving ties stably i.e. keeping the lowest base first.
```
"$+"+
```
Loop `n` times...
```
0`.+:
```
... delete one exponentiation.
```
A`:
```
Keep only the lines where the exponentiation was removed i.e. the first `n` lines.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 41 bytes
```
ＮθＦθＦθ⊞υ⟦Ｘ⁺²ι⁺²κ⁺²ι⁺²κ⟧≔⟦⟧ηＦθ⊞η⌊⁻υηＩＥη✂ι¹
```
[Try it online!](https://tio.run/##VY3NCsIwEITvPsUeNxDBevUknjxUAh5LD7FUs5ifmnT18WMiFOppZthvZgej4xC0zfnsJ54v7G5jxJc4bO4hQjGwqOJkkCV0KnwKoiwn3EsgIWHxT7EK/4e@LB5ToofHrpdgVg9@w0ZCS54cOyxaWlwhUTAVyc940mnGVk8VvFoaRiQJjahEzs0ub9/2Cw "Charcoal – Try It Online") Link is to verbose version of code. Outputs the first `n` terms. Explanation: Inspired by @ais523's answer.
```
Ｎθ
```
Input `n`.
```
ＦθＦθ⊞υ⟦Ｘ⁺²ι⁺²κ⁺²ι⁺²κ⟧
```
Generate the Cartesian product of `[2..n+2)` with itself, plus include the result of the exponentiation as the first term.
```
≔⟦⟧ηＦθ⊞η⌊⁻υη
```
Get the `n` lowest terms from that list, which takes values in ascending order of exponentiation resolving ties by taking the base in ascending order.
```
ＩＥη✂ι¹
```
Output the terms but exclude the exponentiations.
35 bytes using the newer version of Charcoal on ATO:
```
ＮθＦθ⊞υ⌊⁻ΣＥθＥθ⟦Ｘ⁺²κ⁺²μ⁺²κ⁺²μ⟧υＩＥυ✂ι¹
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iOSOxKDk_MWfBgqWlJWm6FjcXe-YVlJb4leYmpRZpFGpac6XlFykAGQoBpcUZGqU6Cr6ZeZm5pbkaQLq0WCMYxEos0CgESkCo6ID8cqDWgBygrJGOQramjgKMnauJxEGViNUEyZUCSWuugKLMvBIN58TiErDRQDuDczKTUzUydRQMNUEqlhQnJRdDHbw5Wkm3LEdJR0FJNxNEGhooxUKlAA "Charcoal – Attempt This Online") Link is to verbose version of code.
]