/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int unsigned long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

int gcd(int a, int b) {
    if (a <= 0 || b <= 0) {
        exit(1);
    }
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = gcd(p, q);
            p /= d;
            q /= d;
            for (int j = 0; j < 20; j++) {
                int d = gcd(q, b);
                q /= d;
                if (d <= 1) {
                    break;
                }
            }
            if (q == 1) {
                out << "Finite" << "\n";
            } else {
                out << "Infinite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    TaskA solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int unsigned long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

int gcd(int a, int b) {
    if (a <= 0 || b <= 0) {
        exit(1);
    }
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = gcd(p, q);
            p /= d;
            q /= d;
            for (int j = 0; j < 100; j++) {
                int d = gcd(q, b);
                if (d <= 1) {
                    break;
                }
                q /= d;
            }
            if (q == 1) {
                out << "Finite" << "\n";
            } else {
                out << "Infinite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    TaskA solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = __gcd(p, q);
            p /= d;
            q /= d;
            for (int j = 0; j < 100; j++) {
                int d = __gcd(q, b);
                q /= d;
                if (d == 1) {
                    break;
                }
            }
            if (q == 1) {
                out << "Finite" << "\n";
            } else {
                out << "Infinite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    TaskA solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int unsigned long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

int gcd(int a, int b) {
    if (a <= 0 || b <= 0) {
        exit(1);
    }
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = gcd(p, q);
            p /= d;
            q /= d;
            for (int j = 0; j < 60; j++) {
                int d = gcd(q, b);
                if (d <= 1 || q == 0) {
                    break;
                }
                q /= d;
            }
            if (q <= 1) {
                out << "Finite" << "\n";
            } else {
                out << "Infinite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    TaskA solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


n = int(input())
a = []
b = []
for _ in range(n):
    i = []
    i = input().split()
    a.append(int(i[0]))
    b.append(int(i[1]))
f = False
for i in range(len(a)):
    for j in range(len(b)):
        if a[i] + b[i] == a[j] and b[j] == 0 - b[i]:
            f = True
            break
    if f:
        print('YES')
        break
else:
    print('NO')


x=[]
for _ in range(int(input())):
  k=list(map(int, input().split()))
  k[1]+=k[0]
  x.append(k)
y=[0]*100
cnt=0
for i in range(len(x)):
  if y[i]==0 and x[i][::-1] in x:
    cnt+=1
    y[x.index(x[i][::-1])]=1
print("YES" if cnt>0 else "NO")


n = int(input())
pos = []
can = False
for i in range(n):
    pos.append(list(map(int, input().split())))
for i in range(n-1):
    curr = pos[i][0] + pos[i][1]
    for j in range(i,n):
        curr1 = pos[j][0] + pos[j][1]
        if curr == pos[j][0] and curr1 == pos[i][0]:
            can = True
            break
if can :
    print("YES")
else:
    print("NO")


Camels = dict()
N = int(input())
for i in range(N):
    X = list(map(int, input().split()))
    if X[0] + X[1] in Camels.keys():
        if Camels[X[0] + X[1]] == X[0]:
            print("YES")
            exit()
    Camels[X[0]] = X[0] + X[1]
print("NO")

# UB_CodeForces
# Advice: Lova everyone in the world
# Location: Where I belong to
# Caption: Happy with family


n = int(input())
locations = []
spits = []
for i in range(n):
    temp = list(map(int, input().split()))
    # locations[i]=temp[0]
    # spits[i]=temp[1]
    locations.append(temp[0])
    spits.append(temp[1])

i = 0
while i < n:
    temp = locations[i] + spits[i]
    exists = temp in locations
    if exists:
        secondCamelIndex = locations.index(temp)
    if (exists and
            locations[secondCamelIndex] + spits[secondCamelIndex] == locations[i]):
        break
    i += 1

print("NO" if i == n else "YES")


n=int(input())
a={}
for i in range(n):
    x,d=map(int,input().split())
    a[x]=d
for x in a:
    if x+a[x] in a:
        if x+a[x]+a[x+a[x]]==x:
            print("YES")
            exit()
print("NO")


n = int(input())
d = list()
x = list()
count == False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
    j = x[i] + d[i]
        if i != j:
            if x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
                count == True

if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
a = {}
flag = False
for i in range(n):
    x, d = input().split()
    x, d = int(x), int(d)
    a[x] = d
for x in a:
    b = a.get(x + a[x])
    if b is not None and b == -a[x]:
        print("YES")
        flag = True
        break
if not flag:
    print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == i and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print("YES")
else:
    print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input90.split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == i and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print("YES")
else:
    print("NO")


a,b = map(int,input().split())
m = list(map(int,inpput().split()))
for i in range(n):
    for j in range(n):
        if a != b and a[i] + b[i] == a[j] and a[j] + b [j] == a[i]:
            print("YES")
        else:
            print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('Yes')
else:
    print('No')


n = int(input())
x = [None] * n
d = [None] * n
for i in range(n):
    x[i], d[i] = map(int, input().split())
f = False
for i in range(n):
    j = x[i] + d[i]
    if x.count(j) == 1 and d[x.index(j)] == -d[i]:
        f = True
if f == True:
    print("YES")
else:
    print("NO")


n = int(input())
x = list()
d = list()
count = False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)
for i in range(n):
    for j in range(n):
        if i != j:
            if x[i]+d[i] == x[j] and x[j]+d[j] == x[i]:
                count = True
if count == True:
    print("YES")
else:
    print("NO")


n = map(int,input().split())
x,d = list()
j = i + 1
j <= n
i <= n
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
        if i != j and x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
            print("YES")
        else:
            print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('YES')
else:
    print('NO')


x,d = map(int,input().split())
n = list(map(int,inpput().split()))
j = i + 1
j <= n
i <= n

for i in range(n):
    for j in range(n):
        if a != b and a[i] + b[i] == a[j] and a[j] + b [j] == a[i]:
            print("YES")
        else:
            print("NO")


import sys
import math

n = int(sys.stdin.readline())

x = [60002] * 60003
for i in range(n):
    xi, di = [int(v) for v in (sys.stdin.readline()).split()]
    x[xi + 30000] = xi + 30000 + di

for i in range(-10000, 10000):
    if(x[x[i + 30000]] == i + 30000):
        print("YES")
        exit()

print("NO")


import sys
import math

n = int(sys.stdin.readline())

x = [-50000] * 20001
for i in range(n):
    xi, di = [int(v) for v in (sys.stdin.readline()).split()]
    x[xi + 10000] = di

for i in range(n):
    #print(x[i + 10000], i)
    cc = i + 10000
    if(x[cc] >= 0 and x[cc] + cc == cc - x[x[cc] + cc]):
        print("YES")
        exit()

print("NO")


a = int(input())
d1,d = [],[]
c = 0
for i in range(a):
    m,n = map(int,input().split())
    d.append(m)
    d1.append(m + n)

for i in range(a):
    for j in range (a):
        if d[i] == d1[j] and d[j] == d1[i]:
            c+=1

if c:
    print("YES")
else:
    print("NO")


N, ans = int(input()), 'NO'
cont = {}
for i in range(N):
    pos, spit = [int(item) for item in input().split()]
    cont[pos]=spit

for key in cont:
    secondCamel = key + cont[key]
    if secondCamel in cont and secondCamel + cont[secondCamel] == key:
        ans = "YES"
        break

print(ans)


n=int(input())
l=0
a={}
for i in range(n):
    x,y=map(int,input().split())
    a[x]=x+y
for j in a:
    try:
        if j==a[a[j]]:
            print("YES")
            break
        else:
            l+=1
    except:
        l+=1
        continue
if l==len(a):
    print("NO")


n = int(input())
l = []
for i in range(n):
    x, d = map(int, input().split())
    l.append((x, d))

yes = False
for i in range(n - 1):
    for j in range(i + 1, n):
        if l[i][0] + l[i][1] == l[j][0] and l[j][0] + l[j][1] == l[i][0]:
            yes = True

print("YES" if yes else "NO")


n = int(input())
x,d = list()
count == False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
        if i != j and x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
            count == True

if count == True:
        print("YES")
    else:
        print("NO")


n = int(input())
x = list()
d = list()
count = False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)
for i in range(n):
    for j in range(n):
        if i != j:
            if x[i]+d[i] == x[j] and x[j]+d[j] == x[i]:
                count = True
                if count == True:
                    break
if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)]  == -t[i]:
        f = True
        break
if f == True:
    print("YES")
else:
    print("NO")


x,d = map(int,input().split())
n = list(map(int,input().split()))
j = i + 1
j <= n
i <= n

for i in range(n):
    for j in range(n):
        if a != b and a[i] + b[i] == a[j] and a[j] + b [j] == a[i]:
            print("YES")
        else:
            print("NO")


n = int(input())
a = {}
flag = False
for i in range(n):
    x, d = input().split()
    x, d = int(x), int(d)
    a[x] = d
for x in a:
    b = a.get(x + a[x])
    if b is not None and b == -a[x]:
        print("YES")
        flag = True
        break
if not flag:
    print("NO")


n= int(input())
l = [None]*n
t =[None]*n
for i in range(n):
    l[i],t[i] = map(int, input().split())
f = False
for j in range(n):
    j = l[i]+t[i]
    if l.count(j) == l and t[l.index(j)]== - t[i]:
        f = True
        break
if f == True:
    print("Yes")
else:
    print("No")


n = int(input())
x = list()
d = list()
count = False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)
for i in range(n):
    for j in range(n):
        if i != j:
            if x[i]+d[i] == x[j] and x[j]+d[j] == x[i]:
                count = True
if count == True:
    print("YES")
if count == False:
    print("NO")


n = map(int,input().split())
x,d = list()
j = i + 1
j <= n
i <= n
for i in range(n):
    for j in range(n):
        if i != j and x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
            print("YES")
        else:
            print("NO")


n = int(input())
d = list()
x = list()
count == False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
        if i != j:
            if x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
                count == True

if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
d = list()
x = list()
j = i + 1
j <= n
i <= n
count == False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
        if i != j:
            if x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
                count == True

if count == True:
        print("YES")
    else:
        print("NO")


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == l and t[l.index(j)] == - t[i]:
        f = True
        break
if f == True:
    print("Yes")
else:
    print("No")


n = int(input())
a = 0
m = []
for i in range(n):
    x, d = map(int, input().split())

    a = x + d
    m.append(a)
    if m.count(d) == True:
        print('YES')
    else:
        print('NO')


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('Yes')
else:
    print('No')


n = int(input())
l = [None] * n
t = [None] * n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('YES')
else:
    print('NO')


a,b = map(int,input().split())
m = list(map(int,inpput().split()))
j = i + 1
j <= n
i <= n
for i in range(n):
    for j in range(n):
        if a != b and a[i] + b[i] == a[j] and a[j] + b [j] == a[i]:
            print("YES")
        else:
            print("NO")


n = int(input())
x = list()
d = list()
count = False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)
for i in range(n):
    for j in range(n):
        if i != j:
            if x[i]+d[i] == x[j] and x[j]+d[j] == x[i]:
                count = True
if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
a = list(map(int, input().split()))
total = 0
for i in range (n):
    total += max(a[i], a[i + 1])
print(total)


di = {}
k = 0
n = int(input())

for _ in range(n):
    a,b = map(int,input().split())
    di[a]= b

sorted(di)

for i in di:
    if di[i]>=0:
        dis = di[i]+i
        if dis in di and di[dis]+dis==i:
            print("YES")
            k = 1
            break
if k == 0:
    print("NO")


n=int(input())

D=[]
X=[]
for i in range(n):
    x,d=map(int,input().split())
    X.append(x)
    D.append(d)
Ans="NO"
for i in range(n):

    for j in range(n):
        if(j==i):
            continue
        if(X[i]==X[j]+D[j] and X[j]==X[i]+D[i]):
            Ans="YES"
            break
print(Ans)


n=int(input())
l=[]
for i in range(n):
    v=list(map(int,input().split()))
    l.append((v[0],v[1]))
d=-1
for i in range(n):
    for j in range(i+1,n):
        e=l[i][0]-l[j][0]
        c=l[j][0]-l[i][0]
        if c==l[i][1]:
            if c+l[j][1]==0:
                d=0
                break
        if e==l[j][1]:
            if e+l[i][1]==0:
                d=0
                break
    if d==0:
        break

if d==0:
    print('YES')
else:
    print('NO')


n=int(input())
l=[]
for i in range(n):
    v=list(map(int,input().split()))
    l.append((v[0],v[1]))
d=-1
for i in range(n):
    for j in range(i+1,n):
        c=abs(l[i][0]-l[j][0])
        if c==l[i][1]:
            if c+l[j][1]==0:
                d=0
                break
    if d==0:
        break

if d==0:
    print('YES')
else:
    print('NO')


#29A
n=int(input())
X=[]
y=0
for i in range(n):
    x,d=list(map(int,input().split(' ')))
    X.append([x,x+d])
    if([x+d,x] in X):
        y=1
if y==1:    print('YES')
else:   print('NO')
#without set


#29A
n = int(input())
s = set()
#set fuctions add,update
for i in range(n):
	x, d = map(int,input().split() )
	if (x + d, -d) in s:
		print('YES')
		exit()
	s.add((x, d))
print('NO')


n = int(input())
a = [None] * n
b = [None] * n
for i in range(n):
  a[i], b[i] = map(int, input().split())
spit = False
for i in range(n):
  w = a[i] + b[i]
  if w in a:
    if b[a.index(w)] == -b[i]:
      spit = True
      break
if spit:
  print("YES")
else:
  print("NO")


def main():
    flag = False
    n=int(input())
    d_t=dict()
    for i in range(n):
        x,d=input().split(" ")
        x,d=int(x),int(d)
        d_t[x]=d
    for x in d_t:
        d2=d_t.get(x +d_t[x])
        if d2 is not None and d2 == -d_t[x]:
            print("YES")
            flag = True
            break
    if not flag:
        print("NO")

if __name__ == '__main__':
    main()


n = int(input())
x = list()
d = list()
count = False
for p in range(n):
    a, b = map(int, input().split())
    x.append(a)
    d.append(b)
for p in range(n):
    for q in range(n):
        if p != q:
            if x[p]+d[p] == x[q] and x[q]+d[q] == x[p]:
                count = True
if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
a = {}
flag = False
for i in range(n):
    x, d = input().split()
    x, d = int(x), int(d)
    a[x] = d
for x in a:
    b = a.get(x + a[x])
    if b is not None and b == -a[x]:
        print("YES")
        flag = True
        break
if not flag:
    print("NO")


n = int(input())
d = list()
x = list()
j = i + 1
j <= n
i <= n
count == False
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)

