#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l) / (a * b) > 0) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l) / (a * b) * pref[a * b];

            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b)] - pref[(l - 1) % (a * b)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b + 1)] - pref[(l - 1) % (a * b + 1)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            int len = (r - l + 1);
            l %= a * b;
            r %= a * b;
            if (l + len <= a * b) {
                now_ans += pref[r];
                now_ans -= pref[l - 1];
            } else {
                if (l != 0) now_ans += pref[a * b] - pref[l - 1];
                //else now_ans += pref[a * b] - pref[a * b - 1];
                if (r != 0) now_ans += pref[r];
            }
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            int len = (r - l + 1);
            l %= a * b;
            r %= a * b;
            if (l + len <= a * b) {
                if (l + len != 0) now_ans += pref[l + len - 1];
                if (l != 0) now_ans -= pref[l - 1];
            } else {
                if (l != 0) now_ans += pref[a * b] - pref[l - 1];
                //else now_ans += pref[a * b] - pref[a * b - 1];
                if (r != 0) now_ans += pref[r];
            }
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb((i % a) % b != (i % b) % a);
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        int new_r = r - (r % (a * b));
        int new_l = l + (a * b - (l % (a * b)));
        now_ans += (new_r - new_l) / (a * b) * pref[a * b];
        l %= a * b;
        now_ans += pref[a * b] - pref[l - 1];
        r %= a * b;
        now_ans += pref[r];
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) > a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    /*for (auto now : pref) {
        cout << now << ' ';
    }
    cout << endl;*/
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l + 1) >= a * b) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l + 1) / (a * b) * pref[a * b];
            l %= a * b;
            if (l != 0) now_ans += pref[a * b] - pref[l - 1];
            //else now_ans += pref[a * b] - pref[a * b - 1];
            r %= a * b;
            if (r != 0) now_ans += pref[r];
        } else {
            now_ans += pref[r % (a * b + 1)] - pref[(l - 1) % (a * b + 1)];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    if (a == 1 || b == 1) {
        for (int i = 0; i < q; i++) {
            int l, r;
            cin >> l >> r;
            cout << 0 << ' ';
        }
        cout << endl;
        return;
    }
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb(((i % a) % b) != ((i % b) % a));
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < a * b; i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        if ((r - l) / (a * b) > 1) {
            int new_r = r - (r % (a * b));
            int new_l = l + ((a * b - (l % (a * b))) % (a * b));
            now_ans += (new_r - new_l) / (a * b) * pref[a * b];

            l %= a * b;
            now_ans += pref[a * b] - pref[l - 1];
            r %= a * b;
            now_ans += pref[r];
        } else {
            now_ans += pref[r] - pref[l - 1];
        }
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int         long long
#define ll          long long
#define double      long double
#define pb          push_back
#define in          insert
#define mp          make_pair
#define f           first
#define s           second
#define x           first
#define y           second
#define all(el)     el.begin(), el.end()
#define allr(el)    el.rbegin(), el.rend()
#define sz(el)      (int)el.size()
#define endl        "\n"

void solve() {

    int a, b, q;
    cin >> a >> b >> q;
    vector<int> ans;
    for (int i = 1; i <= a * b; i++) {
        ans.pb((i % a) % b != (i % b) % a);
    }
    vector<int> pref(a * b + 1);
    for (int i = 0; i < sz(ans); i++) {
        pref[i + 1] = pref[i] + ans[i];
    }
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        int now_ans = 0;
        now_ans += (r - l) / (a * b) * pref[a * b];
        l %= a * b;
        now_ans += pref[a * b] - pref[l - 1];
        r %= a * b;
        now_ans -= pref[a * b] - pref[r];
        cout << now_ans << ' ';
    }
    cout << endl;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			if(r<d){
				cout<<0<<" ";
				continue;
			}
			count=r-l+1;
			ll start,end;
			for(ll j=l;j<r;j++){
				if(j%c==0){
					start=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here1"<<endl;
			for(int j=r;j>=start;j--){
				if(j%c==0){
					count--;
					end=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here2"<<endl;
			ll tmp=(end-start)/c;
			tmp=max(tmp,(ll)0);
			count-=tmp*d;
			cout<<count<<" ";
		}
		cout<<endl;
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			count=r-l+1;
			for(int j=l;j<=r;j++){
				if(j%c<d){
					count--;
				}
			}
			cout<<count<<endl;
		}
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			if(r<d){
				cout<<0<<" ";
				continue;
			}
			count=r-l+1;
			ll start=r,end=r;
			for(ll j=l;j<=r;j++){
				if(j%c==0){
					start=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here1"<<endl;
			for(int j=r;j>=start;j--){
				if(j%c==0){
					count--;
					end=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here2"<<endl;
			ll tmp=(end-start)/c;
			tmp=max(tmp,(ll)0);
			count-=tmp*d;
			cout<<count<<" ";
		}
		cout<<endl;
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			if(r<d){
				cout<<0<<" ";
				continue;
			}
			count=r-l+1;
			ll start=r,end=r;
			for(ll j=l;j<=r;j++){
				if(j%c==0){
					start=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here1"<<endl;
			for(int j=r;j>=start;j--){
				if(j%c==0){
					count--;
					end=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here2"<<endl;
			ll tmp=(end-start)/c;
			tmp=max(tmp,(ll)0);
			count-=tmp*d;
			cout<<count<<" ";
		}
		cout<<endl;
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			if(r<d){
				cout<<0<<endl;
				continue;
			}
			count=r-l+1;
			ll start,end;
			for(ll j=l;j<r;j++){
				if(j%c==0){
					start=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here1"<<endl;
			for(int j=r;j>=start;j--){
				if(j%c==0){
					count--;
					end=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here2"<<endl;
			ll tmp=(end-start)/c;
			tmp=max(tmp,(ll)0);
			count-=tmp*d;
			cout<<count<<endl;
		}
	}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vl vector<ll>
#define pl pair<ll,ll>

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	int t=1;
	cin>>t;
	while(t--){
		ll a,b,q,c,d;
		cin>>a>>b>>q;
		c=a*b;
		d=max(a,b);
		for(int i=0;i<q;i++){
			ll l,r,count;
			cin>>l>>r;
			// if(r<d){
			// 	cout<<0<<" ";
			// 	continue;
			// }
			count=r-l+1;
			ll start=r+1,end=r;
			for(ll j=l;j<=r;j++){
				if(j%c==0){
					start=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here1"<<endl;
			for(int j=r;j>=start;j--){
				if(j%c==0){
					count--;
					end=j;
					break;
				}
				if(j%c<d){
					count--;
				}
			}
			// cout<<count<<"here2"<<endl;
			ll tmp=(end-start)/c;
			tmp=max(tmp,(ll)0);
			count-=tmp*d;
			cout<<count<<" ";
		}
		cout<<endl;
	}

return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define X 10005
#define inf 0x3f3f3f3f
#define one 0x01
#define PI 3.141592653589793238462643383
const int N=1e4;
int mod=2009;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int n,a[1006],num[1006],c[1006];
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int k=1;k<=n;++k)
    {
        memset(num,0,sizeof(num));
        num[k]=1;
   // for(int i=k;i<=n;++i)
   int tag=0;
   int i=k;
   while(!tag)
    {

        num[a[i]]++;
        if(num[a[i]]>=2)
           {
               c[k]=a[i];
               tag=1;
           }
           i=a[i];
    }
    }
    cout<<a[1];
    for(int i=2;i<=n;++i)
        cout<<' '<<c[i];
    cout<<endl;
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;

int p[maxn],cnt[maxn],ans;

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&p[i]);
	for(int i=1;i<=n;i++){
		ans=0;
		memset(cnt,0,sizeof cnt);
		for(int j=i;;j++){
			if(j>n) j=i;
			cnt[p[j]]++;
			if(cnt[p[j]]>=2){
				ans=p[j];
				break;
			}
		}
		printf("%d%c",ans,i==n?'\n':' ');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;

int p[maxn],cnt[maxn],ans;

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&p[i]);
	}
	for(int i=1;i<=n;i++){
		memset(cnt,0,sizeof cnt);
		ans=0;
		cnt[i]++;
		int j=i;
		for(;;){
			cnt[p[j]]++;
			if(cnt[p[j]]>=2){
				ans=p[j];
				break;
			}
			j=p[j];
		}
		printf("%d%c",ans,i==n?'\n':' ');
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int n,ans;
int a[1003];
int f[1003];
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-f;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void dfs(int k)
{
    if(f[k]==2) {
        ans=k;
        return ;
    }
    f[k]++;
    dfs(a[k]);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++)
    {
        dfs(i);
        cout<<ans<<" ";
    }
}


#include<iostream>
#include<cstdio>
using namespace std;
int n,ans;
int a[1003];
int f[1003];
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-f;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void dfs(int k)
{
    if(f[k]==2) {
        ans=k;
        return ;
    }
    f[k]++;
    dfs(a[k]);
    f[k]--;
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++)
    {
        dfs(i);
        cout<<ans<<" ";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e4+5;
inline ll read()
{ ll x=0,f=1;
 char ch=getchar();
 while(ch<'0'||ch>'9')
 { if(ch=='-')f=-1;ch=getchar(); }
 while(ch>='0'&&ch<='9')
 { x=x*10+ch-'0';ch=getchar(); }
 return x*f;
}
inline void write(ll x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
ll gcd(ll x,ll y)
{
    if(y==0)return x;
    return gcd(y,x%y);
}
ll n,a[maxn],b[maxn],p;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		p=i;
		memset(b,0,sizeof(b));
		b[p]++;
		while(b[a[p]]==0)
		{
			b[a[p]]++;
			p=a[p];
		}
		cout<<a[p]<<" ";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,h,a,b,t,x,y,xx,yy,sum;
int main()
{
    cin>>n>>h>>a>>b>>t;
    while(t--)
    {
        sum=0;
        cin>>x>>y>>xx>>yy;
        if(x==xx)cout<<abs(y-yy)<<endl;
        else
        {
            if(y<a||y>b)
            {
                if(y<a)sum+=a-y,y=a;
                else if(y>b)sum+=y-b,y=b;
            }
            sum+=abs(x-xx),x=xx;
            sum+=abs(y-yy);
            printf("%lld\n",sum);
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
typedef long long ll;
ll n,h,a,b,t,x,y,xx,yy,sum;
int main()
{
    cin>>n>>h>>a>>b>>t;
    while(t--)
    {
        sum=0;
        cin>>x>>y>>xx>>yy;
        if(x==xx)cout<<abs(y-yy)<<endl;
        else
        {
            if(y<a||y>b)
            {
                if(y<a)sum+=a-y,y=a;
                else if(y>b)sum+=y-b,y=b;
            }
            sum+=abs(x-xx),x=xx;
            sum+=abs(y-yy);
            cout<<sum<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e4+5;
inline ll read()
{ ll x=0,f=1;
 char ch=getchar();
 while(ch<'0'||ch>'9')
 { if(ch=='-')f=-1;ch=getchar(); }
 while(ch>='0'&&ch<='9')
 { x=x*10+ch-'0';ch=getchar(); }
 return x*f;
}
inline void write(ll x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
ll n,a[maxn],b[maxn],p;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		p=i;
		memset(b,0,sizeof(b));
		b[p]++;
		while(b[a[p]]==0)
		{
			b[a[p]]++;
			p=a[p];
		}
		cout<<a[p]<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[1003], p[1003], tim[1003];

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", &p[i]);
	for(int i = 1; i <= n; ++i) {
		memset(tim, 0, sizeof tim);
		int j = i;
		for(;!tim[j]; j = p[j])
			tim[j] = 1;
		cout<<j<<" ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct node{
	int nxt, to;
};

node E[1003];

int head[1003], cnt = 1, deg[1003], p[1003], n, ans[1003], degree[1003];

bool on_circ[1003];

inline void add_edge(int fr, int to) {
	E[cnt].to = to; E[cnt].nxt = head[fr]; head[fr] = cnt++;
	degree[to] += 1;
}

void findcirc() {
	queue<int> q;
	for(int i = 1; i <= n; ++i) if(!degree[i]) q.push(i);
	while(!q.empty()) {
		int cur = q.front(); q.pop();
		for(int p = head[cur]; p; p = E[p].nxt) {
			int nx = E[p].to;
			degree[nx]--;
			if(!degree[nx]) q.push(nx);
		}
	}
	for(int i = 1; i <= n; ++i) if(degree[i]) on_circ[i] = 1;
}

int dfs(int x) {
	if(ans[x]) return ans[x];
	for(int i = head[x]; i; i = E[i].nxt) {
		ans[x] = dfs(E[i].to);
	}
	return ans[x];
}

void getans() {
	for(int i = 1; i <= n; ++i)
		if(on_circ[i]) ans[i] = i;
	for(int i = 1; i <= n; ++i) {
		if(!ans[i]) dfs(i);
	}
}

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", &p[i]);
	for(int i = 1; i <= n; ++i) add_edge(i, p[i]);
	//for(int i = head[1]; i; i = E[i].nxt) cout<<E[i].to<<" ";
	findcirc();
	getans();
	for(int i = 1; i <= n; ++i) printf("%d ", ans[i]);
}


#include <bits/stdc++.h>

using namespace std;

struct node{
	int nxt, to;
};
//qaq
node E[1003];

int head[1003], cnt = 1, deg[1003], p[1003], n, ans[1003], degree[1003];

bool on_circ[1003];

inline void add_edge(int fr, int to) {
	E[cnt].to = to; E[cnt].nxt = head[fr]; head[fr] = cnt++;
	degree[to] += 1;
}

void findcirc() {
	queue<int> q;
	for(int i = 1; i <= n; ++i) if(!degree[i]) q.push(i);
	while(!q.empty()) {
		int cur = q.front(); q.pop();
		for(int p = head[cur]; p; p = E[p].nxt) {
			int nx = E[p].to;
			degree[nx]--;
			if(!degree[nx]) q.push(nx);
		}
	}
	for(int i = 1; i <= n; ++i) if(degree[i]) on_circ[i] = 1;
}

int dfs(int x) {
	if(ans[x]) return ans[x];
	for(int i = head[x]; i; i = E[i].nxt) {
		ans[x] = dfs(E[i].to);
	}
	return ans[x];
}

void getans() {
	for(int i = 1; i <= n; ++i)
		if(on_circ[i]) ans[i] = i;
	for(int i = 1; i <= n; ++i) {
		if(!ans[i]) dfs(i);
	}
}

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", &p[i]);
	for(int i = 1; i <= n; ++i) add_edge(i, p[i]);
	//for(int i = head[1]; i; i = E[i].nxt) cout<<E[i].to<<" ";
	findcirc();
	getans();
	for(int i = 1; i <= n; ++i) printf("%d ", ans[i]);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <map>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector <int> v(n + 1);

    for (int i = 1;i <=n; i++)
        cin >> v[i];
    map <int, int> M;
    for(int i = 1;i <= n; i++)
    {
        M[i] = 1;
        int t = v[i];
        while(M[t] != 1)
        {
            M[t] = 1;
            t = v[t];
        }
        M.clear();
        cout << t << " ";
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, t = 1;
    cin >> n;
    vector <int> v1(n + 1);
    vector <int> v2(n + 1);
    vector <int> v3(n + 1);
    for(int i = 1; i <= n; i++)
    {
        cin >> v1[i];
    }
    for(int i = 1; i <= n; i++)
    {
        v3[i] = i;
    }
    v2 = v3;

    for(int i = 1; i <= n; i++)
    {
        v2[i] = 0;
        t = i;
        while(v2[v1[t]] != 0)
        {
            t = v1[t];
            v2[t] = 0;
        }
        cout << v1[t] << " ";
        v2 = v3;

    }

    return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;
const int maxn = 1010;

int n;
int next[maxn];
int num[maxn];
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &next[i]);
        }
        if(n == 1) {
            printf("1\n");
            continue;
        }
        for(int i = 1; i <= n; i++)
        {
            memset(num, 0, sizeof(num));
            int cur = i;
            while(true) {
                num[cur]++;
                if(num[cur] == 2) break;
                cur = next[cur];
            }
            if(i > 1) printf(" ");
            printf("%d", cur);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;
const int maxn = 1010;

int n;
int next[maxn];
int num[maxn];
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &next[i]);
        }
        if(n == 1) {
            printf("1\n");
            continue;
        }
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
        {
            int cur = i;
            while(num[cur] < 2) {
                num[cur]++;
                if(num[cur] == 2) break;
                cur = next[cur];
            }
            if(i > 1) printf(" ");
            printf("%d", cur);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;
const int maxn = 1010;

int n;
int next[maxn];
int num[maxn];
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &next[i]);
        }
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
        {
            int cur = i;
            while(num[cur] < 2) {
                num[cur]++;
                if(num[cur] == 2) break;
                cur = next[cur];
            }
            if(i > 1) printf(" ");
            printf("%d", cur);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
	int n;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	for(int i=0;i<n;i++)
	{
		int check[n]={0};
		check[i]++;
		for(int j=0;j<n;j++)
		{
			check[arr[j]-1]++;
			if(check[arr[j]-1]==2)
			{
				cout<<arr[j]<<" ";
				break;
			}
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int  j,t;
	int a[10000],b[10000];
	cin>>t;
	for(int i=1;i<=t;i++)
	cin>>a[i];

for(int i=1;i<=t;i++)
{
	for( j=1;j<=t;j++)
	b[j]=0;

	for(j=i;b[j]<1;b[j]++,j=a[j]);
	cout<<j<<" ";
}
return 0;
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
	int n,j;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	for(int i=0;i<n;i++)
	{
		int check[n+1]={0};
		check[i+1]++;
		for(int j=0;j<n;j++)
		{
			check[arr[j]]++;
			if(check[arr[j]]>1)
			{
				cout<<arr[j]<<" ";
				break;
			}
		}
	}
}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.Math;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		int n,k;
		n=in.nextInt();
		k=in.nextInt();
		double[] arrx=new double[n];
		double[] arry=new double[n];
		for(int i=0;i<n;i++)
		{
			arrx[i]=in.nextDouble();
			arry[i]=in.nextDouble();
		}
		double sum=0,m;
		int z=0;
		while(z<n-1)
		{
			m=Math.sqrt(Math.pow(arrx[z]-arrx[z+1],2)+Math.pow(arry[z]-arry[z+1],2));
			sum=sum+m*0.02;
			z=z+1;
		}
		sum*=k;
		String str=String.format("%.9f",sum);
		System.out.println(str);
	}
}


#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
	int n;
	cin>>n;
	int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	int check[n];
	for(int i=0;i<n;i++)
	{
		for(int k=0;k<n;k++)
			check[k]=0;
		check[i]++;
		for(int j=0;j<n;j++)
		{
			check[arr[j]-1]++;
			if(check[arr[j]-1]>1)
			{
				cout<<arr[j]<<" ";
				break;
			}
		}
	}
}


        /*
        There is simply no substitute for hard work when it comes to
        achieving success.(Heather)
        Writer: Jaideep Bhargava
        Date:11/08/2018
        */
        #include <bits/stdc++.h>
        #define ll long long
        #define sz(a) int((a).size())
        #define fr(i,n) for(int i=0;i<n;i++)
        #define frr(i,n) for(int i=1;i<n;i++)
        #define rf(i,n) for(int i=1;i<=n;i++)
        #define pb push_back
        #define all(c) (c).begin(),(c).end()
        #define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
        #define present(c,x) ((c).find(x) != (c).end())
        #define cpresent(c,x) (find(all(c),x) != (c).end())
        #define MAX INT_MAX
        using namespace std;
        int main()
        {
            int n;
            cin>>n;
            int a[n];
            std::vector<int> v;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
            }
            if(n==1)
                {
                    cout<<a[1]<<endl;
                    return 0;
                }
            for(int i=1;i<=n;i++)
            {
                int ar[n+1];
                frr(k,n+1)
                ar[k]=0;
                ar[i]++;
                for(int j=1;j<=n;j++)
                    {
                        ar[a[j]]++;
                        if(ar[a[j]]==2)
                           {
                                v.pb(a[j]);
                                break;
                            }
                    }
            }
            fr(i,n)
            cout<<v[i]<<" ";
            cout<<endl;
            return 0;
        }


    #include <bits/stdc++.h>
    #define ll long long
    #define sz(a) int((a).size())
    #define fr(i,n) for(int i=0;i<n;i++)
    #define rf(i,n) for(int i=1;i<=n;i++)
    #define jdb(i,v) for(__typeof(v.begin())i=v.begin(); i!=v.end();i++)
    #define present(c,a) (find(all(c),a) != c.end())
    #define spresent(c,a) (c.find(a)!=c.end())
    #define no(s,c) s.count(c)
    #define pb push_back
    #define eb emplace_back
    #define mp make_pair
    #define all(c) (c).begin(),(c).end()
    #define MAX INT_MAX
    using namespace std;
    #define f first
    #define s second
    int main()
    {
        int v;
        cin>>v;
        vector<vector<int> > g(v+1);
        rf(i,v)
        {
            int b;
            cin>>b;
            g[i].eb(b);
        }
        rf(i,v)
        {
            int visited[v+1];
            memset(visited,0,sizeof(visited));
            stack<int>s;
            s.push(i);
            while(!s.empty())
            {
                int top = s.top();
                s.pop();
                if(visited[top]==0)
                {
                    visited[top]=1;
                    jdb(i,g[top])
                    s.push(*i);
                }
                else
                    {
                        cout<<top<<" ";
                        break;
                    }
            }
        }
        return 0;
    }


        /*
        There is simply no substitute for hard work when it comes to
        achieving success.(Heather)
        Writer: Jaideep Bhargava
        Date:11/08/2018
        */
        #include <bits/stdc++.h>
        #define ll long long
        #define sz(a) int((a).size())
        #define fr(i,n) for(int i=0;i<n;i++)
        #define frr(i,n) for(int i=1;i<n;i++)
        #define rf(i,n) for(int i=1;i<=n;i++)
        #define pb push_back
        #define all(c) (c).begin(),(c).end()
        #define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
        #define present(c,x) ((c).find(x) != (c).end())
        #define cpresent(c,x) (find(all(c),x) != (c).end())
        #define MAX INT_MAX
        using namespace std;
        int main()
        {
            int n;
            cin>>n;
            int a[n];
            std::vector<int> v;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
            }
            if(n==1)
                {
                    cout<<1<<endl;
                    return 0;
                }
            for(int i=1;i<=n;i++)
            {
                int ar[n+1];
                frr(k,n+1)
                ar[k]=0;
                ar[i]++;
                for(int j=1;j<=n;j++)
                    {
                        ar[a[j]]++;
                        if(ar[a[j]]==2)
                           {
                                v.pb(a[j]);
                                break;
                            }
                    }
            }
            fr(i,n)
            cout<<v[i]<<" ";
            cout<<endl;
            return 0;
        }


        /*
        There is simply no substitute for hard work when it comes to
        achieving success.(Heather)
        Writer: Jaideep Bhargava
        Date:11/08/2018
        */
        #include <bits/stdc++.h>
        #define ll long long
        #define sz(a) int((a).size())
        #define fr(i,n) for(int i=0;i<n;i++)
        #define frr(i,n) for(int i=1;i<n;i++)
        #define rf(i,n) for(int i=1;i<=n;i++)
        #define pb push_back
        #define all(c) (c).begin(),(c).end()
        #define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
        #define present(c,x) ((c).find(x) != (c).end())
        #define cpresent(c,x) (find(all(c),x) != (c).end())
        #define MAX INT_MAX
        using namespace std;
        int main()
        {
            int n;
            cin>>n;
            int a[n];
            std::vector<int> v;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
            }
            for(int i=1;i<=n;i++)
            {
                int ar[n+1];
                frr(k,n+1)
                ar[k]=0;
                ar[i]++;
                for(int j=1;j<=n;j++)
                    {
                        ar[a[j]]++;
                        if(ar[a[j]]==2)
                           {
                                v.pb(a[j]);
                                break;
                            }
                    }
            }
            fr(i,n)
            cout<<v[i]<<" ";
            cout<<endl;
            return 0;
        }


        /*
        There is simply no substitute for hard work when it comes to
        achieving success.(Heather)
        Writer: Jaideep Bhargava
        Date:11/08/2018
        */
        #include <bits/stdc++.h>
        #define ll long long
        #define sz(a) int((a).size())
        #define fr(i,n) for(int i=0;i<n;i++)
        #define frr(i,n) for(int i=1;i<n;i++)
        #define rf(i,n) for(int i=1;i<=n;i++)
        #define pb push_back
        #define all(c) (c).begin(),(c).end()
        #define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
        #define present(c,x) ((c).find(x) != (c).end())
        #define cpresent(c,x) (find(all(c),x) != (c).end())
        #define MAX INT_MAX
        using namespace std;
        int main()
        {
            int n;
            cin>>n;
            int a[n];
            std::vector<int> v;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i];
            }
            for(int i=0;i<=n;i++)
            {
                int ar[n+1];
                fr(k,n+1)
                ar[k]=0;
                ar[i+1]++;
                for(int j=1;j<=n;j++)
                    {
                        ar[a[j]]++;
                        if(ar[a[j]]==2)
                           {
                                v.pb(a[j]);
                                break;
                            }
                    }
            }
            fr(i,n)
            cout<<v[i]<<" ";
            cout<<endl;
            return 0;
        }


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> st(n + 1);
    for (int i = 1; i < n+1; ++i)
        cin >> st[i];

    for(int i = 1; i < n+1; ++i)
    {
        vector<bool> blank(n + 1, false);
        blank[i] = true;
        for(int j =  1; j < n +1; ++j)
        {
            if(!blank[st[j]])
                blank[st[j]] = true;
            else
            {
                cout << st[j] << " ";
                break;
            }
        }
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> st(n + 1);
    for (int i = 1; i < n+1; ++i)
        cin >> st[i];

    for(int i = 1; i < n+1; ++i)
    {
        vector<bool> blank(n + 1, false);
        blank[i] = true;
        for(int j =  1; j < n +1; ++j)
        {
            if(!blank[st[j]])
                blank[st[j]] = true;
            else
            {
                cout << st[j] << " ";
                break;
            }
        }
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> st(n + 1);
    for (int i = 1; i < n+1; ++i)
        cin >> st[i];

    for(int i = 1; i < n+1; ++i)
    {
        vector<bool> blank(n + 1, false);
        blank[i] = true;
        for(int j =  1; j < n +1; ++j)
        {
            if(blank[st[j]])
            {
                cout << st[j] << " ";
                break;
            }
            blank[st[j]] = true;
        }
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> st(n + 1);
    for (int i = 1; i < n+1; ++i)
        cin >> st[i];

    for(int i = 1; i < n+1; ++i)
    {
        vector<bool> blank(n + 1, false);
        int cur = i;
        while(!blank[cur])
        {
            blank[cur] = true;
            cur = st[cur];
        }
        cout << cur << " ";
    }
    cout<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    vector<int> st(n + 1);
    for (int i = 1; i < n+1; ++i)
        cin >> st[i];

    for(int i = 1; i < n+1; ++i)
    {
        vector<bool> blank(n + 1, false);
        blank[i] = true;
        int j;
        for(j = 1; !blank[st[j]]; ++j)
           blank[st[j]] = true;

        cout << st[j] << " ";
    }
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool tvisited[1001] = {false};
int adj[1001];
int dfs(int a){
	tvisited[a] = true;
	if(tvisited[adj[a]] == false){
		return dfs(adj[a]);
	}
	return adj[a];
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    {
      cin >> adj[i];
    }
  for (int i = 1; i <= n; i++)
    {
      cout << dfs(adj[i]) << " ";
      tvisited[1001] = {0};
    }
	return 0;
}


#include <iostream>
using namespace std;
int visited[1001] = {false};
int adj[1001];
int main() {
	int n;
	cin >> n;
	for(int i = 1 ;  i <= n ; i++){
		cin >> adj[i];
	}
	for(int i = 1 ; i <= n ; i++){
	    int node = i;
        while(visited[node] ==  false){
        	visited[node] = true;
        	node = adj[node];
        }
        cout << node << " ";
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1001] = { false };

int adj[1001];
int
dfs (int a)
{
  visited[a] = true;
  if (visited[adj[a]] == true)
    {
      return adj[a];
    }
  else
    {
      return dfs (adj[a]);
    }
}

int
main ()
{
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    {
      cin >> adj[i];
    }
  int ans;
  for (int i = 1; i <= n; i++)
    {
      cout << dfs(i) << " ";
      visited[1001] ={ false };
    }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool visited[1001] = { false };

int adj[1001];
int
dfs (int a)
{
  visited[a] = true;
  if (visited[adj[a]] == true)
    {
      return adj[a];
    }
  else
    {
      return dfs (adj[a]);
    }
}

int
main ()
{
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    {
      cin >> adj[i];
    }
  int ans;
  for (int i = 1; i <= n; i++)
    {
      cout << dfs(i) << " ";
      for(int j = 1 ; j <= n ; j++){
      	visited[j] = false;
      }
    }
  return 0;
}


#include<iostream>
using namespace std;
typedef long long int ll;

ll vis[100005]={0},check[100005]={0};
ll a[100005]={0},ans[100005] = {0};

ll final(ll node)
{

    if(ans[node]== 0){
        if(ans[a[node]]== 0){
        ans[node]=final(a[node]);
        return ans[node];
        }
        else{
        ans[node]=ans[a[node]];
        return ans[node];
        }
    }
    else{
         return ans[node];
    }

}

void clycle(ll p,ll n)
{
    ans[p]=p;
    ll op=a[p];
    while(op!=p){
    ans[op]=op;
    op=a[op];
    }
}
void dfs(ll node,ll n)
{
    if(vis[node]==0)
    {
        vis[node]=1;
        check[node]=1;
        if(vis[a[node]]*check[a[node]]==1)
        {
            clycle(a[node],n);
            return;
        }
        else{
        dfs(a[node],n);
        check[node]=0;
        }
    }
}

int main()
{
   /* for (ll i = 1; i < 100005; i++)
        {
            ans[i]=-1;
        }
    ll T,*/
    ll n;
    /*cin >> T;
    while (T--)
    {*/

        cin >>n;

        for (ll i = 1; i <= n; i++)
        {
            cin >> a[i];
            if(a[i]==i)
            ans[i]=i;
        }
        //ll d=1;

for(ll i=1;i<n+1;i++)
{
    if(vis[i]==0)
    dfs(i,n);

}

for (ll i = 1; i < n+1; i++)
    {
    ans[i]=final(i);
    }

        for (int i = 1; i <= n; i++)
        {
            cout << ans[i]<<" ";
        }
        cout<<endl;

      for (ll i = 1; i < n+1; i++)
        {
           vis[i]=0;
           check[i]=0;
            a[i]=0;
            ans[i]=-1;

        }

//   }
    return 0;
}


#include<bits/stdc++.h>
#include<sstream>
using namespace std;
typedef long long int ll;

ll a[1005],b[1005],ans[1005],on[1005]={0},vis[1005]={0},p;

ll op(ll node)
{
    if(ans[a[node]]==-1)
    op(a[node]);
    else return ans[a[node]];
}

void cycle(ll node)
{
    ans[node]=node;
    p=a[node];
    while(ans[p]==-1)
    {
        ans[p]=p;
        p=a[p];
    }
        return ;
}

void dfs(ll node)
{
    if(vis[node]*on[node]==1){
    cycle(node);
    return ;
    }
    else
    {
        vis[node]=1;
        on[node]=1;
        dfs(a[node]);
        on[node]=0;
    }
}

int main()
{
    ll n;
    cin>>n;
    for (int i = 1; i <=n; i++)
    {
        cin>>a[i];
        b[a[i]]=i;
        on[i]=0;
        vis[i]=0;
        ans[i]=-1;
    }

    for (int i = 1; i <= n; i++)
    {
        if(vis[i]==0)
        dfs(i);
    }

    for (int i = 1; i <= n; i++)
    {
        if(ans[i]==-1)
        ans[i]=op(i);
    }

    for (int i = 1; i <= n; i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


#include<iostream>
using namespace std;
typedef long long int ll;

ll vis[100005]={0},check[100005]={0};
ll a[100005]={0},ans[100005];

ll final(ll node)
{

    if(ans[node]==-1){
        if(ans[a[node]]==-1){
        ans[node]=final(a[node]);
        return ans[node];
        }
        else{
        ans[node]=ans[a[node]];
        return ans[node];
        }
    }
    else{
         return ans[node];
    }

}

void clycle(ll p,ll n)
{
    ans[p]=p;
    ll op=a[p];
    while(op!=p){
        //d++;
    ans[op]=op;
    // cout << op<<" "; //members in cycle
    op=a[op];
    }
    // yup(p);

}

void dfs(ll node,ll n)
{
    if(vis[node]==0)
    {
        vis[node]=1;
        check[node]=1;
        if(vis[a[node]]*check[a[node]]==1 /*&& flag==0*/)
        {
            clycle(a[node],n);
            // cout<<a[node]<<endl;
            return;
        }
        /*else if (flag==1)
            return;
        */
        else/* if(flag==0)*/{
        dfs(a[node],n);
        check[node]=0;
        }
    }
}

int main()
{
    for (ll i = 1; i < 100005; i++)
        {
            ans[i]=-1;
        }
    ll T,n;
    cin >> T;
    while (T--)
    {

        cin >>n;

        for (ll i = 1; i <= n; i++)
        {
            cin >> a[i];
            if(a[i]==i)
            ans[i]=i;
        }
        //ll d=1;

for(ll i=1;i<n+1;i++)
{
    if(vis[i]==0)
    dfs(i,n);

}

for (ll i = 1; i < n+1; i++)
    {
    ans[i]=final(i);
    }

        for (int i = 1; i <= n; i++)
        {
            cout << ans[i]<<" ";
        }
        cout<<endl;

      for (ll i = 1; i < n+1; i++)
        {
           vis[i]=0;
           check[i]=0;
            a[i]=0;
            ans[i]=-1;

        }

  }
    return 0;
}


#include<iostream>
using namespace std;
typedef long long int ll;

ll vis[100005]={0},check[100005]={0};
ll a[100005]={0},ans[100005];

ll final(ll node)
{

    if(ans[node]==-1){
        if(ans[a[node]]==-1){
        ans[node]=final(a[node]);
        return ans[node];
        }
        else{
        ans[node]=ans[a[node]];
        return ans[node];
        }
    }
    else{
         return ans[node];
    }

}

void clycle(ll p,ll n)
{
    ans[p]=p;
    ll op=a[p];
    while(op!=p){
    ans[op]=op;
    op=a[op];
    }
}
void dfs(ll node,ll n)
{
    if(vis[node]==0)
    {
        vis[node]=1;
        check[node]=1;
        if(vis[a[node]]*check[a[node]]==1)
        {
            clycle(a[node],n);
            return;
        }
        else{
        dfs(a[node],n);
        check[node]=0;
        }
    }
}

int main()
{
   /* for (ll i = 1; i < 100005; i++)
        {
            ans[i]=-1;
        }
    ll T,*/
    ll n;
    /*cin >> T;
    while (T--)
    {*/

        cin >>n;
         for (ll i = 1; i < n+1; i++)
        {
            ans[i]=-1;
        }

        for (ll i = 1; i <= n; i++)
        {
            cin >> a[i];
            if(a[i]==i)
            ans[i]=i;
        }
for(ll i=1;i<n+1;i++)
{
    if(vis[i]==0)
    dfs(i,n);

}
for (ll i = 1; i < n+1; i++)
    {
    ans[i]=final(i);
    }

        for (int i = 1; i <= n; i++)
        {
            cout << ans[i]<<" ";
        }
        cout<<endl;

    return 0;
}


#include<iostream>
using namespace std;
typedef long long int ll;

ll vis[100005]={0},check[100005]={0};
ll a[100005]={0},ans[100005];

ll final(ll node)
{

    if(ans[node]== 0){
        if(ans[a[node]]== 0){
        ans[node]=final(a[node]);
        return ans[node];
        }
        else{
        ans[node]=ans[a[node]];
        return ans[node];
        }
    }
    else{
         return ans[node];
    }

}

void clycle(ll p,ll n)
{
    ans[p]=p;
    ll op=a[p];
    while(op!=p){
    ans[op]=op;
    op=a[op];
    }
}
void dfs(ll node,ll n)
{
    if(vis[node]==0)
    {
        vis[node]=1;
        check[node]=1;
        if(vis[a[node]]*check[a[node]]==1)
        {
            clycle(a[node],n);
            return;
        }
        else{
        dfs(a[node],n);
        check[node]=0;
        }
    }
}

int main()
{
   /* for (ll i = 1; i < 100005; i++)
        {
            ans[i]=-1;
        }
    ll T,*/
    ll n;
    /*cin >> T;
    while (T--)
    {*/

        cin >>n;

        for (ll i = 1; i <= n; i++)
        {
            cin >> a[i];
            if(a[i]==i)
            ans[i]=i;
        }
        //ll d=1;

for(ll i=1;i<n+1;i++)
{
    if(vis[i]==0)
    dfs(i,n+1);

}

for (ll i = 1; i < n+1; i++)
    {
    ans[i]=final(i);
    }

        for (int i = 1; i <= n; i++)
        {
            cout << ans[i]<<" ";
        }
        cout<<endl;

      for (ll i = 1; i < n+1; i++)
        {
           vis[i]=0;
           check[i]=0;
            a[i]=0;
            ans[i]=-1;

        }

//   }
    return 0;
}


#include<bits/stdc++.h>
#include<sstream>
using namespace std;
typedef long long int ll;

ll a[1005],b[1005],ans[1005],on[1005]={0},vis[1005]={0};

ll op(ll node)
{
    if(ans[a[node]]==-1)
    op(a[node]);
    else return ans[a[node]];
}

void cycle(ll node)
{
    ans[node]=node;
    ll p=node;
    while(a[node]==-1)
    {
        p=a[p];
        ans[p]=p;
    }
     p=a[p];
        ans[p]=p;
        return ;
}

void dfs(ll node)
{
    if(vis[node]*on[node]==1){
    cycle(node);
    return ;
    }
    else
    {
        vis[node]=1;
        on[node]=1;
        dfs(a[node]);
        on[node]=0;
    }
}

int main()
{
    ll n;
    cin>>n;
    for (int i = 1; i <=n; i++)
    {
        cin>>a[i];
        b[a[i]]=i;
        on[i]=0;
        vis[i]=0;
        ans[i]=-1;
    }

    for (int i = 1; i <= n; i++)
    {
        if(vis[i]==0)
        dfs(i);
    }

    for (int i = 1; i <= n; i++)
    {
        if(ans[i]==-1)
        ans[i]=op(i);
    }

    for (int i = 1; i <= n; i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    int* p = new int[n+1];
    bool* visited = new bool[n+1];
    vector<int> vec;
    for(int i=1;i<=n;i++){
        cin>>p[i]; visited[i] = false;
    }
    for(int i=1;i<=n;i++){
        visited[i] = true;
        vec.push_back(i);
      for(int j=1;j<=n;j++){
          if(visited[p[j]]){cout<<p[j]<<" "; break;}
          else{visited[p[j]] = true; vec.push_back();}
      }
        for(auto itr = vec.begin()+vec.size()-1;itr>=vec.begin();itr--){
            visited[*itr] = false; vec.pop_back();
        }
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    int* p = new int[n+1];
    bool* visited = new bool[n+1];
    vector<int> vec;
    for(int i=1;i<=n;i++){
        cin>>p[i]; visited[i] = false;
    }
    for(int i=1;i<=n;i++){
        visited[i] = true;
        vec.push_back(i);
      for(int j=1;j<=n;j++){
          if(visited[p[j]]){cout<<p[j]<<" "; break;}
          else{visited[p[j]] = true;}
      }
        for(auto itr = vec.begin()+vec.size()-1;itr>=vec.begin();itr--){
            visited[*itr] = false; vec.pop_back();
        }
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    int* p = new int[n+1];
    bool* visited = new bool[n+1];
    vector<int> vec;
    for(int i=1;i<=n;i++){
        cin>>p[i]; visited[i] = false;
    }
    for(int ii=1;ii<=n;ii++){
        visited[ii] = true;
        vec.push_back(ii);
        int i= ii;
       while(visited[p[i]] == false){
          visited[p[i]] = true; vec.push_back(p[i]); i = p[i];
        }
        cout<<p[i]<<" ";
        for(auto itr = vec.begin()+vec.size()-1;itr>=vec.begin();itr--){
            visited[*itr] = false; vec.pop_back();
        }
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    int* p = new int[n+1];
    bool* visited = new bool[n+1];
    vector<int> vec;
    for(int i=1;i<=n;i++){
        cin>>p[i]; visited[i] = false;
    }
    for(int i=1;i<=n;i++){
        visited[i] = true;
        vec.push_back(i);
      for(int j=1;j<=n;j++){
          if(visited[p[j]]){cout<<p[j]<<" "; break;}
          else{visited[p[j]] = true; vec.push_back(j);}
      }
        for(auto itr = vec.begin()+vec.size()-1;itr>=vec.begin();itr--){
            visited[*itr] = false; vec.pop_back();
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
map<int,int> vis;
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;

int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int x;
    for(int i=1;i<=n;i++)
    {

        x=i;
        while(1)
        {
            if(vis[x]>1)
            {
                cout<<x<<" ";
                break;
            }
            vis[x]++;
            x=a[x];
        }

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
//map<int,int> vis;
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;

int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int x;
    vector<int> vis(n);
    for(int i=1;i<=n;i++)
    {

        x=i;
        while(1)
        {
            if(vis[x]>1)
            {
                cout<<x<<" ";
                vis.clear();
                //vis.resize(n);
                break;
            }
            vis[x]++;
            x=a[x];
        }

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
//map<int,int> vis;
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;

int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int x;
    for(int i=1;i<=n;i++)
    {
        map<int,int> vis;

        x=i;
        while(1)
        {
            if(vis[x]>1)
            {
                cout<<x<<" ";
                break;
            }
            vis[x]++;
            x=a[x];
        }

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
int vis[1001];
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;
void dfs(int node)
{
    vis[node]+=1;

    if(vis[node]>=2)
    {
        pos=node;
        return;
    }
    for(int child:adj[node])
    {

           pos=child;
           dfs(child);
    }
    return;
}
int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        adj[i].push_back(a[i]);
    }
    for(int i=1;i<=n;i++)
    {

        dfs(i);
        cout<<pos<<" ";

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
int vis[1001];
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;

int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int x;
    for(int i=1;i<=n;i++)
    {

        x=i;
        while(1)
        {
            if(vis[x]>1)
            {
                cout<<x<<" ";
                break;
            }
            vis[x]++;
            x=a[x];
        }

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
//map<int,int> vis;
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;

int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int x;
    vector<int> vis(n);
    for(int i=1;i<=n;i++)
    {

        x=i;
        while(1)
        {
            if(vis[x]>1)
            {
                cout<<x<<" ";
                //vis.clear();
                //vis.resize(n);
                break;
            }
            vis[x]++;
            x=a[x];
        }

    }

	return 0;
}


#include <bits/stdc++.h>
#define vi vector<int>
using namespace std;
int vis[1001];
vi adj[1001];
//vector<int> dis(1001);
int a[1001];
int pos;
void dfs(int node)
{
    vis[node]+=1;

    if(vis[node]>=2)
    {
        pos=node;
        return;
    }
    for(int child:adj[node])
    {
           dfs(child);
    }

}
int main() {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        adj[i].push_back(a[i]);
    }
    for(int i=1;i<=n;i++)
    {

        dfs(i);
        cout<<pos<<" ";

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	ll n,x,j,i=1;
	cin>>n;
	ll a[n+1];
	while(i<=n)
	{
		cin>>x;
		a[i]=x;
		i++;
	}
	ll v[n+1];
	i=1;
	while(i<=n)
	{

		memset(v,0,n+1);
		j=i;
		while(v[j]==0)
		{
			v[j]++;
			j=a[j];
		}
		cout<<j<<" ";
		i++;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	ll n,x,j,i=1;
	cin>>n;
	ll a[n+1];
	while(i<=n)
	{
		cin>>x;
		a[i]=x;
		i++;
	}
	i=1;
	while(i<=n)
	{
		ll v[n+1]={};
		j=i;
		while(v[j]==0)
		{
			v[j]++;
			j=a[j];
		}
		cout<<j<<" ";
		i++;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	ll n,x,j,i=1;
	cin>>n;
	ll a[n+1];
	while(i<=n)
	{
		cin>>x;
		a[i]=x;
		i++;
	}
	ll v[n+1]={};
	i=1;
	while(i<=n)
	{
		j=i;
		while(v[j]==0)
		{
			v[j]++;
			j=a[j];
		}
		cout<<j<<" ";
		i++;
		memset(v,0,n+1);
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	ll n,x,j,i=1;
	cin>>n;
	ll a[n+1];
	while(i<=n)
	{
		cin>>x;
		a[i]=x;
		i++;
	}
	i=1;
	while(i<=n)
	{
		ll v[n+1]={};
		j=i;
		while(v[j]==0)
		{
			v[j]++;
			j=a[j];
		}
		i++;
		cout<<j<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1000];
map<int,bool> vis;
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            vis.clear();
            dfs(i);
            cout<<dest<<" ";
        }
        cout<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1000];
map<int,bool> vis;
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin>>n){
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            vis.clear();
            dfs(i);
            cout<<dest<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> arr[1010];
map<int,bool> vis;
int dest;
void DFS(int n)
{
    dest=n;
    if (!vis[n])
    {
        vis[n]=true;
        for (int i=0; i < arr[n].size(); i++)
            DFS(arr[n][i]);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,x;
    cin >> n;
    for (int i=1; i <= n; i++)
    {
        cin >> x;
        arr[i].push_back(x);
    }
    for (int i=1; i <= n; i++)
    {
        vis.clear();
        DFS(i);
        cout << dest << " ";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1005];
map<int,bool> vis;
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            vis.clear();
            dfs(i);
            cout<<dest<<" ";
        }
        cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1000];
map<int,bool> vis;
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
    else cout<<dest<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin>>n){
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            vis.clear();
            dfs(i);
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1000];
map<int,bool> vis;
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
    else cout<<dest<<" ";
}
int main(){
    while(cin>>n){
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        {
            vis.clear();
            dfs(i);
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
vector<int > a[1000];
bool vis[1000];
int dest;
void dfs(int n){
    dest=n;
    if(!vis[n]){
        vis[n]=true;
        dfs(a[n][0]);
    }
    else cout<<dest<<" ";
}
int main(){
    while(cin>>n){
        int x;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            a[i].push_back(x);
        }
        for(int i=1;i<=n;i++)
        dfs(i);
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int min(long long int a, long long int b)
{
	if(a>b)
	{
		return b;

	}
	else
	{
		return a;
	}
}
int main() {
	// your code goes here
	int n;
	cin>>n;

	int p[n];
	int a[n+1];

	for(int i=0;i<n;i++)
	{
		cin>>p[i];

	}
	for(int i=1;i<n+1;i++)
	{
		memset(a, 0, (n+1)*sizeof(a[0]));
		if(i!=p[0])
		{
			a[i]=1;
			for(int j=0;j<n;j++)
			{
				a[p[j]]=a[p[j]]+1;
				if(a[p[j]]>1)
				{
					cout<<p[j]<<" ";
					break;
				}
			}
		}
		else
		{
			int t=0;
			for(int j=0;j<n;j++)
			{
				a[p[j]]=a[p[j]]+1;
				if(a[p[j]]>1)
				{
					cout<<p[j]<<" ";
					t=t+1;
					break;
				}
			}
			if(t==0)
			{
				cout<<p[0]<<" ";
			}

		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int min(long long int a, long long int b)
{
	if(a>b)
	{
		return b;

	}
	else
	{
		return a;
	}
}
int main() {
	// your code goes here
	int n;
	cin>>n;

	int p[n];
	for(int i=0;i<n;i++)
	{
		cin>>p[i];
	}
	int a[n+1];
	for(int i=1;i<n+1;i++)
	{
		memset(a, 0, (n+1)*sizeof(a[0]));
		a[i]=1;
		for(int j=0;j<n;j++)
		{
			a[p[j]]=a[p[j]]+1;
			if(a[p[j]]>1)
			{
				cout<<p[j]<<" ";
				break;
			}

		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define filein()       freopen("in.txt", "r", stdin)
#define fileout()      freopen("out.txt", "w", stdout)

#define FIO()          ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define all(x)         x.begin(), x.end()
#define nfound(v, n)   find(all(v), n)==v.end()
#define found(v, n)    find(all(v), n)!=v.end()
#define UB(v, n)       upper_bound(all(v), n)-v.begin()
#define LB(v, n)       lower_bound(all(v), n)-v.begin()

#define on(n, p)       (n | (1<<(p)))
#define off(n, p)      (n & ~(1<<(p)))
#define check(n, p)    (bool)(n & (1<<(p)))

#define MS(x, v)       memset(&x, v, sizeof(x))
#define CL(x)          memset(&x, 0, sizeof(x))
#define mp             make_pair
#define pb             push_back
#define p              push
#define sz             size
#define cl             clear
#define po             pop
#define em             emplty
#define ss             second
#define fi             first

#define sf             scanf
#define pf             printf
#define pf1(n)         pf("%d", n)
#define sc1(n)         sf("%d", &n)
#define scl1(n)        sf("%I64d", &n)
#define sc2(n, m)      sf("%d %d", &n, &m)
#define scl2(n, m)     sf("%I64d %I64d", &n, &m)
#define sc3(n, m, v)   sf("%d %d %d", &n, &m, &v)
#define scl3(n, m, v)  sf("%I64d %I64d %I64d", &n, &m, &v)
#define nl()           pf("\n")
#define endl           '\n'

#define Max(x, y)      ((x)>(y) ? (x) : (y))
#define Min(x, y)      ((x)<(y) ? (x) : (y))
#define Abs(x)         ((x)<0 ? (-(x)): (x))

#define PI             acos(-1.0)
#define EPS            1e-9

#define MAX            2e18
#define MOD            1000000007

typedef long long lng;
typedef unsigned long long ulng;
typedef pair<int, int> pii;
typedef pair<lng, lng> pll;
typedef vector<int> vi;
typedef vector<lng> vl;

template<class T> inline void IO(T &x){char c=getchar();T sgn=1;while(c<'0' || c>'9'){if (c=='-') sgn=-1;c=getchar();}x=0;while(c>='0' && c<='9'){x=(T)(x<<1)+(x<<3)+c-'0';c=getchar();}x*=sgn;}
template<class T> T POW(T b, T p){T res=1;while(p>0){if (p&1) res*=b;p >>= (1ll), b*=b;}return res;}
template<class T> T bMOD(T a, T p, T m){if (p==0) return (T) 1;if (!(p&1)){T temp=bMOD(a, p/2, m);return ((temp%m)*(temp%m))%m;}return ((a%m)*(bMOD(a, p-1, m)%m))%m;}
template<class T> T inMOD(T a, T m){return bMOD(a, m-2, m);}
template<class T> bool isPrime(T n){for(T i=2; i*i<=n; i++){if (n%i==0) return false;}return true;}
template<class T> string toString(T n){stringstream ss;string num;ss<<n;ss>>num;return num;}
template<class T> T sq(T n){return (n*n);}
template<class T> T gcd(T a, T b){return (b==0) ? a : gcd (b, a%b);}
template<class T> T lcm(T a, T b){return (a/gcd (a, b))*b;}
bool iseq(double a, double b){return fabs(a-b)<EPS;}
template<class T> T toDeg(T x){return (180.0*x)/((T)PI);}
template<class T> T toReg(T x){return (x*(T)PI)/(180.0);}
template<class T> double _distance (T x1, T y1, T x2, T y2){return 1.0*sqrt(sq(x1-x2)+sq(y1-y2));}

int kx[]={-2, -2, +2, +2, +1, -1, +1, -1};
int ky[]={+1, -1, +1, -1, -2, -2, +2, +2};

int dx[]={+0, +0, -1, +1, +1, +1, -1, -1};
int dy[]={+1, -1, +0, +0, -1, +1, +1, -1};

int x[]={+0, +0, +1, -1};
int y[]={+1, -1, +0, +0};

/*

    @author: Badhan Sen
    CSE-14, JUST.
    mail: galaxybd9@gmail.com
*/

int main ()
{
    int n;
    cin >> n;
    int a[1001];
    for(int i=1; i<=n; i++){
        cin >> a[i];
    }
    for(int i=1; i<=n; i++){
        bool vis[1001];
        memset(vis, false, sizeof vis);
        vis[i] = true;
        int p = a[i];
        while(vis[p] == false){
            vis[p] = true;
            p = a[p];
        }
        if(i == n) cout << p << endl;
        else cout << p << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n;
	cin>>n;

	int p[n];
	int a[n+1];
	int t=0;
	int o=0;
	memset(a, 0, (n+1)*sizeof(a[0]));
	for(int i=0;i<n;i++)
	{
		cin>>p[i];
		a[p[i]]=a[p[i]]+1;
		if(a[p[i]]==2 && t==0)
		{
			t=i;
			o=p[i];
		}

	}
	if(t==0)
	{
		for(int i=1;i<n+1;i++)
		{
			cout<<i<<" ";
		}
	}
	else
	{
		for(int i=1;i<n+1;i++)
		{
			int y=0;
			for(int j=0;j<i;j++)
			{
				if(p[j]==i & i!=p[0])
				{
					cout<<p[j]<<" ";
					y++;
					break;
				}
			}
			if(y==0)
			{
				cout<<o<<" ";
			}

		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int min(long long int a, long long int b)
{
	if(a>b)
	{
		return b;

	}
	else
	{
		return a;
	}
}
int main() {
	// your code goes here
	int n;
	cin>>n;

	int p[n];
	for(int i=0;i<n;i++)
	{
		cin>>p[i];
	}
	int a[n+1];
	for(int i=1;i<n+1;i++)
	{
		memset(a, 0, (n+1)*sizeof(a[0]));
		if(i!=p[0])
		{
			a[i]=1;

		}
		int t=0;
		for(int j=0;j<n;j++)
		{
			a[p[j]]=a[p[j]]+1;
			if(a[p[j]]>1)
			{
				cout<<p[j]<<" ";
				t=t+1;
				break;
			}

		}
		if(t==0)
		{
			for(int j=0;j<n;j++)
		{
			a[p[j]]=a[p[j]]+1;
			if(a[p[j]]>1)
			{
				cout<<p[j]<<" ";
				t=t+1;
				break;
			}

		}

		}

	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define s second
#define f first
#define pb push_back
#define mp make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    int ch[n+1],x;
    for(int i=0;i<=n;++i)
        ch[i]=-1;
    for(int i=1;i<=n;++i)
    {
        memset(ch,-1,sizeof(ch));
            x= i;
        while(ch[x]!=1)
        {
            if(ch[x]==-1)
                ch[x]=0,x=a[x];
            else
                ch[x]=1,x=a[x];
        }
        cout<<x<<" ";
    }
    cout<<"\n";
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define s second
#define f first
#define pb push_back
#define mp make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    int ch[n+1],x;
    memset(ch,-1,sizeof(ch));
    for(int i=1;i<=n;++i)
    {
        if(ch[i]==-1)
        {
            x= i;
            while(ch[x]!=1)
            {
                if(ch[x]==-1)
                    ch[x]=0,x=a[x];
                else
                    ch[x]=1,x=a[x];
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        x= i;
        while(ch[x]!=1) x =a[x];

        cout<<x<<" ";
    }
    cout<<"\n";
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define s second
#define f first
#define pb push_back
#define mp make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    int ch[n+1],x;
    for(int i=0;i<=n;++i)
        ch[i]=-1;
    for(int i=1;i<=n;++i)
    {
        if(ch[i]==-1 && ch[a[i]]==-1)
        {
            x= i;
            while(ch[x]!=1)
            {
                if(ch[x]==-1)
                    ch[x]=0,x=a[x];
                else
                    ch[x]=1,x=a[x];
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        x= i;
        while(ch[x]!=1) x =a[x];

        cout<<x<<" ";
    }
    cout<<"\n";
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define s second
#define f first
#define pb push_back
#define mp make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    int ch[n+1],x;
    for(int i=0;i<=n;++i)
        ch[i]=-1;
    for(int i=1;i<=n;++i)
    {
        if(ch[i]==-1 && ch[a[i]]!=0)
        {
            x= i;
            while(ch[x]!=1)
            {
                if(ch[x]==-1)
                    ch[x]=0,x=a[x];
                else
                    ch[x]=1,x=a[x];
            }
        }
        else if(ch[a[i]]==0)
            ch[i]=0;
    }
    for(int i=1;i<=n;++i)
    {
        x= i;
        while(ch[x]!=1) x =a[x];

        cout<<x<<" ";
    }
    cout<<"\n";
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define s second
#define f first
#define pb push_back
#define mp make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    int ch[n+1],x;
    for(int i=0;i<=n;++i)
        ch[i]=-1;
    for(int i=1;i<=n;++i)
    {
        if(ch[i]==-1)
        {
            x= i;
            while(ch[x]!=1)
            {
                if(ch[x]==-1)
                    ch[x]=0,x=a[x];
                else
                    ch[x]=1,x=a[x];
            }
        }
    }
    for(int i=1;i<=n;++i)
    {
        x= i;
        while(ch[x]!=1) x =a[x];

        cout<<x<<" ";
    }
    cout<<"\n";
}


#include <bits/stdc++.h>
#include<unordered_map>
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK:936777216")
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define Wal3a ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Wal3aBzyada ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define MOD (ll)1e9+7
#define nl '\n'
// fflush(stdout);  cout << flush;
ll OO = 0x3f3f3f3f;
ll x, y, z;
ll maxi = 0, k, n;
ll vis[105];
string s2, s1;
map<int, int>mp;
int main(){
	Wal3a
		/*	ll n, h, a, b, k;
		cin >> n >> h >> a >> b >> k;
		ll x1, y1, x2, y2;
		while (k--){
		cin >> x1 >> y1 >> x2 >> y2;
		if (x1 == x2){
		cout << abs(y1 - y2) << nl;
		}
		else{

		}
		}*/
		cin >> n;
	for (int i = 0; i < n; i++){
		cin >> x;
		mp[x]++;
	}
	vector <pair<int, int> >vp;
	for (auto it = mp.begin(); it != mp.end(); ++it){
		vp.push_back({ it->second, it->first });
	}
	sort(vp.rbegin(), vp.rend());
	for (int i = 0; i < vp.size(); i++){
		while (mp[vp[i].second]--){
			cout << vp[i].second << " ";
		}
	}
	cout << nl;
}


#include <bits/stdc++.h>
#include<unordered_map>
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK:936777216")
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define Wal3a ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Wal3aBzyada ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define MOD (ll)1e9+7
#define nl '\n'
// fflush(stdout);  cout << flush;
ll OO = 0x3f3f3f3f;
ll x, y, z;
ll maxi = 0, k, n,ko;
ll vis[105],vis2[105];
string s2, s1;
map<int, int>mp,mp1;
struct Compare {
	bool operator()(pair<char, int> const & a, pair<char, int> const & b) const
	{
		if (a.second == b.second)return a.first < b.first;
		else return a.second > b.second;
	}
}obj;
vector<int>vp[105];
void dfs(int s){
	if (vis[s] > 0){
		vis[s] += 1;
		return;
	}
	vis[s] += 1;
	for (int j = 0; j < vp[s].size(); j++)dfs(vp[s][j]);
}
int main(){
	Wal3a
		cin >> n;
	ll a,b;
	cin >> b;
	vis2[b] = 1;
	for (int i = 1; i <= n-1; i++){
		cin >> a;
		vis2[a] += 1;
		vp[b].push_back(a);
		b = a;
	}
	for (int i = 1; i <= n; i++){
		if (vis2[i]){
			dfs(i);
			while (vis[i]--&&vis2[i]--){
				cout << i << " ";
			}
			memset(vis, 0, sizeof vis);
		}
	}
	cout << nl;
}


#include <bits/stdc++.h>
#include<unordered_map>
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK:936777216")
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define Wal3a ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Wal3aBzyada ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define MOD (ll)1e9+7
#define nl '\n'
// fflush(stdout);  cout << flush;
ll OO = 0x3f3f3f3f;
ll x, y, z;
ll maxi = 0, k, n,ko;
ll vis[1005];
string s2, s1;
map<int, int>mp,mp1;
struct Compare {
	bool operator()(pair<char, int> const & a, pair<char, int> const & b) const
	{
		if (a.second == b.second)return a.first < b.first;
		else return a.second > b.second;
	}
}obj;
vector<int>vp;
int main(){
	Wal3a
	cin >> n;
	ll a;
	for (int i = 1; i <= n; i++){
		cin >> a;
		mp[i] = a;
	}
	for (int i = 1; i <= n; i++){
		int v=i;
		while (true){
			if (vis[v]){
				cout << v << " ";
				break;
			}
			++vis[v];
			v = mp[v];
		}
		memset(vis, 0, sizeof vis);
	}
	cout << nl;
}


#include <bits/stdc++.h>
#include<unordered_map>
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK:936777216")
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define Wal3a ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Wal3aBzyada ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define MOD (ll)1e9+7
#define nl '\n'
// fflush(stdout);  cout << flush;
ll OO = 0x3f3f3f3f;
ll x, y, z;
ll maxi = 0, k, n,ko;
ll vis[105];
string s2, s1;
map<int, int>mp,mp1;
struct Compare {
	bool operator()(pair<char, int> const & a, pair<char, int> const & b) const
	{
		if (a.second == b.second)return a.first < b.first;
		else return a.second > b.second;
	}
}obj;
int main(){
	Wal3a
		cin >> n;
	vector<pair<ll,ll> >v;
	for (int i = 0; i < n; i++){
		cin >> x;
		mp[x]++;
	}
	for (auto G : mp){
		v.push_back({ G.first, G.second });
	}
	sort(v.begin(), v.end(),obj);
	for (int i = 0;i<v.size();i++){
		while (v[i].second--){
			cout << v[i].first << " ";
		}
	}
	cout << nl;
}


#include <bits/stdc++.h>
#include<unordered_map>
#pragma GCC optimize ("O3")
#pragma comment(linker, "/STACK:936777216")
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif
using namespace std;
#define ll long long
#define ull unsigned long long
#define Wal3a ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Wal3aBzyada ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define MOD (ll)1e9+7
#define nl '\n'
// fflush(stdout);  cout << flush;
ll OO = 0x3f3f3f3f;
ll x, y, z;
ll maxi = 0, k, n,ko;
ll vis[1005], vis2[1005];
string s2, s1;
map<int, int>mp,mp1;
struct Compare {
	bool operator()(pair<char, int> const & a, pair<char, int> const & b) const
	{
		if (a.second == b.second)return a.first < b.first;
		else return a.second > b.second;
	}
}obj;
vector<int>vp[1005];
void dfs(int s){
	if (vis[s] > 0){
		vis[s] += 1;
		return;
	}
	vis[s] += 1;
	for (int j = 0; j < vp[s].size(); j++)dfs(vp[s][j]);
}
int main(){
	Wal3a
		cin >> n;
	ll a,b;
	cin >> b;
	vis2[b] = 1;
	for (int i = 1; i <= n-1; i++){
		cin >> a;
		vis2[a] += 1;
		vp[b].push_back(a);
		b = a;
	}
	for (int i = 1; i <= n; i++){
		if (vis2[i]){
			dfs(i);
			while (vis[i]--&&vis2[i]--){
				cout << i << " ";
			}
			memset(vis, 0, sizeof vis);
		}
	}
	cout << nl;
}


#include <bits/stdc++.h>
using namespace std;
int v[1010]={0};
int main()
{
    int n;
    cin >> n;
    vector<int>w(n);
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }
    for (int i = 1; i <= n; ++i) {
        for (int i = 0; i < 1010; ++i) {
            v[i]=0;
        }
        int j = i;
        while(!v[j])
        {
            v[j]++;
            j = w[j];
        }
        cout<<j<<" ";
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int>w(n);
    for (int i = 0; i < n; ++i) {
        cin >> w[i];
    }
    for (int i = 0; i < n; ++i) {
        set<int>g;
        g.insert(i+1);
        for (int j = 0; j < n; ++j) {
            if(g.count(w[j]))
            {
                cout<<w[j]<<" ";
                break;
            }
            g.insert(w[j]);
        }

    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int>w(n);
    for (int i = 0; i < n; ++i) {
        cin >> w[i];
    }
    for (int i = 0; i < n; ++i) {
        map<int,int>d;
        d[i+1]++;
        for (int j = 0; j < n; ++j) {
            d[w[j]]++;
            if(d[w[j]] >= 2)
            {
                cout<< w[j] << " ";
                break;
            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
int v[1010];
int main()
{
    int n;
    cin >> n;
    vector<int>w(n);
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }
    for (int i = 1; i <= n; ++i) {
        for (int i = 0; i < 1011; ++i) {
            v[i]=0;
        }
        int j = i;
        while(!v[j])
        {
            v[j]++;
            j = w[j];
        }
        cout<<j<<" ";
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
int v[1010]={0};
int main()
{
    int n;
    cin >> n;
    vector<int>w(n+1);
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }
    for (int i = 1; i <= n; ++i) {
        for (int i = 0; i < 1010; ++i) {
            v[i]=0;
        }
        int j = i;
        while(!v[j])
        {
            v[j]++;
            j = w[j];
        }
        cout<<j<<" ";
    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int>w(n);
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }
    for (int i = 1; i <= n; ++i) {
        set<int>g;
        int j = i;
        while(g.count(j) == 0)
        {
            g.insert(j);
            j = w[j];
        }
        cout<<j<<" ";
    }
    return 0;

}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std ;
ll mod (ll a)
{ if(a>0) return a; else return -a;}
ll t[200001];
ll n;
void build() { for(int i=n-1;i>0;i--) t[i]=min(t[2*i],t[2*i+1]);}
ll query(ll l,ll r)
{
    if(l>r) return -1;
    ll res=INT_MAX;
    for(l+=n,r+=n;l<r;l>>=1,r>>=1)
    {
        if(l&1) res=min(t[l++],res);
        if(r&1) res=min(t[--r],res);
    }
    return res;
}
vector<int> g[1001];
#define pb push_back
void edge(int a, int b)
{
    g[a].pb(b);
}
vector<bool> v;
void bfs(int u)
{
    queue<int> q;
    q.push(u);
    v[u] = true;
    int f=0;
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i = 0;i < g[ p ].size() ; i++)
            {
                if(v[ g[ p ][ i ] ] == false)
                {
                     q.push(g[ p ][ i ]);
                     v[g[ p ][ i ] ] = true;
                }
                else {
                    if(g[p][i]==1000 && f==0) { cout<<1<<endl; f=1; }
                 else
                        cout<<g[p][i]<<" "; return; }
    }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
	ll n; cin>>n;

  //  g.assign(n, vector<int>());
	for(int i=1;i<=n;i++)
    {
        ll a; cin>>a;
        g[i].push_back(a);
    }
    //cout<<1<<endl;
    for(int i=1;i<=n;i++)
       {
           v.assign(n,false);
           bfs(i);
       }
}
//dipcoders


#include <bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std ;
ll mod (ll a)
{ if(a>0) return a; else return -a;}
ll t[200001];
ll n;
void build() { for(int i=n-1;i>0;i--) t[i]=min(t[2*i],t[2*i+1]);}
ll query(ll l,ll r)
{
    if(l>r) return -1;
    ll res=INT_MAX;
    for(l+=n,r+=n;l<r;l>>=1,r>>=1)
    {
        if(l&1) res=min(t[l++],res);
        if(r&1) res=min(t[--r],res);
    }
    return res;
}
ll minVal(ll x, ll y) { return (x < y)? x: y; }
ll getMid(ll s, ll e) {  return s + (e -s)/2;  }
ll RMQUtil(ll *st, ll ss, ll se, ll qs, ll qe, ll index)
{
    if (qs <= ss && qe >= se)
        return st[index];
    if (se < qs || ss > qe)
        return INT_MAX;
   ll mid = getMid(ss, se);
    return minVal(RMQUtil(st, ss, mid, qs, qe, 2*index+1),
                  RMQUtil(st, mid+1, se, qs, qe, 2*index+2));
}
ll RMQ(ll *st, ll n, ll qs, ll qe)
{
    if (qs < 0 || qe > n-1 || qs > qe)
    {
        printf("Invalid Input");
        return -1;
    }
    return RMQUtil(st, 0, n-1, qs, qe, 0);
}
ll constructSTUtil(ll arr[], ll ss, ll se, ll *st, ll si)
{
    if (ss == se)
    {
        st[si] = arr[ss];
        return arr[ss];
    }
    ll mid = getMid(ss, se);
    st[si] =  minVal(constructSTUtil(arr, ss, mid, st, si*2+1),
                     constructSTUtil(arr, mid+1, se, st, si*2+2));
    return st[si];
}
ll *constructST(ll arr[], ll n)
{
    ll x = (ll)(ceil(log2(n)));
    ll max_size = 2*(ll)pow(2, x) - 1;
    ll *st = new ll[max_size];
    constructSTUtil(arr, 0, n-1, st, 0);
    return st;
}
vector<int> g[1001];
#define pb push_back
void edge(int a, int b)
{
    g[a].pb(b);
}
vector<bool> v;
void bfs(int u)
{
    queue<int> q;
    q.push(u);
    v[u] = true;
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i = 0;i < g[ p ].size() ; i++)
            {
                if(v[ g[ p ][ i ] ] == false)
                {
                     q.push(g[ p ][ i ]);
                     v[g[ p ][ i ] ] = true;
                }
                else { cout<<g[p][i]<<" "; return; }
    }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
	ll n; cin>>n;

  //  g.assign(n, vector<int>());
	for(int i=1;i<=n;i++)
    {
        ll a; cin>>a;
        g[i].push_back(a);
    }
    //cout<<1<<endl;
    for(int i=1;i<=n;i++)
       {
           v.assign(n,false);
           bfs(i);
       }
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std ;
ll mod (ll a)
{ if(a>0) return a; else return -a;}
ll t[200001];
ll n;
void build() { for(int i=n-1;i>0;i--) t[i]=min(t[2*i],t[2*i+1]);}
ll query(ll l,ll r)
{
    if(l>r) return -1;
    ll res=INT_MAX;
    for(l+=n,r+=n;l<r;l>>=1,r>>=1)
    {
        if(l&1) res=min(t[l++],res);
        if(r&1) res=min(t[--r],res);
    }
    return res;
}
int main()
{
    ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int arr[n+1];
	int i;
	for(i=1;i<=n;i++)
		cin>>arr[i];
	for(i=1;i<=n;i++)
	{
		map<int,int> m1;
		m1[i]++;
		int t1=arr[i];
		m1[t1]++;
		int x=t1;
		while(1)
		{
			if(m1[x]>1)	break;
			x=arr[x];
			m1[x]++;
			if(m1[x]>1)
				break;
		}
		cout<<x<<" ";
	}
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std ;
ll mod (ll a)
{ if(a>0) return a; else return -a;}
ll t[200001];
ll n;
void build() { for(int i=n-1;i>0;i--) t[i]=min(t[2*i],t[2*i+1]);}
ll query(ll l,ll r)
{
    if(l>r) return -1;
    ll res=INT_MAX;
    for(l+=n,r+=n;l<r;l>>=1,r>>=1)
    {
        if(l&1) res=min(t[l++],res);
        if(r&1) res=min(t[--r],res);
    }
    return res;
}
vector<int> g[1001];
#define pb push_back
void edge(int a, int b)
{
    g[a].pb(b);
}
vector<bool> v;
void bfs(int u)
{
    queue<int> q;
    q.push(u);
    v[u] = true;
    int f=0;
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i = 0;i < g[ p ].size() ; i++)
            {
                if(v[ g[ p ][ i ] ] == false)
                {
                     q.push(g[ p ][ i ]);
                     v[g[ p ][ i ] ] = true;
                }
                else {
                    if(g[p][i]==1000 && f==0) { cout<<1<<" "; f=1; }
                 else
                        cout<<g[p][i]<<" "; return; }
    }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
	ll n; cin>>n;

  //  g.assign(n, vector<int>());
	for(int i=1;i<=n;i++)
    {
        ll a; cin>>a;
        g[i].push_back(a);
    }
    //cout<<1<<endl;
    for(int i=1;i<=n;i++)
       {
           v.assign(n,false);
           bfs(i);
       }
}
//dipcoders


#include <bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std ;
ll mod (ll a)
{ if(a>0) return a; else return -a;}
ll t[200001];
ll n;
void build() { for(int i=n-1;i>0;i--) t[i]=min(t[2*i],t[2*i+1]);}
ll query(ll l,ll r)
{
    if(l>r) return -1;
    ll res=INT_MAX;
    for(l+=n,r+=n;l<r;l>>=1,r>>=1)
    {
        if(l&1) res=min(t[l++],res);
        if(r&1) res=min(t[--r],res);
    }
    return res;
}
ll minVal(ll x, ll y) { return (x < y)? x: y; }
ll getMid(ll s, ll e) {  return s + (e -s)/2;  }
ll RMQUtil(ll *st, ll ss, ll se, ll qs, ll qe, ll index)
{
    if (qs <= ss && qe >= se)
        return st[index];
    if (se < qs || ss > qe)
        return INT_MAX;
   ll mid = getMid(ss, se);
    return minVal(RMQUtil(st, ss, mid, qs, qe, 2*index+1),
                  RMQUtil(st, mid+1, se, qs, qe, 2*index+2));
}
ll RMQ(ll *st, ll n, ll qs, ll qe)
{
    if (qs < 0 || qe > n-1 || qs > qe)
    {
        printf("Invalid Input");
        return -1;
    }
    return RMQUtil(st, 0, n-1, qs, qe, 0);
}
ll constructSTUtil(ll arr[], ll ss, ll se, ll *st, ll si)
{
    if (ss == se)
    {
        st[si] = arr[ss];
        return arr[ss];
    }
    ll mid = getMid(ss, se);
    st[si] =  minVal(constructSTUtil(arr, ss, mid, st, si*2+1),
                     constructSTUtil(arr, mid+1, se, st, si*2+2));
    return st[si];
}
ll *constructST(ll arr[], ll n)
{
    ll x = (ll)(ceil(log2(n)));
    ll max_size = 2*(ll)pow(2, x) - 1;
    ll *st = new ll[max_size];
    constructSTUtil(arr, 0, n-1, st, 0);
    return st;
}
vector<int> g[1001];
#define pb push_back
void edge(int a, int b)
{
    g[a].pb(b);
}
vector<bool> v;
void bfs(int u)
{
    queue<int> q;
    q.push(u);
    v[u] = true;
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i = 0;i < g[ p ].size() ; i++)
            {
                if(v[ g[ p ][ i ] ] == false)
                {
                    //level[ g[ p ][ i ] ] = level[ p ]+1;
                     q.push(g[ p ][ i ]);
                     v[g[ p ][ i ] ] = true;
                }
                else { cout<<g[p][i]<<" "<<endl; return; }
    }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
	ll n; cin>>n;

  //  g.assign(n, vector<int>());
	for(int i=1;i<=n;i++)
    {
        ll a; cin>>a;
        g[i].push_back(a);
    }
    //cout<<1<<endl;
    for(int i=1;i<=n;i++)
       {
           v.assign(n,false);
           bfs(i);
       }

}
//dipcoders


#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <stdio.h>
#include <unordered_set>
#include <iterator>
#include <math.h>
#include <iomanip>
using namespace std;

int dfs(vector<int> arr, vector<bool> &v, int i){
    if(v[i]) return i;
    v[i] = true;
    return dfs(arr, v, arr[i]-1);
}

int main(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }
    for(int i = 0; i < n; i++){
        vector<bool> v(n, false);
        cout << dfs(arr, v, i)+1 << " ";
    }
    cout << endl;
}

// // https://codeforces.com/problemset/page/4?order=BY_SOLVED_DESC