[Question]
  [
*This challenge is related to some of the MATL language's features, as part of the [May 2018 Language of the Month](https://codegolf.meta.stackexchange.com/questions/16337/language-of-the-month-for-may-2018-matl) event.* *Associated challenge:* [Function clipboard: copy](https://codegolf.stackexchange.com/questions/164712/function-clipboard-copy).
---
# Introduction
MATL's *function clipboard* stores ("copies") the inputs to the four most recent calls to normal, input-taking functions. *Normal* functions are the most common type of functions in MATL. *Input-taking* means that the function takes at least one input. The stored clipboard contents can be pushed onto the stack ("pasted").
This challenge will take the clipboard contents as input. It will be assumed that all functions that have produced that clipboard state took one or more positive integers as inputs. So the clipboard state can be represented by a list of lists of numbers. (For more information on how the clipboard is actually filled see the related challenge; but that's not necessary for the current one).
### Interpreting the clipboard contents
**Example 1**
The *first* inner list refers to the *most recent* function call, and so on, Thus the clipboard state
```
[[11, 28], [12, 16], [4], [5, 6]]
```
indicates that the last function call took two inputs, namely `11`, `28`; the second-last call took inputs `12`, `16`; etc. (This clipboard state is produced by the code in the first example of the related challenge).
**Example 2**
If *there have not been enough function calls*, some trailing inner lists in the clipboard will be empty:
```
[[7, 5], [], [], []]
```
(This is produced by a program that simply adds `7` and `5`).
**Example 3**
Function calls can have *any number of inputs*, but always at least `1` (functions taking no inputs do not change the clipboard state). So the following is also possible.
```
[[3], [2, 40, 34], [7, 8, 15], []]
```
### Accessing the clipboard contents
The contents of the function clipboard are pushed onto the stack using MATL's function `M` (which, by the way, is not a normal function, but a clipboard function). This function takes a positive integer as input, and pushes some of the clipboard contents onto the stack, as follows. With reference to the clipboard state in example 1:
```
[[11, 28], [12, 16], [4], [5, 6]]
```
* `1M` returns *all inputs* to the most recent function call. So, for the considered example, it gives `11`, `28`.
* Similarly, `2M`, `3M` and `4M` return all inputs to the second, third and fourth most recent function calls. So `2M` gives `12`, `16`; `3M` gives `4`; and `4M` gives `5`, `6`.
* Numbers beyond `4` select *individual inputs* to function calls that took *more than one* input. So `5M` returns the *last* input to the *most recent* such call. In our case this gives `28`. `6M` returns the *preceding* individual input, which is `11`. `7M` returns the last input of the second-last call, that is, `16`, and `8M` gives `12`. Now, `9M` gives `6`. Note how input `4` is skipped because it was the *only* input in its function call. Lastly, `10M` gives `5`.
For the clipboard state in example 3:
```
[[3], [2, 40, 34], [7, 8, 15], []]
```
* `1M` gives `3`. `2M` gives `2`, `40`, `34`. `3M` gives `7`, `8`, `15`.
* `4M` has *undefined behaviour* (for the purposes of this challenge), because there have only been three function calls.
* `5M` gives `34`. `6M` gives `40`. `7M` gives `2`. `8M` gives `15`. `9M` gives `8`, `10M` gives `7`.
* `11M`, `12M`, ... also have *undefined behaviour*.
## The challenge
**Input**:
* the clipboard state, as a list of lists, or any other reasonable format;
* a positive integer *n*.
**Output**: the result of calling function `M` with *n* as input. The output will be one or several numbers with an unambiguous separator, or in any reasonable format such as a list or array.
Clarifications:
* The clipboard state consists of four lists of numbers. Some of the trailing lists may be empty, as in examples 2 and 3. If preferred, you can input the clipboard without those trailing empty lists. So example 3 would become `[[3], [2, 40, 34], [7, 8, 15]]`.
* All numbers in the clipboard will be positive integers, possibly with more than one digit.
* The number *n* is guaranteed to be valid. So for example 3 above, `n` cannot be `4` or `11`.
Additional rules:
* Input and output can be taken by [any reasonable means](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods).
* Programs or functions are allowed, in any [programming language](https://codegolf.meta.stackexchange.com/questions/2028/what-are-programming-languages/2073#2073). [Standard loopholes are forbidden](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default).
* Shortest code in bytes wins.
## Test cases
```
Clipboard state
Number
Output(s)
[[11, 28], [12, 16], [4], []]
2
12, 16
[[11, 28], [12, 16], [4], []]
5
28
[[7, 144], [12], [4, 8], [3, 4, 6]]
1
7, 144
[[7, 144], [12], [4, 8], [3, 4, 6]]
10
4
[[30], [40], [50, 60], [70, 80, 90]]
2
40
[[30], [40], [50, 60], [80, 90]]
7
90
[[15], [30], [2, 3, 5], [4, 5, 10]]
3
2, 3, 5
[[15], [30], [2, 3, 5], [4, 5, 10]]
7
2
```
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~56 51~~ 47 bytes
~~-5~~-9 bytes thanks to [Laikoni](https://codegolf.stackexchange.com/users/56433/laikoni?tab=profile) (pattern match to ensure length > 1 and using `do`-notation over list comprehension)!
```
c!n=([]:c++do l@(_:_:_)<-c;reverse$pure<$>l)!!n
```
[Try it online!](https://tio.run/##lZHdasJAEIXvfYqjCElwpEaNf9VSKBT6DMtSQhxr6HaVzdo@vp3dVOmdSNhhZuc7OTPJvmw@2ZjzueraTar0qhoMtgeY5/R9JU@2HlaPjr/ZNdw/nhyv@08m63bt@ausLTbYHjpAvUNpDDw3PoYGfs9WGoBjf3IWaSYVm4bj5dHV1qP3WtaGt62i15GOqhKyicZ6CFNbbiBu@GD/crCerW8CY9ijEmPH5RZ97Grj2SF92CRIMlQJViso9Wa91tELsBfaJuEFrXkq@2ah/Nmz43b0tCJYgstEUKEbhKLsdNqVNmFApErlOWG80ASVjwn5LGTTELSmMV0uw8J0Cy9ISeuKzqU/nbZoxAhROCFIOhNBThfoHtGI1D/BZBSxGIuRIDGbS7aQsxy1a0j/huJKz0kt/9F5Ee0jIxvLHMXfXIWMGgQTujbuUInNOPL6LLgOFamJnKl8xxnNJZN/PvsF "Haskell – Try It Online")
### Pointfree, ~~58~~ 55 bytes
-3 bytes thanks to [Laikoni](https://codegolf.stackexchange.com/users/56433/laikoni?tab=profile) (moving `([]:)` and replacing `id`)!
Alternatively we could use this pointfree version
[`(!!).(([]:)<>map pure.(>>=reverse).filter((1<).length))`](https://tio.run/##lZHBbtswDIbvfoo/RQFLgOrFSZy0gePLhgE77AlUHwyHSYwpsiGr29vPo@Ql6K3oQQQpfr9/Ur404y8yZuquQ@88vjW@yX72tu@OEKKspEwSsZA44HUSi4XMhND1XpbVtRkwvDnKRFUdHP0mN5LMTp3x5ITIS5kZsmd/kXK6Np3lDxz7BOhOaIyBp9HHMMJfyHIDcOTfnIWQXJEZKV4OrrMeD9@bztBxVjwk3NFtqmxao3yC6SyNKB8rnMl/7a0n60dGDHm07OuoOeIR82gQXw4pUok2xX4PrX9YX9fRCrA32qbBY/YW7cLKUP65kKN5ctEqWAUX3qXFIghZmSTzRocwH4TWea6weq4VdL5SyLch24RQ12qlbpdhX/URXijNrTu64/5mM6MRU4jCtQKnWxbk6gZ9RrRU@p1gvYxYjMWSkZjtOHvm87Kc1@D@B4o7vVP65R2dF9E@Mrwxz1H8n6vgUYNgre6NT6jYZhX5emK8DpXSaz4bfset2nHG/3z7tz2Z5jxOT@0w/AM "Haskell – Try It Online").
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
ḊƇUẎ⁸;⁹ị
```
[Try it online!](https://tio.run/##y0rNyan8///hjq5j7aEPd/U9atxh/ahx58Pd3f8PLz866eHOGf//R0cbG8TqKESbgElTAx0FMzDLHMiyAGJLg9jY/0YA "Jelly – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 17 [bytes](https://github.com/abrudz/SBCS)
```
{⍺⊃⍵⍪∊⊖¨⍵⌿⍨1<≢¨⍵}
```
[Try it online!](https://tio.run/##jZBPS8MwGMbv/RQBD10h25ou3eZQRHZxAxXcbmOHrE212H80GSjiSRAddniZNw/qZYhXL14E/Sb5IjPp2AaCfw7N@6bP70mevCQJiu4pCeLD2exMZG9idCGyV5E9i6uRGN19TNXu5l1kU7Qhrh/z/flMZPdgl/gR8IaRw/04AgU2HIQ@Y7I3NE3p2@7nQ6gzwGmYBIRT4MUp6Lb2QSGMXd/zqQt4DIhLEm40cscR5wlrlMvOEeElxolzTE9kHx3SkhOHZZ6SiDmpn/CybWHT3go3Mcbr2MT1tUWjaZ64vBXZ2FIPGU86B025dndanXmoLmWcpsvYmsblD@k4k1RufBGjp3mvH1A2DHgD6LBdaMvRGJ4skhlP2p39PUMNZsm2IpeegAIqDgijrqFM@TAXejPwk0FMUneuvJ6v4gBHWpimW7rKovd6CEGr3oc9ZEFUlRXLr9/XNd3@k0ALogYRxjmhZKjgCsSwOqfMf2HLQBVTqWqxTVhVtWbCugnXzZyr/cytoMoyu60uUaIFK9DO77Uh@nbUL9TsCw "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 57 bytes
```
a=>n=>a.map(e=>e[1]&&a.push(...[...e].reverse()))&&a[n-1]
```
[Try it online!](https://tio.run/##FY3LCoMwFET3@YqsSgLXYHxUi8RdvyJkcbHX2mITSVo3pd9udTEwh@EwT1wxDfGxvDMfbrSNZkPTe9OjeuEiyPRktTudUC2fNAmllN1DTkVaKSYSUsp9tT7TbjetLR1wWwCvcuBldUADvAWu66M71zFmNRRQAq/hDA20cAGdOzWGeMVhOi6/jPMh@BRmUnO4i1GgFCRlx36y2/4 "JavaScript (Node.js) – Try It Online")
This is an anonymous, curried function. Run it with `( function code )(clipboard)(n)`
## Explanation
```
a=>n=>{
    // The strategy is to append the individual clipboard inputs to the end of a,
    // after the function calls (lists). We then return a[n-1] to offset JavaScript's
    // zero indexing.
    a.map(e=>{
        e[1]&& // if this list has more than one element...
            a.push(...[...e].reverse()) // add each element to a, in reverse order.
            // reverse() modifies the original array, so we have to use [...e] to "clone" e
    })
    return a[n-1]
}
```
[Answer]
# JavaScript (ES6), 72 bytes
Takes input in currying syntax `(clipboard)(n)`.
```
a=>m=>a[m-1]||(g=r=>(r=r|a[k][1]&&a[k].pop())?--m<5?r:g(1):g(!++k))(k=0)
```
[Try it online!](https://tio.run/##nZBBbsMgEEX3PQXdRIyCE7BN7FTFOQhigdLEah0Hi1Rd@e7ugImqyps2SIy@xLynGT7sl70d/fvwmV3d22k6q8mqpleN1X0mzDjSVnnVUK/8aHVntDCrVQibwQ0U4JBl/as8@JeWCsDyvF53ALRTHKaju97c5bS5uJaeqdZCMJLXhhEtckbELqQyFGOA5gAkne2WzA1P/zLIX4a8XtAVImU505FkJLoKRjDugkMEB9Jz60MGjgo0LOGCRyRWybE9pgpTjXfP778QYP5H@oes0vZI75e0kHHMyODP4bwyzS9xpSgo0urp@RFFdVdM3w "JavaScript (Node.js) – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), ~~60~~ 56 bytes
thanks to [Jonathan Allan](https://codegolf.stackexchange.com/users/53748/jonathan-allan) for -4 bytes.
```
lambda l,n:(l+sum([b[::-1]for b in l if b[1:]],[]))[n-1]
```
[Try it online!](https://tio.run/##lZDLCsIwEEX3/Yq7bHGEpk8N9EtiFi1aLMQoti78@joZH@BC1EWGO5kzz9N12h99NvfNZnbtodu2cOR17Bbj5RCbzmi9VLY/ntFh8HAYenRGaWvJ2CQxnqPztBunEQ1MBMQZwRilCNnKslTsqyqoIhhrEwpU@ROlAlVzqCjulBAEyckJLKsXm/4By5B5KoTYkrMrUTWrFb91@oTrz/A7mMtSpfSTMLfhv/IxCO@s3ot@ZyMbReH6nsLtPeTUmguczoOf0MeO4JP5Bg "Python 2 – Try It Online")
[Answer]
# Java 8, 110 bytes
A lambda (curried) taking the clipboard state as an `int[][]` and the number as an `int` and returning `int` or `int[]` (a single number may be returned through either type).
```
s->n->{if(--n<4)return s[n];else{int i=0,l;for(n-=4;(l=s[i].length)<=n|l<2;i++)n-=l>1?l:0;return s[i][l+~n];}}
```
[Try It Online](https://tio.run/##jZFNb9swDIbPzq8gChSQEVuIg3Qo6o@ilwE7DDv0aPig2nKmTKENSe4QZN5fz@jIbYJih15EiiIf6iV34lXEXS9x1/w69cOLVjXUWlgL34VCOC6COWidcGRahULDjqr44JTm7YC1Ux3yr7OTfUMnt9KUVVlFn0mM4MfLTtauKKAX1knITzYuMC6OqmVxjNkmNNINBsGWWKVSW3lU6EDlq0inbWcYxvkmZTq3paq4lrh1P8Msxz86W6dquQzpXRfJo35Ype8kVZV6@Zd443gK0sVHma@damBPE2DPzijclhUIs7XhNJDgSiE0wgnIAeVvuA5PacExSSJY34@Rv6wjSL7Ml81s7yLwoTGlg8QAm7QhIZOUTEZ2NTlLcnz3wI8LjLSDdpR4nhoXfa8PbPpOOPsYTszACwBi74VzsjkHVQtsBigkzVjLrr0omDsF70XU5rLKJ2PEwXLXeTRjV3Ue6juP0zHt6z80n3dhXFU8H0jPnneD4z29Of22BO4B7Oa2eYBbexMBRhdd4RlBhHExnv4B)
## Ungolfed
```
s ->
    n -> {
        if (--n < 4)
            return s[n];
        else {
            int i = 0, l;
            for (
                n -= 4;
                (l = s[i].length) <= n | l < 2;
                i++
            )
                n -= l > 1 ? l : 0;
            return s[i][l + ~n];
        }
    }
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 12 bytes
```
Díʒg<Ā}˜«s<è
```
[Try it online!](https://tio.run/##MzBNTDJM/f/f5fDaU5PSbY401J6ec2h1sc3hFf//R0cbmsbqKEQbG4BIIx0FYx0FsIAJkNZRMDSIjeUyBwA "05AB1E – Try It Online")
**Explanation**
```
D              # duplicate input list
 í             # reverse each
  ʒg<Ā}        # filter, keep only elements that are longer than 1
       ˜       # flatten
        «      # append to original list
         s<    # decrement the second input
           è   # get the element in the list at that index
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 12 bytes
```
!S+(m;ṁ↔f(¬ε
```
[Try it online!](https://tio.run/##yygtzv7/XzFYWyPX@uHOxkdtU9I0Dq05t/X////R0YaxOtFGQGwMxCY6pjpmOuZAVmzsfzMA "Husk – Try It Online")
### Explanation
Pretty much a direct port of the Haskell answer:
```
!S+(m;ṁ↔f(¬ε  -- example inputs: [[1],[2,3],[4],[5,6,7],[]] 7
 S+           -- concatenate itself with itself modified by
        f(    -- | filter
           ε  -- | | length 1
          ¬   -- | | not
              -- | : [[2,3],[5,6,7],[]]
      ṁ       -- | map and flatten
       ↔      -- | | reverse
              -- | : [3,2,7,6,5]
              -- | map
              -- | | pure
              -- | : [[3],[2],[7],[6],[5]]
              -- : [[1],[2,3],[4],[5,6,7],[],[3],[2],[7],[6],[5]]
!             -- index into it: [2]
```
[Answer]
# [R](https://www.r-project.org/), 58 bytes
```
function(M,n)c(M,unlist(lapply(M[lengths(M)>1],rev)))[[n]]
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/T8NXJ08zGUiW5uVkFpdo5CQWFORUavhG56TmpZdkFGv4atoZxuoUpZZpampGR@fFxv5P0wCrNNZJ1jDSMTHQMTbRBDLNdSx0DE1BLE1NHUMDzf8A "R – Try It Online")
Takes `M` as a `list` of vectors `c()`; so replacing `[[` with `list(`, `[` with `c(` and `]` with `)` should transform the test cases into R test cases.
For inputs `n<=4` with "undefined behavior", returns `NULL` and for other invalid inputs, throws a "subscript out of bounds" error.
```
function(M,n)
                                        [[n]]	# take the nth element of
c(M,                                   )	# M concatenated with:
    unlist(                           )		# the individual elements of
           lapply(               ,rev)		# in-place reversals of
                  M[lengths(M)>1]		# elements of M with length > 1
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), ~~12~~ ~~14~~ 13 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
àJ├∙ε╝F▀ÿi☻Ia
```
[Run and debug it](https://staxlang.xyz/#p=854ac3f9eebc46df9869024961&i=[[11,+28],+[12,+16],+[4],+[]],+2%0A[[11,+28],+[12,+16],+[4],+[]],+5%0A[[7,+144],+[12],+[4,+8],+[3,+4,+6]],+1%0A[[7,+144],+[12],+[4,+8],+[3,+4,+6]],+10%0A[[30],+[40],+[50,+60],+[70,+80,+90]],+2%0A[[30],+[40],+[50,+60],+[80,+90]],+7%0A[[15],+[30],+[2,+3,+5],+[4,+5,+10]],+3%0A[[15],+[30],+[2,+3,+5],+[4,+5,+10]],+7&m=2)
Explanation:
```
vsc{%vfr$r+@]|u Full program, unpacked, implicit input
vs              Decrement the number and get the list
  c{  f         Copy and filter:
    %v            Length not equal to 1?
       r$r      Reverse, flatten, and reverse again
          +     Concat orig array and and modified array
           @]|u Index, wrap into array, uneval
```
## Stax, 12 bytes
```
Å{b≈\☼╣Δ@░ ‼
```
Unpacked:
```
{vsc{%vfr$r+@}
```
This is a block, so I can get rid of the `]|u`, but I don't know if this is valid as it's packing a block.
[Answer]
# [J](http://jsoftware.com/), 33 22 bytes
-11 bytes (1/3 shorter) thanks to FrownyFrog's solution!
```
{0;],|.&.>;/@;@#~1<#&>
```
[Try it online!](https://tio.run/##jc5NCsIwEAXgfU/xsNAmEOKkTdra2NKLuBAxiBsXXfpz9TilXSiIuhgY8j7y5hzD2GkQWlC8kt@pm85079eDH9KH2aZZH2Wy0shDp3Mo3FuEMUmOh9MFBQKEMQpFI70whYKppIf12LezcD@FmUTN79ZyYAoPYRVYQ5QKvFZygfSvnM4qiVt4hCMOaEI1bw3PhhZXf3ZvpmRjnGcHjvh@rnJyrnZ8y@tf3118Ag "J – Try It Online")
## My initial solution:
# [J](http://jsoftware.com/), 33 bytes
```
<:@[{(,[:<"0@;[:|.&.>(1<#)&>#])@]
```
Not happy - I'm pretty sure it can be golfed much further.
## Explanation:
A dyadic function, taking the clipboard state as its rigth argument, the left argument is `n`
`<:@[` subtract 1 from the left argument
`{` selects the `i`th element (calculated above) from the list on its right
`(...)` the entire list
`#` copy
`]` from the list of clipboard state
`(1<#)` the sublists with length greater than 1
`|.&.>` rotate each copied sublist
`<"0@;` raze and box - puts each number into a separate box
`,` append the new list to the list of clipboard state
`@]` makes the entire verb in (...) monadic 
[Try it online!](https://tio.run/##jc5BCoMwEAXQvaf4KGgCISTRqE20eA9xUUpD6aYLl23Pno7oooXSdjEw5D/y5xLD3EsoOKjYuWG8MTG6LlWDH91d5nLPdJfxfJ9NfJgiT1KJIvSygMDDIcxJcjqerzAIYFoLmJZ7po2ArrlH5XFwq7A/hV5EQ@9VRYE2HqwSIA1WCtBa8w2qf@VyVqmohYZZRYFaUENbS7NTm2s@uzdTktHWkwNFdD9VWb5WW7rl9a/vLj4B "J – Try It Online")
[Answer]
# [V](https://github.com/DJMcMayhem/V) + [coreutils](https://www.gnu.org/software/coreutils/manual/coreutils.html), ~~53 45 43 42~~ 40 bytes
-9 bytes thanks to [DJMcMayhem](https://codegolf.stackexchange.com/users/31716/djmcmayhem?tab=profile) (using `VGÇ /d` over `:,$g/^[^ ]*$/d`, `D@"dd` over `"aDÀdd` and `!!` over `:.!`)! 
My very first attempt at [V](https://github.com/DJMcMayhem/V) (tips welcome!), the below code is using circled characters (eg. `ⓞ` for `\xf`) for readability:
```
jäGⓞVGÇ /d
ⓞò!!tr \  \\n|tac
jòHD@"ddjdG
```
[Try it online!](https://tio.run/##K/v/P@vwEnf@MPfD7Qr6KVz8hzcpKpYUKcQoKMTE5NWUJCZzZR3e5OHioJSSkpXi/v@/OZehKZexAZeRgrGCKZeJgqmCoQEXAA "V – Try It Online")
### Hexdump
```
00000000: 6ae4 470f 5647 c720 2f64 0a0f f221 2174  j.G.VG. /d...!!t
00000010: 7220 5c20 205c 5c6e 7c74 6163 0a6a f248  r \  \\n|tac.j.H
00000020: 4440 2264 646a 6447                      D@"ddjdG
```
## Explanation
The first line contains *n* and the lines below contain the entries of the clipboard, each entry is separated by spaces if there were multiple inputs:
```
j                        " move to the beginning of the clipboard entries
 äG                      " duplicate the clipboard
   ⓞ                    " <C-o> move cursor to the beginning of the 2nd copy
     VG                  " select everything from cursor to the end of buffer and ..
       Ç /d              " .. delete every line that doesn't contain a space
ⓞ                       " <C-o> move cursor to the beginning of the 2nd copy (now without single arguments)
  ò                   ò  " do the following until the end of buffer
   !!                    "   on the current line execute the shell command
     tr \  \\n           "   replace spaces with newlines
              |tac⮠     "   and reverse the lines
                    j    "   move to next line
H                        " go to the beginning of buffer (where n is)
 D                       " delete n (stores it in register ")
  @"                     " that many times ..
    dd                   " .. remove the line
      j                  " move cursor to next line
       dG                " delete everything from here to the end of buffer
```
[Answer]
# [Red](http://www.red-lang.org), 91 bytes
```
func[b n][a: copy[]foreach c b[if 1 < length? c[append a reverse copy c]]pick append b a n]
```
[Try it online!](https://tio.run/##jc9BDoIwEAXQvaf4R2iBAhoT7@B2MgsorRBNbSqaeHpsiSZEF7JoZ/Ff86fBdNPRdMQbu5vs3Wlq4ZiaHfTVP4ntNZhG99BoabCQ2ONi3GnsD9DUeG9chwbBPEy4mfkNNLMf9BnvtI2548mHwY2wIJISWc0gmUGWcRbxMCPb/CVqQSrIophJypF0jgJlZHIlEwuXi5SnSwmUaVYCtcBWfK32Cz@qWn5ApaKUZsih5m4VGyPL17FqegE "Red – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 176 bytes
```
#define p printf("%d ",
int*_,i;f(x,n)int**x;{if(n<5){for(_=x[2*n-2];_-x[2*n-1];++_)p*_);}else{n-=4;for(i=0;i<8;i+=2)if(n&&x[i]+1-x[i+1])for(_=x[i+1];_-x[i]&&n;--_,--n);p*_);}}
```
[Try it online!](https://tio.run/##XZFBj5swEIXv/IpRVo1sMF1gSRTJobfeVr31hCwUERNGTUwEpkVC/HZqm2w2G5@en958M2OX4aks5/nlKCtUEq5wbVHpiqy@HWHFPKP9giGvyMAUtTd/4CNWRO03dKyalhTZkCe@ChPBi3CRseBBUNCrX1A@yXMnRxVmKbdpzCKO@x3HIEuoxazXQ44iiE0pBrGgH0h7cUAU67XiYViwMFSUL9BpfkFVnvujhH2nzcSn7/UPOyxcDqiIFYf2VDIo60MLvtF/c0Fh9MCc11f4OVxlqTvY2Fh/kcpokscMEgZvDFLBYENd2KJ8JTud7wS/OxW2xomj6MHrZNmo45Opa2yfvarpW10/mX6ZpwIyGB2a3WhsAbBbzfSZR@457UaLbKUr5J9mYs0F8@C6Jg76YG5dvWtxw5pvAILGjTgg7CHlEAT48YL2LC@r@ovtolvd/CHume3HMVjBivJ79l@NZwnEhB8J9pi1ze8HhnHQDboE/xL40uDX7/f3Z/Z0V8vWvh3AlFjwkppuKxEbYK6Pm3Qj6I3TSt23yuzqTd48xzEkuzlOIN7O6Txv/wM "C (gcc) – Try It Online")
Takes the array as a list of 4 start/end pointer pairs, then n. 
Description:
```
#define p printf("%d ",  // This gives us the short-hand for printing
int*_,                   // This defines _ as a pointer to int
i;                       // This defines i as an integer
f(x,n)int**x;{           // This defines f as a function taking int **x and int n
                         // NOTE: x is {start, end, start, end, start, end, start, end}
if (n<5) {               // This is for the 1-4 case
  for(_=x[2*n-2];        // Loop _ from the 'end pointer' 
  _-x[2*n-1];++_)        // Until the 'start pointer'
  p*_);                  // Using the short-hand, print *_
}else{                   // This is for the 5+ case
  n-=4;                  // Cut n to improve indexing
  for(i=0;i<8;i+=2)      // Loop over each 'start pointer index'
    for(_=x[i+1];        // Loop _ from the 'end pointer'
        _-x[i]&&n;       // Until the 'start pointer' or n becomes 0
        --_,--n);        // Decreasing n each time
  p*_);}}                // _ now points to the 'correct' index, so print it
```
]