for i in range(n):
    for j in range(n):
        if i != j:
            if x[i] + d[i] == x[j] and x[j] + d [j] == x[i]:
                count == True

if count == True:
    print("YES")
else:
    print("NO")


n = int(input())
x = [None] * n
d = [None] * n
s = [None] * n
for i in range(n):
    x[i], d[i] = map(int, input().split())
    s[i] = sum(x[i], d[i])
ss = list(set(s))
if len(ss) < len(s):
    print("YES")
else:
    print("NO")


n = int(input())
x = list()
d = list()
count = False
##def counnter(n):
##    if i <= n:
##        return
for i in range(n):
    s, t = map(int, input().split())
    x.append(s)
    d.append(t)
for i in range(n):
    for j in range(n):
        if i != j:
            if x[i]+d[i] == x[j] and x[j]+d[j] == x[i]:
                count = True
if count == True:
    print('YES')
else:
    print('NO')


n = int(input())
l = [None]*n
t = [None]*n
for i in range(n):
    l[i],t[i] = map(int,input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('Yes')
else:
    print('No')


loc = []
for notused in range(int(input())):
	entry = list(map(int, input().split(" ")))
	loc.append((entry[0], entry[1]))
loc.sort()
locations = [x[0] for x in loc]
hit = []
for i in range(len(loc)):
	if loc[i][0]+loc[i][1] in locations:
		hit.append((i, locations.index(loc[i][0]+loc[i][1])))
toprint = "NO"
for i in range(len(hit)):
	if (hit[i][1], hit[i][0]) in hit:
		toprint = "YES"
		break;
print(toprint)


n = int(input())
xl = []
dl = []
hm = [0]*100000

for i in range(n):
    x,d = map(int,input().split())
    xl.append(x)
    dl.append(d)
    hm[x]=1
c = 0
f = 1
for i in range(n):
    if(hm[x[i]+d[i]] == 1):
        c+=1
        if(c>=2):
            f = 0
            print('YES')
            break
if(f):
    print('NO')


n = int(input())
xl = []
dl = []
hm = [0]*100000

for i in range(n):
    x,d = map(int,input().split())
    xl.append(x)
    dl.append(d)
    hm[x]=1
c = 0
f = 1
for i in range(n):
    if(hm[xl[i]+dl[i]] == 1):
        c+=1
        if(c>=2):
            f = 0
            print('YES')
            break
if(f):
    print('NO')


n = int(input())
xl = []
dl = []
hm1 = [0]*100000
hm2 = [0]*100000
for i in range(n):
    x,d = map(int,input().split())
    xl.append(x)
    dl.append(d)
    hm1[x]=1
c = 0
f = 1
for i in range(n):
    if(hm1[xl[i]+dl[i]] == 1):
        hm2[xl[i]] = xl[i]+dl[i]
    if(hm1[xl[i]+dl[i]] == 1 and hm2[xl[i]+dl[i]] == xl[i]):
        c+=1
        if(c>=2):
            f = 0
            print('YES')
            break
if(f):
    print('NO')


n = int(input())
xp = [0]*100010
xl,dl = [],[]
for _ in range(n):
    x,d = map(int,input().split())
    xp[x]=1
    xl.append(x)
    dl.append(d)
f = 1
for i in range(n):
    if(xp[xl[i]+dl[i]]):
        z = xl.index(xl[i]+dl[i])
        if(xl[z]+dl[z]==xl[i]):
            print('YES')
            f = 0
            break
if(f):
    print('NO')


n = int(input())
xl = []
dl = []
hm1 = [0]*100000
hm2 = [11111]*100000
for i in range(n):
    x,d = map(int,input().split())
    xl.append(x)
    dl.append(d)
    hm1[x]=1
c = 0
f = 1
for i in range(n):
    if(hm1[xl[i]+dl[i]] == 1 and hm2[xl[i]+dl[i]] == xl[i]):
        c+=1
        if(c>=2):
            f = 0
            print('YES')
            break
    elif(hm1[xl[i]+dl[i]] == 1):
        hm2[xl[i]] = xl[i]+dl[i]
if(f):
    print('NO')


n=int(input())
a=[]
b=[]
for i in range(n):
    x,y=[int(i) for i in input().split()]
    a.append(y)
for i in a:
    if i not in b:
        b.append(i)
if len(a)==len(b):
    print("YES")
else:
    print("NO")


n=int(input())
a=[]
c=0
b=[]
for i in range(n):
     x,y=[int(i) for i in input().split()]
     y=y+x
     a.append(x)
     b.append(y)
for i in range(n):
     for j in range(n):
          if(a[i]==b[j]) and(a[j]==b[i]):
               c=1
               break
if(c):
     print("YES")
else:
     print("NO")


a=[]
for ctr in range(int(input())):
    x,y=map(int,input().split())
    a.append([x,y])
for i in range(len(a)):
    for j in range(i+1,len(a)):
        xi=a[i][0]
        xj=a[j][0]
        di=a[i][1]
        dj=a[j][1]
        if xi+di == xj and xj+dj == xi:
            print("YES")
            exit()
print("NO")


a=[]
for ctr in range(int(input())):
    x,y=map(int,input().split())
    a.append([x,y])
for i in range(len(a)):
    for j in range(i+1,len(a)):
        xi=a[i][0]
        xj=a[j][0]
        di=a[i][1]
        dj=a[j][1]
        if xi+di == xj and xj+dj == xi:
            print("YES")
            break
print("NO")


l=[]
for ctr in range(int(input())):
    x,y=map(int,input().split())
    l.append([x,y])
for i in range(len(a)):
    for j in range(i+1,len(a)):
        xi=a[i][0]
        xj=a[j][0]
        di=a[i][1]
        dj=a[j][1]
        if xi+di == xj and xj+dj == xi:
            print("YES")
            exit()
print("NO")


def main():
    n = int(input())
    c = [tuple(map(int,input().split())) for i in range(n)]
    pairs = [(a,b) for a in c for b in c]
    for (a,b) in pairs:
        if a[0] + a[1] == b[0] and b[0] + b[1] == a[0]:
            return 'YES'
    return 'NO'

print(main())


n = int(input())
b = 0
x = [0] * 105
d = [0] * 105
s = ""
l = []
for i in range(0,n):
    s = str(input())
    l = list(s.split())
    x[i] = int(l[0])
    d[i] = int(l[1])
for i in range(0,n):
    for j in range(i + 1,n):
        if((x[i] + d[i]) == x[j]):
            if((x[j] + d[j]) == x[i]):
                b = 1
if(b == 1):
    print("YES")
else:
    print("NO")


n = int(input())
x = d = []
for i in range(n):
    xx, dd = map(int,input().split())
    x = x + [xx]
    d = d + [dd]
answer = 'NO'
i = 0
while i<n and answer=='NO':
    j = i + 1
    while j<n and answer=='NO':
        if (x[j]+d[j]==x[i]) and (x[i]+d[i]==x[j]):
            answer = 'YES'
        j = j + 1
    i = i + 1
print(answer)


def camels(lst1, lst2, n):
    flag = 0
    for i in range(n):
        for j in range(n):
            if i == j:
                continue
            if lst1[i] + lst2[i] == lst1[j] and lst1[j] + lst2[j] == lst1[i]:
                flag = 1
    if flag == 1:
        return "YES"
    return "NO"

m = int(input())
b, c = list(), list()
for i in range(m):
    h, a = [int(i) for i in input().split()]
    b.append(h)
    c.append(a)
print(camels(b, c, m))


# -*- coding: utf-8 -*-

lista,dist,cont = [],[],0
n = int(input())

for i in range(n):
    x,y = map(int,input().split())
    lista.append(x)
    dist.append(x+y)

for i in range(n):
    if dist[i] in lista:
        if lista[i] == dist[lista.index(dist[i])]: cont = 1; break

if cont == 0: print('NO')
else: print('YES')


c=[tuple(map(int,input().split())) for _ in range(int(input()))]
print('YES' if any(sum(ci)==cj[0] and sum(cj)==ci[0] for ci in c for cj in c) else 'NO')


n = int(input())

d = dict()
for _ in range(n):
	pos, throw = map(int, input().split())

	target = pos + throw
	if target in d and d[target] == pos:
		print('YES')
		exit()
	else:
		d[pos] = target

print('NO')


n = int(input())
s = set()
for _ in range(n):
    a, b = map(int, input().split())
    b = a + b
    c = (min(a, b), max(a, b))
    if c in s:
        print('YES')
        exit(0)
    else:
        s.add(c)
print('NO')


n=int(input())
s=set()
for i in range(n):
	x,d =map(int,input().split())
	if (x+d,-d) in s:
		print('YES')
		exit()
	s.add((x, d))
print('NO')


# bsdk idhar kya dekhne ko aaya hai, khud kr!!!
# import math
# from itertools import *
# import random
# import calendar
import datetime
# import webbrowser

n = int(input())
camel = []
shit = []
for i in range(n):
    x, y = map(int, input().split())
    camel.append(x)
    shit.append(y)
for i in range(n):
    flag = 0
    for j in range(0, n):
        if i != j:
            if (camel[i] + shit[i] == camel[j]) and (camel[j] + shit[j] == camel[i]):
                flag = 1
                print("YES")
                break
    if flag == 1:
        break
else:
    print("NO")


n=int(input())
s=[]
for i in range(n):
	ss=input().split()
	ss[0]=int(ss[0])
	ss[1]=int(ss[1])
	s.append(ss)
tt=False
for i in range(n):
	r=s[i][0]+s[i][1]
	for y in range(1,n):
		if r==s[y][0]:
			t=s[y][0]+s[y][1]
			if t==s[i][0]:
				tt=True
				break
if tt==False:
	print("NO")
else:
	print("YES")


N = int(input())

X = []

for n in range(N):
    p, d = map(int, input().split())
    X += [[p, d]]

f = False

for i in range(N - 1):
    for j in range(i + 1, N):
        if X[i][0] + X[i][1] == X[j][0] and X[j][0] + X[j][1] == X[i][0]:
            f = True
            break

if f:
    print("YES")
else:
    print("NO")


n = int(input())
s = [[int(i) for i in input().split()] for i in range(n)]
cnt = 0

for i in range(n):
	for j in range(n):
		if i!=j:
			if (s[i][1]+s[i][0])==s[j][0] and (s[j][1]+s[j][0])==s[i][0]:
				cnt+=1

if cnt:
	print("YES")
else:
	print("NO")


n = int(input())
a = [0] * 101
b = [0] * 101
for i in range(0, n):
    (a[i], b[i]) = map(int, input().split(' '));
    print(i);
for i in range(0, n):
    for j in range(i + 1, n):
        if a[i] + b[i] == a[j] and a[j] + b[j] == a[i]:
            print("YES")
            exit()
print("NO")


n = int(input())
a = [0] * 101
b = [0] * 101
for i in range(0, n):
    (a[i], b[i]) = map(int, input().split(' '));
for i in range(0, n):
    for j in range(i + 1, n):
        if a[i] + b[i] == a[j] and a[j] + b[j] == a[i]:
            print("YES")
            exit()
print("NO")


n=int(input())
c=[]
s=[]
flag=0
for _ in range(n):
    x,d=map(int,input().split())
    c.append(x)
    s.append(d)
for i in range(len(c)):
    for j in range(len(c)):
        if c[i]+s[i]==c[j] and c[j]+s[j]==c[i]:
            flag=1
            break
if flag==1:
    print('YES')
else:
    print('NO')


def main():
    n = int(input())
    spitting = {}

    for _ in range(n):
        [x, d] = [int(_) for _ in input().split()]
        spitting[x] = x + d

    found = False

    for i, j in spitting.items():
        if j in spitting and spitting[j] == i:
            found = True
            break

    print('YES' if found else 'NO')

if __name__ == '__main__':
    main()


data = {}
for _ in range(int(input())):
	a,b = map(int,str(input()).split(" "))
	data[a] = a+b
count= 0
for i in data:
	if data[i] in data.keys() and data[data[i]]==i:
		count+=2
	if count==2:
		print("YES")
		exit()
print("NO")


data = {}
for _ in range(int(input())):
	a,b = map(int,str(input()).split(" "))
	data[a] = a+b
if set(data.keys()).intersection(set(data.values())).__len__()!=2:
	print("NO")
else:
	print("YES")


n = int(input())
arr =[]
sol = []
for i in range(n):
    a = list(map(int,input().split()))
    arr.append(a)

for j in range(n):
    arr[j][1] = arr[j][0] + arr[j][1]
    if arr[j][::-1] in sol:
        print("Yes")
        exit(0)
    else:
        sol.append(arr[j])
print("No")


def main():
    t = int(input())
    coor = []
    for _ in range(t):
        a, b = map(int, input().split())
        coor.append([a, a+b])
    for i in range(t-1):
        for j in range(i+1, t):
            if coor[i][0] == coor[j][1] and coor[i][1] == coor[j][0]:
                print('YES')
                return
    print('NO')

if __name__ == '__main__':
    main()


n = int(input())
s = set()
for i in range(n):
	x, d = [int(x) for x in input().split()]
	if (x + d, -d) in s:
		print('YES')
		exit()
	s.add((x, d))
print('NO')


h = set()

for _ in range(int(input())):
    x, d = map(int, input().split())
    if (x,d) in h:
        print('YES')
        break
    h.add((x+d,-d))
else:
    print('NO')


n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
for i in range(n-1):
    for j in range(i+1,n):
        if a[i][0]+a[i][1] == a[j][0] and a[j][0]+a[j][1] == a[i][0]:
            print ("YES")
            exit()
print ("NO")


n = int(input())
a = []
for i in range(n):
    x,b = input().split()
    a.append([int(x),int(b)])

f = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        else:
            if a[i][0] + a[i][1] == a[j][0] and a[j][0] + a[j][1] == a[i][0]:
                f = 1
                break

if f == 1:
    print ("YES")
else:
    print ("NO")


from collections import defaultdict
ans, p = 'NO', defaultdict(int)
for i in range(int(input())):
    x, d = map(int, input().split())
    y = x + d
    if p[y] < 0 or p[x] > 0:
        ans = 'YES'
        break
    p[x], p[y] = -1, 1
print(ans)


ans, p = 'NO', set()
for i in range(int(input())):
    x, d = map(int, input().split())
    y = x + d
    if (x, y) in p:
        ans = 'YES'
        break
    p.add((y, x))
print(ans)


ans, p = 'NO', set()
for i in range(int(input())):
    x, d = map(int, input().split())
    y = x + d
    if str(x) + ' ' + str(y) in p:
        ans = 'YES'
        break
    p.add(str(y) + ' ' + str(x))
print(ans)


n = int(input())
Distancias = [0]*n
Posiciones = [0]*n
for k in range (n):
    L = input()
    L = L.split()
    P = int(L[0])
    d = int(L[1])
    Distancias[k]=P+d
    Posiciones[k]=P
Aux = 0
for k in range (n):
    for i in range (n):
        if Posiciones[k]==Distancias[i]:
            if Posiciones[i]==Distancias[k]:
                Aux =1
if Aux ==1:
    print('YES')
else:
    print('NO')


c = [tuple(map(int, input().split())) for i in range(int(input()))]

print('YES' if any(sum(ci) == cj[0] and sum(cj) == ci[0] for ci in c for cj in c) else 'NO')


n = int(input())
l = [None]*n
t = [None]*n
for i in range(n):
    l[i], t[i] = map(int, input().split())
f = False
for i in range(n):
    j = l[i] + t[i]
    if l.count(j) == 1 and t[l.index(j)] == -t[i]:
        f = True
        break
if f == True:
    print('YES')
else:
    print('NO')


#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

const int N = 2e5 + 5;

struct Euler {
  struct Edge { int nei, nr, id; };
  vector<vector<Edge>> slo;
  VI ans, used, deg, beg;
  int e_num, n;
  Euler() : e_num(0), n(0) {}
  void AddEdge(int a, int b, int idd) {
    e_num++;
    if (a > n || b > n) {
      n = max(a, b);
      slo.resize(n + 2);
      deg.resize(n + 2);
      beg.resize(n + 2);
    }
    used.PB(0);
    slo[a].PB({b, e_num, idd});
    //slo[b].PB({a, e_num});
    //deg[a]++; deg[b]++;
  }
  VVI FindEuler() { // if used many times, please clear ans, beg, used
    used.PB(0);
    assert(SZ(used) > e_num);
    //RE (i, n) { if (deg[i] % 2 == 1) { return VI(); } }
    VVI lol;
    RE (i, n) {
      if (beg[i] < SZ(slo[i])) {
        Go(i);
        lol.PB(ans);
        ans.clear();
      }
    }
    //Go(1);
    return lol;
  }
private:
  void Go(int v) {
    debug(v);
    while (beg[v] < SZ(slo[v])) {
      Edge& e = slo[v][beg[v]];
      beg[v]++;
      int nei = e.nei;
      if (used[e.nr]) { continue; }
      used[e.nr] = 1;
      Go(nei);
      ans.PB(e.id);
      //ans.PB(nei);
    }
  }
};

int a[N];
int b[N];

int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;

  int n, s;
  cin>>n>>s;
  map<int, int> scal;
  RE (i, n) {
    cin>>a[i];
    scal[a[i]] = 1;
  }
  int nxt = 1;
  for (auto& p : scal) {
    p.nd = nxt;
    nxt++;
  }
  RE (i, n) {
    a[i] = scal[a[i]];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  VVI where(n + 2);
  Euler euler;
  int moves = 0;
  RE (i, n) {
    if (a[i] == b[i]) { continue; }
    moves++;
    where[a[i]].PB(i);
    euler.AddEdge(a[i], b[i], i);
  }
  if (moves > s) {
    cout<<"-1\n";
    return 0;
  }
  int to_join = s - moves;
  if (to_join <= 2) { to_join = 0; }
  VVI cycs = euler.FindEuler();
//   debug(cycs);
//   //VVI mapuj;
//   for (auto& v : cycs) {
//     //VI nowy;
//     for (auto& x : v) {
//       int cp = x;
//       x = where[x].back();
//       where[cp].pop_back();
//     }
//     //reverse(ALL(v));
//   }
  debug(cycs);
  mini(to_join, SZ(cycs));
  VI bigger;
  VI begs;
  REP (i, to_join) {
    bigger.insert(bigger.end(), ALL(cycs.back()));
    begs.PB(cycs.back().back());
    cycs.pop_back();
  }
  if (to_join) {
    reverse(ALL(begs));
    cycs.PB(begs);
    cycs.PB(bigger);
  }
  cout<<SZ(cycs)<<endl;
  for (auto v : cycs) {
    cout<<SZ(v)<<"\n";
    for (auto x : v) {
      cout<<x<<" ";
    }
    int cp = a[v.back()];
    FORD (i, SZ(v) - 1, 1) {
      a[v[i]] = a[v[i - 1]];
    }
    a[v[0]] = cp;
    cout<<"\n";
  }
  RE (i, n) {
    cerr<<a[i]<<" ";
  }
  cerr<<endl;
  RE (i, n) {
    assert(a[i] == b[i]);
  }

  return 0;
}


#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

const int N = 2e5 + 5;

struct Euler {
  struct Edge { int nei, nr, id; };
  vector<vector<Edge>> slo;
  VI ans, used, deg, beg;
  int e_num, n;
  Euler() : e_num(0), n(0) {}
  void AddEdge(int a, int b, int idd) {
    e_num++;
    if (a > n || b > n) {
      n = max(a, b);
      slo.resize(n + 2);
      deg.resize(n + 2);
      beg.resize(n + 2);
    }
    used.PB(0);
    slo[a].PB({b, e_num, idd});
    //slo[b].PB({a, e_num});
    //deg[a]++; deg[b]++;
  }
  VVI FindEuler() { // if used many times, please clear ans, beg, used
    used.PB(0);
    assert(SZ(used) > e_num);
    //RE (i, n) { if (deg[i] % 2 == 1) { return VI(); } }
    VVI lol;
    RE (i, n) {
      if (beg[i] < SZ(slo[i])) {
        Go(i);
        lol.PB(ans);
        ans.clear();
      }
    }
    //Go(1);
    return lol;
  }
private:
  void Go(int v) {
    debug(v);
    while (beg[v] < SZ(slo[v])) {
      Edge& e = slo[v][beg[v]];
      beg[v]++;
      int nei = e.nei;
      if (used[e.nr]) { continue; }
      used[e.nr] = 1;
      Go(nei);
      ans.PB(e.id);
      //ans.PB(nei);
    }
  }
};

int a[N];
int b[N];

int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;

  int n, s;
  cin>>n>>s;
  map<int, int> scal;
  RE (i, n) {
    cin>>a[i];
    scal[a[i]] = 1;
  }
  int nxt = 1;
  for (auto& p : scal) {
    p.nd = nxt;
    nxt++;
  }
  RE (i, n) {
    a[i] = scal[a[i]];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  VVI where(n + 2);
  Euler euler;
  int moves = 0;
  RE (i, n) {
    if (a[i] == b[i]) { continue; }
    moves++;
    where[a[i]].PB(i);
    euler.AddEdge(a[i], b[i], i);
  }
  if (moves > s) {
    cout<<"-1\n";
    return 0;
  }
  int to_join = min(n, s - moves);
  if (to_join <= 2) { to_join = 0; }
  VVI cycs = euler.FindEuler();
//   debug(cycs);
//   //VVI mapuj;
//   for (auto& v : cycs) {
//     //VI nowy;
//     for (auto& x : v) {
//       int cp = x;
//       x = where[x].back();
//       where[cp].pop_back();
//     }
//     //reverse(ALL(v));
//   }
  debug(cycs);
  mini(to_join, SZ(cycs));
  VI bigger;
  VI begs;
  REP (i, to_join) {
    bigger.insert(bigger.end(), ALL(cycs.back()));
    begs.PB(cycs.back().back());
    cycs.pop_back();
  }
  if (to_join) {
    reverse(ALL(begs));
    cycs.PB(begs);
    cycs.PB(bigger);
  }
  cout<<SZ(cycs)<<endl;
  for (auto v : cycs) {
    cout<<SZ(v)<<"\n";
    for (auto x : v) {
      cout<<x<<" ";
    }
    int cp = a[v.back()];
    FORD (i, SZ(v) - 1, 1) {
      a[v[i]] = a[v[i - 1]];
    }
    a[v[0]] = cp;
    cout<<"\n";
  }
  RE (i, n) {
    cerr<<a[i]<<" ";
  }
  cerr<<endl;
  RE (i, n) {
    assert(a[i] == b[i]);
  }

  return 0;
}


/************************************************
 * Au: Hany01
 * Date: Aug 26th, 2018
 * Prob: LOJ2818 CF1012E EJOI2018
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
	static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 2e5 + 5;

int n, s, a[maxn], b[maxn], fa[maxn], fu, fv, p[maxn], stk[maxn], top, stk1[maxn], top1, vis[maxn], v[maxn], bk, tot, mgcir, num, las;
map<int, int> pos;

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void uni(int u, int v) { if ((u = find(u)) != (v = find(v))) fa[u] = v; }

int main()
{
#ifdef hany01
	freopen("loj2818.in", "r", stdin);
	freopen("loj2818.out", "w", stdout);
#endif

	n = read(), s = read();
	For(i, 1, n) a[i] = b[i] = read();
	sort(b + 1, b + 1 + n);
	Fordown(i, n, 1) pos[b[i]] = i;
	For(i, 1, n) fa[i] = i;
	For(i, 1, n) if (a[i] != b[i]) {
		while (a[pos[a[i]]] == b[pos[a[i]]]) ++ pos[a[i]];
		uni(i, pos[a[i]]), p[pos[a[i]]] = i, v[i] = pos[a[i]] ++, ++ tot;
	}
	if (tot > s) { puts("-1"); return 0; }

	For(i, 2, n) if (a[i] != b[i]) {
		if (las && b[i] == b[las]) {
			if ((fu = find(p[i])) != (fv = find(p[las])))
				swap(v[p[i]], v[p[las]]), fa[fu] = fv;
		} else las = i;
	}

	For(i, 1, n) if (!vis[i] && a[i] != b[i]) {
		int u = i;
		do vis[u] = 1, u = v[u]; while (u != i);
		++ num;
	}
	Set(vis, 0);

	if (s == tot || s == tot + 1 || num <= 2) {
		printf("%d\n", num);
		For(i, 1, n) if (!vis[i] && a[i] != b[i]) {
			int u = i; top = 0;
			do stk[++ top] = u, vis[u] = 1, u = v[u]; while (u != i);
			printf("%d\n", top);
			For(j, 1, top) printf("%d ", stk[j]);
			putchar('\n');
		}
		return 0;
	}

	mgcir = min(s - tot, num), printf("%d\n", 2 + num - mgcir);
	For(i, 1, n) if (!vis[i] && a[i] != b[i]) {
		int u = i;
		do stk[++ top] = u, vis[u] = 1, u = v[u]; while (u != i);
		stk1[++ top1] = i;
		if (!-- mgcir) { bk = i; break; }
	}
	printf("%d\n", top);
	For(j, 1, top) printf("%d ", stk[j]);
	putchar('\n');
	printf("%d\n", top1);
	Fordown(j, top1, 1) printf("%d ", stk1[j]);
	putchar('\n');
	For(i, bk + 1, n) if (!vis[i] && a[i] != b[i]) {
		int u = i; top = 0;
		do stk[++ top] = u, vis[u] = 1, u = v[u]; while (u != i);
		printf("%d\n", top);
		For(j, 1, top) printf("%d ", stk[j]);
		putchar('\n');
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename S, typename T>
void xmin(S&a, T const&b){if(b<a) a=b;}
template<typename S, typename T>
void xmax(S&a, T const&b){if(b>a) a=b;}

vector<vector<pair<int, int> > > g;

vector<int> eu;
vector<int> cured;

void rec(int u){
    for(int&i=cured[u];i<(int)g[u].size();){
        int v = g[u][i].first;
        int x = g[u][i].second;
        ++i;
        //cerr << u << " -> " << v << " : " << x << "\n";
        rec(v);
        eu.push_back(x);
    }
}

signed main()
{
    #ifdef LOCAL_RUN
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    int TTT; cin >> TTT; while(TTT--){
    #else
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    #endif // LOCAL_RUN

    int n, s;
    cin >> n >> s;
    vector<int> a(n), b;
    for(auto &e:a){
        cin >> e;
    }
    b = a;
    sort(b.begin(), b.end());
    for(auto &e:a){
        e = lower_bound(b.begin(), b.end(), e) - b.begin();
    }
    b = a;
    sort(b.begin(), b.end());
    /*vector<int> l(n, -1);
    for(int i=0;i<n;++i){
        if(l[a[i]] == -1){
            l[a[i]] = i;
        }
    }*/

    g.clear();
    g.resize(n);
    for(int i=0;i<n;++i){
        if(a[i] != b[i]){
            //cerr << a[i] << " -> " << b[i] << " " << i << "\n";
            g[a[i]].emplace_back(b[i], i);
        }
    }
    vector<vector<int> > cyc;
    int tot = 0;
    cured.assign(n, 0);
    for(int i=0;i<n;++i){
        eu.clear();
        rec(i);
        if(!eu.empty()){
            cyc.push_back(eu);
            tot+=eu.size();
        }
    }
    if(tot > s){
        cout << "-1\n";
    } else {
        int spare = s - tot;
        if(spare > 2 && cyc.size()>2){
            xmin(spare, (int)cyc.size());
            vector<int> big, small;
            for(int i=0;i<spare;++i){
                small.push_back(cyc.back().back());
                big.insert(big.end(), cyc.back().begin(), cyc.back().end());
                cyc.pop_back();
            }
            reverse(small.begin(), small.end());
            cyc.push_back(small);
            cyc.push_back(big);
        }
        cout << cyc.size() << "\n";
        for(auto const&e:cyc){
            cout << e.size() << "\n";
            for(auto const&f:e){
                cout << 1+f << " ";
            }
            cout << "\n";
        }
    }

    #ifdef LOCAL_RUN
    }
    #endif // LOCAL_RUN
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int n, K;
map<int, int>Map;
int A[201000], B[201000], w[201000];
vector<int>GG[201000];
int GPV[201000];
int tA[201000];
vector<int>G;
int UF[201000], sss;
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
void Merge(int a, int b) {
	a = Find(a), b = Find(b);
	if (a != b)UF[a] = b;
}
int v[201000];
vector<vector<int> >RR;
void Pro(vector<int> &T) {
	RR.push_back(T);
	for (int i = T.size() - 1; i >= 0; i--) {
		int ni = i - 1;
		if (ni < 0)ni = T.size() - 1;
		tA[T[ni]] = A[T[i]];
	}
	for (auto &t : T)A[t] = tA[t];
}
void Go(int a) {
	vector<int>T;
	int t=a;
	while (1) {
		v[t] = 1;
		T.push_back(t);
		t = w[t];
		if (t == a)break;
	}
	Pro(T);
	return;
}
bool Do(int ck) {
	int i, c = 0;
	for (i = 1; i <= n; i++) {
		B[i] = A[i];
		GG[i].clear();
		UF[i] = i;
		GPV[i] = 0;
		v[i] = 0;
	}
	sort(B + 1, B + n + 1);
	Map.clear();
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i])c++;
	}
	if (c > K) {
		puts("-1");
		return false;
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			if (!Map.count(A[i])) {
				Map[A[i]] = i;
				GG[i].push_back(i);
			}
			else {
				GG[Map[A[i]]].push_back(i);
			}
		}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			G.push_back(i);
			int x = Map[B[i]];
			w[i] = GG[x][GPV[x]];
			GPV[x]++;
		}
	}
	for (auto &x : G) {
		Merge(x, w[x]);
	}
	for (i = 0; i + 1 < G.size(); i++) {
		int x = G[i], y = G[i + 1];
		if (B[x] == B[y] && Find(x) != Find(y)) {
			swap(w[x], w[y]);
			Merge(x, y);
		}
	}
	int res = 0;
	vector<int>TP;
	for (auto &x : G) {
		if (!v[Find(x)]) {
			TP.push_back(x);
			res++;
			v[Find(x)] = 1;
		}
	}
	if (!ck) {
		int cur = G.size();
		int rr = min(res, max(K - cur, 0));
		if (rr >= 3) {
			res = res - rr + 2;
			vector<int>TP2;
			for (int i = 0; i < rr; i++) {
				TP2.push_back(TP[i]);
			}
			Pro(TP2);
			return Do(1);
		}
	}
	for (i = 1; i <= n; i++)v[i] = 0;
	for (auto &x : G) {
		if (!v[x]) {
			Go(x);
		}
	}
	return true;
}
int main() {
	int i;
	scanf("%d%d", &n, &K);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]);
	}
	if (Do(0)) {
		printf("%d\n", RR.size());
		for (auto &t : RR) {
			printf("%d\n", t.size());
			for (int i = t.size() - 1; i >= 0; i--)printf("%d ", t[i]);
			puts("");
		}
	}
}


#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int n, K;
map<int, int>Map;
int A[201000], B[201000], w[201000];
vector<int>GG[201000];
int GPV[201000];
int tA[201000];
vector<int>G;
int UF[201000], sss;
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
void Merge(int a, int b) {
	a = Find(a), b = Find(b);
	if (a != b)UF[a] = b;
}
int v[201000];
vector<vector<int> >RR;
void Pro(vector<int> &T) {
	RR.push_back(T);
	for (int i = T.size() - 1; i >= 0; i--) {
		int ni = i - 1;
		if (ni < 0)ni = T.size() - 1;
		tA[T[ni]] = A[T[i]];
	}
	for (auto &t : T)A[t] = tA[t];
}
void Go(int a) {
	vector<int>T;
	int t=a;
	while (1) {
		v[t] = 1;
		T.push_back(t);
		t = w[t];
		if (t == a)break;
	}
	Pro(T);
	return;
}
bool Do(int ck) {
	int i, c = 0;
	for (i = 1; i <= n; i++) {
		B[i] = A[i];
		GG[i].clear();
		UF[i] = i;
		GPV[i] = 0;
		v[i] = 0;
	}
	sort(B + 1, B + n + 1);
	Map.clear();
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i])c++;
	}
	if (c > K) {
		puts("-1");
		return false;
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			if (!Map.count(A[i])) {
				Map[A[i]] = i;
				GG[i].push_back(i);
			}
			else {
				GG[Map[A[i]]].push_back(i);
			}
		}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			G.push_back(i);
			int x = Map[B[i]];
			w[i] = GG[x][GPV[x]];
			GPV[x]++;
		}
	}
	for (auto &x : G) {
		Merge(x, w[x]);
	}
	for (i = 0; i + 1 < G.size(); i++) {
		int x = G[i], y = G[i + 1];
		if (B[x] == B[y] && Find(x) != Find(y)) {
			swap(w[x], w[y]);
			Merge(x, y);
		}
	}
	int res = 0;
	vector<int>TP;
	for (auto &x : G) {
		if (!v[Find(x)]) {
			TP.push_back(x);
			res++;
			v[Find(x)] = 1;
		}
	}
	if (!ck) {
		int cur = G.size();
		int rr = min(res, max(K - cur, 0));
		if (rr >= 3) {
			res = res - rr + 2;
			vector<int>TP2;
			for (int i = 0; i < rr; i++) {
				TP2.push_back(TP[i]);
			}
			Pro(TP);
			return Do(1);
		}
	}
	for (i = 1; i <= n; i++)v[i] = 0;
	for (auto &x : G) {
		if (!v[x]) {
			Go(x);
		}
	}
	return true;
}
int main() {
	int i;
	scanf("%d%d", &n, &K);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]);
	}
	if (Do(0)) {
		printf("%d\n", RR.size());
		for (auto &t : RR) {
			printf("%d\n", t.size());
			for (int i = t.size() - 1; i >= 0; i--)printf("%d ", t[i]);
			puts("");
		}
	}
}


#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int n, K;
map<int, int>Map;
int A[201000], B[201000], w[201000];
vector<int>GG[201000];
int GPV[201000];
int tA[201000];
vector<int>G;
int UF[201000], sss;
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
void Merge(int a, int b) {
	a = Find(a), b = Find(b);
	if (a != b)UF[a] = b;
}
int v[201000];
void Go(int a) {
	vector<int>T;
	int t=a;
	while (1) {
		v[t] = 1;
		T.push_back(t);
		t = w[t];
		if (t == a)break;
	}
	sss += T.size();
	printf("%d\n", T.size());
	for (int i = T.size() - 1; i >= 0; i--) {
		printf("%d ", T[i]);
		int ni = i - 1;
		if (ni < 0)ni = T.size() - 1;
		tA[T[ni]] = A[T[i]];
	}
	for (auto &t : T)A[t] = tA[t];
	printf("\n");
	return;
}
int main() {
	int i, c = 0;
	scanf("%d%d", &n, &K);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]);
		B[i] = A[i];
		UF[i] = i;
	}
	sort(B + 1, B + n + 1);
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i])c++;
	}
	if (c > K) {
		puts("-1");
		return 0;
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			if (!Map.count(A[i])) {
				Map[A[i]] = i;
				GG[i].push_back(i);
			}
			else {
				GG[Map[A[i]]].push_back(i);
			}
		}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			G.push_back(i);
			int x = Map[B[i]];
			w[i] = GG[x][GPV[x]];
			GPV[x]++;
		}
	}
	for (auto &x : G) {
		Merge(x, w[x]);
	}
	for (i = 0; i+1 < G.size(); i++) {
		int x = G[i], y = G[i + 1];
		if (B[x] == B[y] && Find(x) != Find(y)) {
			swap(w[x], w[y]);
			Merge(x, y);
		}
	}
	int res = 0;
	for (auto &x : G) {
		if (!v[Find(x)]) {
			res++;
			v[Find(x)] = 1;
		}
	}
	printf("%d\n", res);
	for (i = 1; i <= n; i++)v[i] = 0;
	int tt = 0;
	for (auto &x : G) {
		if (!v[x]) {
			tt++;
			Go(x);
		}
	}
	if (tt != res) {
		while (1) {}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			while (1) {}
		}
	}
	if (sss > K) {
		while (1) {}
	}
}


#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int n, K;
map<int, int>Map;
int A[201000], B[201000], w[201000];
vector<int>GG[201000];
int GPV[201000];

vector<int>G;
int UF[201000];
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
void Merge(int a, int b) {
	a = Find(a), b = Find(b);
	if (a != b)UF[a] = b;
}
int v[201000];
void Go(int a) {
	vector<int>T;
	int t=a;
	while (1) {
		v[t] = 1;
		T.push_back(t);
		t = w[t];
		if (t == a)break;
	}
	printf("%d\n", T.size());
	for (int i = T.size() - 1; i >= 0; i--)printf("%d ", T[i]);
	printf("\n");
	return;
}
int main() {
	int i, c = 0;
	scanf("%d%d", &n, &K);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]);
		B[i] = A[i];
		UF[i] = i;
	}
	sort(B + 1, B + n + 1);
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i])c++;
	}
	if (c > K) {
		puts("-1");
		return 0;
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			if (!Map.count(A[i])) {
				Map[A[i]] = i;
				GG[i].push_back(i);
			}
			else {
				GG[Map[A[i]]].push_back(i);
			}
		}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			G.push_back(i);
			int x = Map[B[i]];
			w[i] = GG[x][GPV[x]];
			GPV[x]++;
		}
	}
	for (auto &x : G) {
		Merge(x, w[x]);
	}
	for (i = 0; i+1 < G.size(); i++) {
		int x = G[i], y = G[i + 1];
		if (B[x] == B[y] && Find(x) != Find(y)) {
			swap(w[x], w[y]);
			Merge(x, y);
		}
	}
	int res = 0;
	for (auto &x : G) {
		if (!v[Find(x)]) {
			res++;
			v[Find(x)] = 1;
		}
	}
	printf("%d\n", res);
	for (i = 1; i <= n; i++)v[i] = 0;
	int tt = 0;
	for (auto &x : G) {
		if (!v[x]) {
			tt++;
			Go(x);
		}
	}
	if (tt != res) {
		while (1) {}
	}
}


#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int n, K;
map<int, int>Map;
int A[201000], B[201000], w[201000];
vector<int>GG[201000];
int GPV[201000];

vector<int>G;
int UF[201000];
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
void Merge(int a, int b) {
	a = Find(a), b = Find(b);
	if (a != b)UF[a] = b;
}
int v[201000];
void Go(int a) {
	vector<int>T;
	int t=a;
	while (1) {
		v[t] = 1;
		T.push_back(t);
		t = w[t];
		if (t == a)break;
	}
	printf("%d\n", T.size());
	for (int i = T.size() - 1; i >= 0; i--)printf("%d ", T[i]);
	printf("\n");
	return;
}
int main() {
	int i, c = 0;
	scanf("%d%d", &n, &K);
	for (i = 1; i <= n; i++) {
		scanf("%d", &A[i]);
		B[i] = A[i];
		UF[i] = i;
	}
	sort(B + 1, B + n + 1);
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i])c++;
	}
	if (c > K) {
		puts("-1");
		return 0;
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			if (!Map.count(A[i])) {
				Map[A[i]] = i;
				GG[i].push_back(i);
			}
			else {
				GG[Map[A[i]]].push_back(i);
			}
		}
	}
	for (i = 1; i <= n; i++) {
		if (A[i] != B[i]) {
			G.push_back(i);
			int x = Map[B[i]];
			w[i] = GG[x][GPV[x]];
			GPV[x]++;
		}
	}
	for (auto &x : G) {
		Merge(x, w[x]);
	}
	for (i = 0; i+1 < G.size(); i++) {
		int x = G[i], y = G[i + 1];
		if (B[x] == B[y] && Find(x) != Find(y)) {
			swap(w[x], w[y]);
			Merge(x, y);
		}
	}
	int res = 0;
	for (auto &x : G) {
		if (!v[Find(x)]) {
			res++;
			v[Find(x)] = 1;
		}
	}
	printf("%d\n", res);
	for (i = 1; i <= n; i++)v[i] = 0;
	for (auto &x : G) {
		if (!v[x]) {
			Go(x);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
int a[N],p[N],head[N],now=0;
pair<int,int> b[N];
vector<int> cycle[N];
int findd(int x){
	if(head[x]<0){
		return x;
	}
	int y=findd(head[x]);
	head[x]=y;
	return y;
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	head[x]+=head[y];
	head[y]=x;
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,s,sz=0;
	cin>>n>>s;
	for(i=1;i<=n;i++){
		head[i]=-1;
		cin>>a[i];
		b[i]={a[i],i};
	}
	sort(b+1,b+1+n);
	i=j=1;
	while(i<=n){
		while(j<=n&&b[i].first==b[j].first){
			j++;
		}
		now++;
		while(i<j){
			a[b[i].second]=b[i].first=now;
			i++;
		}
	}
	for(i=1;i<=n;i++){
		if(a[i]!=b[i].first){
			sz++;
			head[i]=-1;
			b[sz]={a[i],i};
			a[sz]=i;
		}
	}
	if(sz==0){
		cout<<0;
		return 0;
	}
	if(sz>s){
		cout<<-1;
		return 0;
	}
	sort(b+1,b+1+sz);
	sort(a+1,a+1+sz);
	for(i=1;i<=sz;i++){
		p[b[i].second]=a[i];
		if(!samee(b[i].second,a[i])){
			unionn(b[i].second,a[i]);
		}
	}
	i=j=1;
	while(i<=sz){
		while(j<=sz&&b[i].first==b[j].first){
			j++;
		}
		for(k=i+1;k<j;k++){
			if(!samee(b[i].second,b[k].second)){
				swap(p[b[i].second],p[b[k].second]);
				unionn(b[i].second,b[k].second);
			}
		}
		i=j;
	}
//	for(i=1;i<=n;i++){
//		cout<<p[i]<<' ';
//	}
//	cout<<endl;
	int cnt=0;
	for(i=1;i<=n;i++){
		if(p[i]){
			cycle[cnt].push_back(i);
			j=p[i];
			while(j!=i){
				cycle[cnt].push_back(j);
				j=p[j];
			}
			for(k=0;k<cycle[cnt].size();k++){
				p[cycle[cnt][k]]=0;
			}
			cnt++;
		}
	}
	if(cnt<=1||s==sz+1||s==sz){
		cout<<cnt<<'\n';
		for(i=0;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
	}
	else{
		cout<<cnt-min(cnt,s-sz)+2<<'\n';
		j=min(s-sz,cnt);
		for(i=j;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
		cout<<j<<'\n';
		k=0;
		for(i=0;i<j;i++){
			k+=cycle[i].size();
			cout<<cycle[i][0]<<' ';
		}
		cout<<'\n';
		cout<<k<<'\n';
		for(i=j-1;i>-1;i--){
			for(l=1;l<cycle[i].size();l++){
				cout<<cycle[i][l]<<' ';
			}
			cout<<cycle[i][0]<<' ';
		}
	}
}
/*
6 9
2 1 4 3 6 5
*/


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
int a[N],p[N],head[N],now=0;
pair<int,int> b[N];
vector<int> cycle[N];
int findd(int x){
	if(head[x]<0){
		return x;
	}
	int y=findd(head[x]);
	head[x]=y;
	return y;
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	head[x]+=head[y];
	head[y]=x;
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,s,sz=0;
	cin>>n>>s;
	for(i=1;i<=n;i++){
		head[i]=-1;
		cin>>a[i];
		b[i]={a[i],i};
	}
	sort(b+1,b+1+n);
	i=j=1;
	while(i<=n){
		while(j<=n&&b[i].first==b[j].first){
			j++;
		}
		now++;
		while(i<j){
			a[b[i].second]=b[i].first=now;
			i++;
		}
	}
	for(i=1;i<=n;i++){
		if(a[i]!=b[i].first){
			sz++;
			head[i]=-1;
			b[sz]={a[i],i};
			a[sz]=i;
		}
	}
	if(sz==0){
		cout<<0;
		return 0;
	}
	if(sz>s){
		cout<<-1;
		return 0;
	}
	sort(b+1,b+1+sz);
	sort(a+1,a+1+sz);
	for(i=1;i<=sz;i++){
		p[b[i].second]=a[i];
		if(!samee(b[i].second,a[i])){
			unionn(b[i].second,a[i]);
		}
	}
	i=j=1;
	while(i<=sz){
		while(j<=sz&&b[i].first==b[j].first){
			j++;
		}
		for(k=i+1;k<j;k++){
			if(!samee(b[i].second,b[k].second)){
				swap(p[b[i].second],p[b[k].second]);
				unionn(b[i].second,b[k].second);
			}
		}
		i=j;
	}
//	for(i=1;i<=n;i++){
//		cout<<p[i]<<' ';
//	}
//	cout<<endl;
	int cnt=0;
	for(i=1;i<=sz;i++){
		if(p[a[i]]){
			cycle[cnt].push_back(a[i]);
			j=p[a[i]];
			while(j!=a[i]){
				cycle[cnt].push_back(j);
				j=p[j];
			}
			for(k=0;k<cycle[cnt].size();k++){
				p[cycle[cnt][k]]=0;
			}
			cnt++;
		}
	}
	if(cnt<=1||s==sz+1||s==sz){
		cout<<cnt<<'\n';
		for(i=0;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
	}
	else{
		cout<<cnt-min(cnt,s-sz)+2<<'\n';
		j=min(s-sz,cnt);
		for(i=j;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
		cout<<j<<'\n';
		k=0;
		for(i=0;i<j;i++){
			k+=cycle[i].size();
			cout<<cycle[i][0]<<' ';
		}
		cout<<'\n';
		cout<<k<<'\n';
		for(i=j-1;i>-1;i--){
			for(l=1;l<cycle[i].size();l++){
				cout<<cycle[i][l]<<' ';
			}
			cout<<cycle[i][0]<<' ';
		}
	}
}
/*
6 9
2 1 4 3 6 5
*/


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
int a[N],p[N],head[N],now=0,idx[N];
pair<int,int> b[N];
int findd(int x){
	if(head[x]<0){
		return x;
	}
	return head[x]=findd(head[x]);
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	if(x==y){
		return;
	}
	head[x]+=head[y];
	head[y]=x;
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
vector<int> cycle[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,s,sz=0;
	cin>>n>>s;
	for(i=1;i<=n;i++){
		head[i]=-1;
		cin>>a[i];
		b[i]={a[i],i};
	}
	sort(b+1,b+1+n);
	now=0;
	i=j=1;
	while(i<=n){
		while(j<=n&&b[i].first==b[j].first){
			j++;
		}
		now++;
		while(i<j){
			b[i].first=now;
			a[b[i].second]=now;
			i++;
		}
	}
	for(i=1;i<=n;i++){
		if(a[i]!=b[i].first){
			sz++;
			head[i]=-1;
			b[sz]={a[i],i};
			idx[sz]=i;
		}
	}
	if(sz==0){
		cout<<0;
		return 0;
	}
	if(sz>s){
		cout<<-1;
		return 0;
	}
	sort(b+1,b+1+sz);
	sort(idx+1,idx+1+sz);
	for(i=1;i<=sz;i++){
		p[b[i].second]=idx[i];
		unionn(b[i].second,idx[i]);
	}
	i=j=1;
	while(i<=n){
		while(j<=n&&b[i].first==b[j].first){
			j++;
		}
		for(k=i+1;k<j;k++){
			if(!samee(b[i].second,b[k].second)){
				swap(p[b[i].second],p[b[k].second]);
				unionn(b[i].second,b[k].second);
			}
		}
		i=j;
	}
//	for(i=1;i<=n;i++){
//		cout<<p[i]<<' ';
//	}
//	cout<<endl;
	int cnt=0;
	for(i=1;i<=n;i++){
		if(p[i]){
			cycle[cnt].push_back(i);
			j=p[i];
			while(j!=i){
				cycle[cnt].push_back(j);
				j=p[j];
			}
			for(k=0;k<cycle[cnt].size();k++){
				p[cycle[cnt][k]]=0;
			}
			cnt++;
		}
	}
	if(cnt<=1||s==sz+1||s==sz){
		cout<<cnt<<'\n';
		for(i=0;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
	}
	else{
		cout<<cnt-min(cnt,s-sz)+2<<'\n';
		j=min(s-sz,cnt);
		for(i=j;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
		cout<<j<<'\n';
		k=0;
		for(i=0;i<j;i++){
			k+=cycle[i].size();
			cout<<cycle[i][0]<<' ';
		}
		cout<<'\n';
		cout<<k<<'\n';
		for(i=j-1;i>-1;i--){
			for(l=1;l<cycle[i].size();l++){
				cout<<cycle[i][l]<<' ';
			}
			cout<<cycle[i][0]<<' ';
		}
	}
}
/*
6 9
2 1 4 3 6 5
*/


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
int a[N],p[N],head[N],now=0,idx[N];
pair<int,int> b[N],c[N];
int findd(int x){
	if(head[x]<0){
		return x;
	}
	return head[x]=findd(head[x]);
}
void unionn(int x,int y){
	x=findd(x),y=findd(y);
	if(x==y){
		return;
	}
	head[x]+=head[y];
	head[y]=x;
}
bool samee(int x,int y){
	return findd(x)==findd(y);
}
vector<int> cycle[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,i,j,k,l,s,sz=0;
	cin>>n>>s;
	for(i=1;i<=n;i++){
		head[i]=-1;
		cin>>a[i];
		b[i]={a[i],i};
	}
	sort(b+1,b+1+n);
	now=0;
	i=j=1;
	while(i<=n){
		while(j<=n&&b[i].first==b[j].first){
			j++;
		}
		now++;
		while(i<j){
			b[i].first=now;
			a[b[i].second]=now;
			i++;
		}
	}
	for(i=1;i<=n;i++){
		if(a[i]!=b[i].first){
			sz++;
			head[i]=-1;
			c[sz]={a[i],i};
			idx[sz]=i;
		}
	}
	if(sz==0){
		cout<<0;
		return 0;
	}
	if(sz>s){
		cout<<-1;
		return 0;
	}
	sort(c+1,c+1+sz);
	sort(idx+1,idx+1+sz);
	for(i=1;i<=sz;i++){
		p[c[i].second]=idx[i];
		unionn(c[i].second,idx[i]);
	}
	i=j=1;
	while(i<=n){
		while(j<=n&&c[i].first==c[j].first){
			j++;
		}
		for(k=i+1;k<j;k++){
			if(!samee(c[i].second,c[k].second)){
				swap(p[c[i].second],p[c[k].second]);
				unionn(c[i].second,c[k].second);
			}
		}
		i=j;
	}
//	for(i=1;i<=n;i++){
//		cout<<p[i]<<' ';
//	}
//	cout<<endl;
	int cnt=0;
	for(i=1;i<=n;i++){
		if(p[i]){
			vector<int> wow;
			wow.push_back(i);
			j=p[i];
			while(j!=i){
				wow.push_back(j);
				j=p[j];
			}
			for(k=0;k<wow.size();k++){
				p[wow[k]]=0;
			}
			cycle[cnt]=wow;
			cnt++;
		}
	}
	if(cnt<=1||s==sz+1||s==sz){
		cout<<cnt<<'\n';
		for(i=0;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
	}
	else{
		cout<<cnt-min(cnt,s-sz)+2<<'\n';
		j=min(s-sz,cnt);
		for(i=j;i<cnt;i++){
			cout<<cycle[i].size()<<'\n';
			for(j=0;j<cycle[i].size();j++){
				cout<<cycle[i][j]<<' ';
			}
			cout<<'\n';
		}
		cout<<j<<'\n';
		k=0;
		for(i=0;i<j;i++){
			k+=cycle[i].size();
			cout<<cycle[i][0]<<' ';
		}
		cout<<'\n';
		cout<<k<<'\n';
		for(i=j-1;i>-1;i--){
			for(l=1;l<cycle[i].size();l++){
				cout<<cycle[i][l]<<' ';
			}
			cout<<cycle[i][0]<<' ';
		}
	}
}
/*
6 9
2 1 4 3 6 5
*/