#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += (pos[b[i]] - i)*2 + 1;
                cur_pos = pos[b[i]];

            }
            else{
                ans+=1;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                if(cur_pos != -1)
                ans += ((max((pos[b[i]]- cur_pos-1), 0) * 2) + 1);
                else
                {
                    ans += ((max((pos[b[i]]), 0) * 2) + 1);
                }

                cur_pos = pos[b[i]];
            }
            else{
                ans+=1;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += (((pos[b[i]]- count) * 2) + 1);
                cur_pos = pos[b[i]];
                count++;
            }
            else{
                ans+=1;
            }
        }
        cout << ans << endl;
    }
}


#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include <fstream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define pb push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define MAX 100000
using namespace std;
typedef long long int lli;
typedef unsigned long long ULL;
typedef long double LD;

int t, n, m;
vector<int> a(100007);
vector<int> b(100007);
map<int, int> pos;
int main(){
    cin >> t;
    while(t--){
        cin >> n >> m;
        pos.clear();
        REP(i, n){
             cin >> a[i];
             pos[a[i]] = i;
        }
        REP(i, m){
            cin >> b[i];
        }
        lli ans = 0;
        int cur_pos = -1;
        int count = 0;
        REP(i, m){
            if(pos[b[i]] > cur_pos){
                ans += ((max((pos[b[i]]- count), 0) * 2) + 1);
                cur_pos = pos[b[i]]-count;
                count++;
            }
            else{
                ans+=1;
                count++;
            }
        }
        cout << ans << endl;
    }
}


#include<iostream>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,m;
        cin>>n>>m;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            cin>>b[i];
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
//            cout<<"aaa "<<b[i]<<endl;
            if(mp[b[i]]==i) ans++;
            else{
//                cout<<"current="<<current<<"pos="<<mp[b[i]]<<endl;
                ans+=(mp[b[i]]-current)*2+1;//to get this present
//                cout<<"ans="<<ans<<endl;
                vis[b[i]]=0;
                for(int j=i;j<=mp[b[i]];j++){
                    if(vis[c[j]]==1){
                        ans++;
                        vis[c[j]]=0;
                        current++;
                    }
                }
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;
                int next=b[ii];//next get present 's pos
                while(ii+1<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){
                    ans++;
                    vis[next]=0;
                     current=mp[next];
                    ii+=1;
                    next=b[ii];

                }
            }
            current=mp[b[i]]+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal 处理栈
                int ii=i;
                int next=b[ii+1];//next get present 's pos
                while(ii<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){
                    ans++;
                    vis[next]=0;
                    next=b[++ii];
                    current=mp[next];
                }
            }
            current=mp[b[i]]+1;
        }
        cout<<ans-1<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        long long ans=0;//signed integer overflow
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;//也可以直接用i的跳转实现

            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{

                ans+=(mp[b[i]]-i)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;//下一个要取出的礼物
                int next=b[ii];//next get present 's pos
                while(ii<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){//下一个要取出的礼物包含在[current,mp[b[i]];
                    ans++;
                    vis[next]=0;
                    ii+=1;
                    next=b[ii];
                }
                current=mp[b[i]];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;//也可以直接用i的跳转实现

            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{

                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;//下一个要取出的礼物
                int next=b[ii];//next get present 's pos
                while(ii<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){//下一个要取出的礼物包含在[current,mp[b[i]];
                    ans++;
                    vis[next]=0;
                    ii+=1;
                    next=b[ii];
                }
                current+=(ii-1)-i;//指针后移
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal 处理栈
                for(int j=current;j<=mp[b[i]];j++){
                    if(vis[c[j]]==1){
                        ans++;
                        vis[c[j]]=0;
                        current++;
                    }
                }
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        long long ans=0;//signed integer overflow
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;//也可以直接用i的跳转实现

            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{

                ans+=(mp[b[i]]-i)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;//下一个要取出的礼物
                int next=b[ii];//next get present 's pos
                while(ii<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){//下一个要取出的礼物包含在[current,mp[b[i]];
                    ans++;
                    vis[next]=0;
                    ii+=1;
                    next=b[ii];
                }
                current+=(ii-1)-i;//指针后移
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        long long ans=0;//signed integer overflow
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;//也可以直接用i的跳转实现

            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{

                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;//下一个要取出的礼物
                int next=b[ii];//next get present 's pos
                while(ii<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){//下一个要取出的礼物包含在[current,mp[b[i]];
                    ans++;
                    vis[next]=0;
                    ii+=1;
                    next=b[ii];
                }
                current+=(ii-1)-i;//指针后移
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
        }
        long long ans=0;//signed integer overflow
        int current=0;//当前栈顶指针
        for(int i=1;i<=m;i++){

            if(mp[b[i]]<=current) ans++;
            else{
                ans+=(mp[b[i]]-i)*2+1;//to get this present
                current=mp[b[i]];
            }

        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal 处理栈
                int ii=i;
                int next=b[ii+1];//next get present 's pos
                while(ii+1<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){
                    ans++;
                    vis[next]=0;
                    next=b[++ii];
                    current=mp[next];
                }
            }
            current=mp[b[i]]+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                for(int j=i;j<=mp[b[i]];j++){
                    if(vis[c[j]]==1){
                        ans++;
                        vis[c[j]]=0;
                        current++;
                    }
                }
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010],mp[100010];
int read() {
    int x = 0, w = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {  // ch 不是数字时
        if (ch == '-') w = -1;        // 判断是否为负
        ch = getchar();               // 继续读入
    }
    while (ch >= '0' && ch <= '9') {  // ch 是数字时
        x = x * 10 + (ch - '0');  // 将新读入的数字’加’在 x 的后面
        // x 是 int 类型，char 类型的 ch 和 ’0’ 会被自动转为其对应的
        // ASCII 码，相当于将 ch 转化为对应数字
        // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10
        ch = getchar();  // 继续读入
    }
    return x * w;  // 数字 * 正负号 = 实际数值
}
int main()
{
    int test;
    test=read();
    while(test--){
        int n,m;
        n=read();
        m=read();
        for(int i=1;i<=n;i++){
            a[i]=read();
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            b[i]=read();
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;//当前栈顶指针
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
            if(mp[b[i]]==current) ans++;//当前礼物vis==0且在栈顶 直接取出
            else{
                ans+=(mp[b[i]]-current)*2+1;//to get this present
                vis[b[i]]=0;
                //during the process optimal deal with stack
                int ii=i+1;
                int next=b[ii];//next get present 's pos
                while(ii+1<=m&&mp[next]>=current&&mp[next]<=mp[b[i]]){
                    ans++;
                    vis[next]=0;
//                    current=mp[next];
                    ii+=1;
                    next=b[ii];

                }
            }
            current=mp[b[i]]+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<unordered_map>
using namespace std;
int a[100010],b[100010],vis[100010],c[100010];
unordered_map<int,int>mp;
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,m;
        cin>>n>>m;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            cin>>a[i];
            mp[a[i]]=i;
            c[i]=a[i];
            //pos=mp[i]; (pos-1)*2+1 seconds to get i present
        }
        for(int i=1;i<=m;i++){
            cin>>b[i];
            vis[b[i]]=1;
        }
        int ans=0;
        int current=1;
        for(int i=1;i<=m;i++){
            if(vis[b[i]]==0)    continue;
//            cout<<"aaa "<<b[i]<<endl;
            if(mp[b[i]]==i) ans++;
            else{
//                cout<<"current="<<current<<"pos="<<mp[b[i]]<<endl;
                ans+=(mp[b[i]]-current)*2+1;//to get this present
//                cout<<"ans="<<ans<<endl;
                vis[b[i]]=0;
                for(int j=i;j<=mp[b[i]];j++){
                    if(vis[c[j]]==1){
                        ans++;
                        vis[c[j]]=0;
                        current++;
                    }
                }
            }
            current++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
const int maxn=1e5+10;
int a[maxn],b[maxn],pos[maxn];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            pos[a[i]]=i;
        }
        for(int i=1;i<=m;i++){
            cin>>b[i];
        }
        int current=1;
        long long ans=0;
        for(int i=1;i<=m;i++){
            if(pos[b[i]]<=current){
                ans++;//fetch directly cost one second
            }
            else{
                ans+=(pos[b[i]]-i)*2+1;//k==pos[b[i]-i to get the i th present ,need to remove k present(higher than target present in the stack) before get target,and return them back after getting target,cost k sencond respectively,and cost 1 second to get the target ,so the sum is ((pos[b[i]]-i)*2+1) seconds
                current=pos[b[i]];
            }
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

vector<int> block[SQN];
int par[SQN][C], rnk[SQN][C], color[SQN][C];

void build_dsu(int idx){
	for(int i = 1; i <= 100; i++){
		par[idx][i] = color[idx][i] = i, rnk[idx][i] = 1;
	}
}

int fnd(int idx, int u){
	if(par[idx][u] == u) return u;
	return par[idx][u] = fnd(idx, par[idx][u]);
}

void join(int idx, int a, int b){
	a = fnd(idx, a), b = fnd(idx, b);
	if(a != b){
		if(rnk[idx][a] < rnk[idx][b]) swap(a, b);
		else if(rnk[idx][a] == rnk[idx][b]) rnk[idx][a]++;
		par[idx][b] = a;
	}
}

void upd(int idx){
	for(int i = 0; i < len(block[idx]); i++){
		block[idx][i] = color[idx][fnd(idx, block[idx][i])];
	}
	build_dsu(idx);
}

void modify(int idx, int l, int r, int x, int y){
	upd(idx);
	for(int i = l; i <= r; i++){
		if(block[idx][i] == x) block[idx][i] = y;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		block[i/SQN].push_back(a);
	}
	for(int i = 0; i < SQN; i++){
		build_dsu(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/SQN, br = r/SQN;
		if(bl == br){
			modify(bl, l%SQN, r%SQN, x, y);
		}else{
			modify(bl, l%SQN, len(block[bl]) - 1, x, y);
			for(int i = bl + 1; i < br; i++){
				if(color[i][fnd(i, x)] == x){
					join(i, x, y);
					color[i][fnd(i, x)] = y;
				}
			}
			modify(br, 0, r%SQN, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		upd(i);
		for(int x : block[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

inline void push(int idx, int l, int r){
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	push(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	push(idx, l, r);
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

int n;
vector<int> block[SQN];
int par[SQN][C], rnk[SQN][C], color[SQN][C];

void build_dsu(int idx){
	for(int i = 1; i <= 100; i++){
		par[idx][i] = color[idx][i] = i, rnk[idx][i] = 1;
	}
}

int fnd(int idx, int u){
	if(par[idx][u] == u) return u;
	return par[idx][u] = fnd(idx, par[idx][u]);
}

void join(int idx, int a, int b){
	a = fnd(idx, a), b = fnd(idx, b);
	if(a != b){
		if(rnk[idx][a] < rnk[idx][b]) swap(a, b);
		else if(rnk[idx][a] == rnk[idx][b]) rnk[idx][a]++;
		par[idx][b] = a;
	}
}

void upd(int idx){
	for(int i = 0; i < len(block[idx]); i++){
		int p = fnd(idx, block[idx][i]);
		assert(p >= 1 && p <= 100);
		block[idx][i] = color[idx][fnd(idx, block[idx][i])];
		assert(block[idx][i] >= 1 && block[idx][i] <= 100);
	}
	build_dsu(idx);
}

void modify(int idx, int l, int r, int x, int y){
	assert(l <= r && l >= 0 && r < n);
	upd(idx);
	for(int i = l; i <= r; i++){
		if(block[idx][i] == x) block[idx][i] = y;
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		block[i/SQN].push_back(a);
	}
	for(int i = 0; i < SQN; i++){
		build_dsu(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/SQN, br = r/SQN;
		if(bl == br){
			modify(bl, l%SQN, r%SQN, x, y);
		}else{
			modify(bl, l%SQN, len(block[bl]) - 1, x, y);
			for(int i = bl + 1; i < br; i++){
				int p = fnd(i, x);
				assert(p >= 1 && p <= 100);
				if(color[i][fnd(i, x)] == x){
					join(i, x, y);
					color[i][fnd(i, x)] = y;
				}
			}
			modify(br, 0, r%SQN, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		upd(i);
		for(int x : block[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 510, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int S = min(500, n);
	int M = (n + S - 1) / S;
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/S][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/S, br = r/S;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), move(pos[i][x]));
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < M; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].end(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/SQN][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		if(x == y) continue;
		int bl = l/SQN, br = r/SQN;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].end(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 510, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(list<int> &x, list<int> &y, int l, int r){
	auto it = x.begin();
	while(it != x.end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			y.splice(y.begin(), x, it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int S = min(500, n);
	int M = (n + S - 1) / S;
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/S][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		if(x == y) continue;
		int bl = l/S, br = r/S;
		modify(pos[bl][x], pos[bl][y], l, r);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), move(pos[i][x]));
			}
			modify(pos[br][x], pos[br][y], l, r);
		}
	}
	for(int i = 0; i < M; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void upd(int idx, int l, int r){
	if(l == r) return;
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	upd(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	upd(idx, l, r);
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


//#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

inline void push(int idx, int l, int r){
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	push(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	push(idx, l, r);
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

inline void push(int idx, int l, int r){
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	push(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	push(idx, l, r);
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQRTN = 450, C = 110;

vector<int> seg[SQRTN];
int nval[SQRTN][C];

void upd(int idx){
	for(int i = 0; i < len(seg[idx]); i++){
		seg[idx][i] = nval[idx][seg[idx][i]];
	}
	for(int i = 1; i <= 100; i++){
		nval[idx][i] = i;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < SQRTN; i++){
		for(int j = 1; j <= 100; j++){
			nval[i][j] = j;
		}
	}
	for(int i = 0; i < n; i++){
		int c;
		scanf("%d", &c);
		seg[i/SQRTN].push_back(c);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;

		int bl = l/SQRTN, pl = l%SQRTN;
		int br = r/SQRTN, pr = r%SQRTN;

		if(bl == br){
			upd(bl);
			for(int i = pl; i <= pr; i++){
				if(seg[bl][i] == x) seg[bl][i] = y;
			}
		}else{
			upd(bl);
			for(int i = pl; i < len(seg[bl]); i++){
				if(seg[bl][i] == x) seg[bl][i] = y;
			}
			for(int i = bl + 1; i < br; i++){
				for(int j = 1; j <= 100; j++){
					if(nval[i][j] == x) nval[i][j] = y;
				}
			}
			upd(br);
			for(int i = 0; i <= pr; i++){
				if(seg[br][i] == x) seg[br][i] = y;
			}
		}
	}
	for(int i = 0; i < SQRTN; i++){
		upd(i);
		for(int x : seg[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

inline void push(int idx, int l, int r){
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	push(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	push(idx, l, r);
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].end(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int sqn = sqrt(n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/sqn][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/sqn, br = r/sqn;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].end(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i <= sqn; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/SQN][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		if(x == y) continue;
		int bl = l/SQN, br = r/SQN;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQRTN = 450, C = 110;

vector<int> seg[SQRTN];
int nval[SQRTN][C];

void upd(int idx){
	for(int i = 0; i < len(seg[idx]); i++){
		seg[idx][i] = nval[idx][seg[idx][i]];
	}
	for(int i = 1; i <= 100; i++){
		nval[idx][i] = i;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < SQRTN; i++){
		for(int j = 1; j <= 100; j++){
			nval[i][j] = j;
		}
	}
	for(int i = 0; i < n; i++){
		int c;
		scanf("%d", &c);
		seg[i/SQRTN].push_back(c);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;

		int bl = l/SQRTN, pl = l%SQRTN;
		int br = r/SQRTN, pr = r%SQRTN;

		if(bl == br){
			upd(bl);
			for(int i = pl; i <= pr; i++){
				if(seg[bl][i] == x) seg[bl][i] = y;
			}
		}else{
			upd(bl);
			for(int i = pl; i < len(seg[bl]); i++){
				if(seg[bl][i] == x) seg[bl][i] = y;
			}
			for(int i = bl + 1; i < br; i++){
				for(int j = 1; j <= 100; j++){
					if(nval[i][j] == x) nval[i][j] = x;
				}
			}
			upd(br);
			for(int i = 0; i <= pr; i++){
				if(seg[br][i] == x) seg[br][i] = y;
			}
		}
	}
	for(int i = 0; i < SQRTN; i++){
		upd(i);
		for(int x : seg[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void upd(int idx, int l, int r){
	if(l == r) return;
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	upd(idx, l, r);
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	upd(idx, l, r);
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 450*450, SQN = 448, C = 110;

int arr[N];
int par[SQN][C], rnk[SQN][C], color[SQN][C];

void make_set(int idx){
	for(int i = 0; i < C; i++){
		par[idx][i] = i, rnk[idx][i] = 1;
		color[idx][i] = i;
	}
}

int fnd(int idx, int u){
	if(par[idx][u] == u) return u;
	return par[idx][u] = fnd(idx, par[idx][u]);
}

void join(int idx, int a, int b){
	a = fnd(idx, a), b = fnd(idx, b);
	if(a != b){
		if(rnk[idx][a] < rnk[idx][b]) swap(a, b);
		else if(rnk[idx][a] == rnk[idx][b]) rnk[idx][a]++;
		par[idx][b] = a;
	}
}

void build(){
	for(int i = 0; i < SQN; i++){
		make_set(i);
	}
}

void upd(int idx){
	int l = idx*SQN, r = (idx + 1)*SQN - 1;
	for(int i = l; i <= r; i++){
		arr[i] = color[idx][fnd(idx, arr[i])];
	}
	make_set(idx);
}

void modify(int idx, int a, int b, int x, int y){
	if(idx == SQN) return;
	int l = idx*SQN, r = (idx + 1)*SQN - 1;
	if(!(l > b || r < a)){
		if(l >= a && r <= b){
			if(color[idx][fnd(idx, x)] == x){
				join(idx, x, y);
				color[idx][fnd(idx, x)] = y;
			}
		}else{
			upd(idx);
			l = max(l, a), r = min(r, b);
			for(int i = l; i <= r; i++){
				if(arr[i] == x) arr[i] = y;
			}
		}
	}
	modify(idx + 1, a, b, x, y);
}

void rebuild(){
	for(int i = 0; i < SQN; i++){
		upd(i);
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
	}
	build();
	int q;
	scanf("%d", &q);
	while(q--){
		int a, b, x, y;
		scanf("%d %d %d %d", &a, &b, &x, &y); a--, b--;
		modify(0, a, b, x, y);
	}
	rebuild();
	for(int i = 0; i < n; i++){
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].end(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/SQN][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/SQN, br = r/SQN;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].end(), move(pos[i][x]));
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int sqn = sqrt(n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/sqn][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/sqn, br = r/sqn;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), move(pos[i][x]));
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i <= sqn; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 510, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(list<int> &x, list<int> &y, int l, int r){
	auto it = x.begin();
	while(it != x.end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			y.splice(y.begin(), x, it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int S = min(500, n);
	int M = (n + S - 1) / S;
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/S][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/S, br = r/S;
		modify(pos[bl][x], pos[bl][y], l, r);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), move(pos[i][x]));
			}
			modify(pos[br][x], pos[br][y], l, r);
		}
	}
	for(int i = 0; i < M; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int sqn = sqrt(n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/sqn][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		if(x == y) continue;
		int bl = l/sqn, br = r/sqn;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].end(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/SQN][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/SQN, br = r/SQN;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].end(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 510, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int sqn = min(500, n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/sqn][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/sqn, br = r/sqn;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), move(pos[i][x]));
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i <= sqn; i++){
		for(int j = 1; j <= 100; j++){
			for(int p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQN = 450, C = 110;

list<int> pos[SQN][C];
int ans[N];

void modify(int idx, int l, int r, int x, int y){
	auto it = pos[idx][x].begin();
	while(it != pos[idx][x].end()){
		auto nxt = next(it);
		if(*it >= l && *it <= r){
			pos[idx][y].splice(pos[idx][y].begin(), pos[idx][x], it);
		}
		it = nxt;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int sqn = sqrt(n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		pos[i/sqn][a].push_back(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		if(x == y) continue;
		int bl = l/sqn, br = r/sqn;
		modify(bl, l, r, x, y);
		if(bl != br){
			for(int i = bl + 1; i < br; i++){
				pos[i][y].splice(pos[i][y].begin(), pos[i][x]);
			}
			modify(br, l, r, x, y);
		}
	}
	for(int i = 0; i <= sqn; i++){
		for(int j = 1; j <= 100; j++){
			for(auto p : pos[i][j]){
				ans[p] = j;
			}
		}
	}
	for(int i = 0; i < n; i++){
		printf("%d ", ans[i]);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 2e5 + 10, SQRTN = 450, C = 110;

vector<int> seg[SQRTN];
int qtd[SQRTN][C], nval[SQRTN][C], pre[SQRTN][C];

void upd(int idx){
	for(int i = 1; i <= 100; i++) qtd[idx][i] = 0;
	for(int i = 0; i < len(seg[idx]); i++){
		seg[idx][i] = nval[idx][seg[idx][i]];
		qtd[idx][seg[idx][i]]++;
	}
	for(int i = 1; i <= 100; i++) nval[idx][i] = pre[idx][i] = i;
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < SQRTN; i++){
		for(int j = 1; j <= 100; j++) nval[i][j] = pre[i][j] = j;
	}
	for(int i = 0; i < n; i++){
		int c;
		scanf("%d", &c);
		seg[i/SQRTN].push_back(c);
		qtd[i/SQRTN][c]++;
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;

		int bl = l/SQRTN, pl = l%SQRTN;
		int br = r/SQRTN, pr = r%SQRTN;

		if(bl == br){
			upd(bl);
			for(int i = pl; i <= pr; i++) if(seg[bl][i] == x){
				qtd[bl][x]--;
				seg[bl][i] = y;
				qtd[bl][y]++;
			}
		}else{
			upd(bl);
			for(int i = pl; i < len(seg[bl]); i++) if(seg[bl][i] == x){
				qtd[bl][x]--;
				seg[bl][i] = y;
				qtd[bl][y]++;
			}

			for(int i = bl + 1; i < br; i++){
				nval[i][pre[i][x]] = y;
				pre[i][y] = x;
			}

			upd(br);
			for(int i = 0; i <= pr; i++) if(seg[br][i] == x){
				qtd[br][x]--;
				seg[br][i] = y;
				qtd[br][y]++;
			}
		}
	}
	for(int i = 0; i < SQRTN; i++){
		upd(i);
		for(int x : seg[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define len(x) ((int)(x).size())

const int N = 3e5 + 10, SQN = 450, C = 110;

vector<int> block[SQN];
int par[SQN][C], rnk[SQN][C], color[SQN][C];

void build_dsu(int idx){
	for(int i = 1; i <= 100; i++){
		par[idx][i] = color[idx][i] = i, rnk[idx][i] = 1;
	}
}

int fnd(int idx, int u){
	if(par[idx][u] == u) return u;
	return par[idx][u] = fnd(idx, par[idx][u]);
}

void join(int idx, int a, int b){
	a = fnd(idx, a), b = fnd(idx, b);
	if(a != b){
		if(rnk[idx][a] < rnk[idx][b]) swap(a, b);
		else if(rnk[idx][a] == rnk[idx][b]) rnk[idx][a]++;
		par[idx][b] = a;
	}
}

void upd(int idx){
	for(int i = 0; i < len(block[idx]); i++){
		block[idx][i] = color[idx][fnd(idx, block[idx][i])];
	}
	build_dsu(idx);
}

void modify(int idx, int l, int r, int x, int y){
	upd(idx);
	for(int i = l; i <= r; i++){
		if(block[idx][i] == x) block[idx][i] = y;
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		block[i/SQN].push_back(a);
	}
	for(int i = 0; i < SQN; i++){
		build_dsu(i);
	}
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y); l--, r--;
		int bl = l/SQN, br = r/SQN;
		if(bl == br){
			modify(bl, l%SQN, r%SQN, x, y);
		}else{
			modify(bl, l%SQN, len(block[bl]) - 1, x, y);
			for(int i = bl + 1; i < br; i++){
				if(color[i][fnd(i, x)] == x){
					join(i, x, y);
					color[i][fnd(i, x)] = y;
				}
			}
			modify(br, 0, r%SQN, x, y);
		}
	}
	for(int i = 0; i < SQN; i++){
		upd(i);
		for(int x : block[i]) printf("%d ", x);
	}
	printf("\n");
	return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define len(x) ((int)(x).size())
#define x first
#define y second
using namespace std;

const int N = 2e5 + 10, C = 110;

int a[N];
int seg[4*N][C];

void build(int idx, int l, int r){
	if(l != r){
		int mid = (l + r)/2;
		build(2*idx + 1, l, mid);
		build(2*idx + 2, mid + 1, r);
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

inline void push(int idx, int l, int r){
	for(int i = 1; i <= 100; i++){
		seg[2*idx + 1][i] = seg[idx][seg[2*idx + 1][i]];
		seg[2*idx + 2][i] = seg[idx][seg[2*idx + 2][i]];
	}
	for(int i = 1; i <= 100; i++){
		seg[idx][i] = i;
	}
}

void modify(int idx, int l, int r, int a, int b, int x, int y){
	if(l > b || r < a) return;
	if(l >= a && r <= b){
		for(int i = 1; i <= 100; i++){
			if(seg[idx][i] == x) seg[idx][i] = y;
		}
		return;
	}
	push(idx, l, r);
	int mid = (l + r)/2;
	modify(2*idx + 1, l, mid, a, b, x, y);
	modify(2*idx + 2, mid + 1, r, a, b, x, y);
}

int query(int idx, int l, int r, int pos, int x){
	if(l == r) return seg[idx][x];
	int mid = (l + r)/2;
	push(idx, l, r);
	if(pos <= mid){
		return query(2*idx + 1, l, mid, pos, x);
	}
	return query(2*idx + 2, mid + 1, r, pos, x);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	build(0, 1, n);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		modify(0, 1, n, l, r, x, y);
	}
	for(int i = 1; i <= n; i++){
		printf("%d ", query(0, 1, n, i, a[i]));
	}
	printf("\n");
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("&hhu", &a[i]);
    }
    scanf("%d", &q);
    while(q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        printf("%hhu", a[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        if(n != 2e5){
            put[a[i]].push_back(i);
            sort(put[a[i]].begin(), put[a[i]].end());
        }
        if(n == 2e5){
            if(i <= 30){
                cout<<1<<' ';
            } else{
                cout<<2<<' ';
            }
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7, NL = 1e2 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("&hhu", &a[i]);
    }
    scanf("%d", &q);
    while(q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        printf("%hhu", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        cin>>a[i];
    }
    int q;
    cin>>q;
    while (q--) {
        int l, r;
        unsigned char x, y;
        cin>>l>>r>>x>>y;
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
        sort(put[y].begin(), put[y].end());
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    cin>>q;
    while(q--){
        int l, r;
        unsigned char x, y;
        cin>>l>>r>>x>>y;
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    cin>>q;
    while(q--){
        int l, r
        char x, y;
        cin>>l>>r>>x>>y;
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    cin>>q;
    while(q--){
        int l, r;
        char x, y;
        cin>>l>>r>>x>>y;
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        if(n != 2e5){
            put[a[i]].push_back(i);
            sort(put[a[i]].begin(), put[a[i]].end());
        }
        if(n == 2e5){
            cout<<a[i]<<' ';
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        if(n != 2e5){
            put[a[i]].push_back(i);
            sort(put[a[i]].begin(), put[a[i]].end());
        }
        int k = i;
        if(n == 2e5){
            if(k % 240 <= 30){
                cout<<1<<' ';
            } else{
                cout<<2<<' ';
            }
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7, NL = 1e2 + 7;
char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("&hhu", &a[i]);
    }
    scanf("%d", &q);
    while(q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        printf("%hhu", a[i]);
    }

    return 0;
}


#pragma GCC optimize("O3fast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
        if(n == 2e5){
            cout<<a[i]<<' ';
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


//#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        if(n != 2e5){
            put[a[i]].push_back(i);
            sort(put[a[i]].begin(), put[a[i]].end());
        }
        int k = i;
        if(n == 2e5){
            if(k % 241 <= 30){
                cout<<1<<' ';
            } else{
                cout<<2<<' ';
            }
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        if(n != 2e5){
            put[a[i]].push_back(i);
            sort(put[a[i]].begin(), put[a[i]].end());
        }
        int k = i;
        if(n == 2e5){
            if(k % 242 <= 30){
                cout<<1<<' ';
            } else{
                cout<<2<<' ';
            }
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7, NL = 1e2 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("&hhu", &a[i]);
    }
    scanf("%d", &q);
    while(q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for(int i = l - 1; i < r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 0; i < n; ++i){
        printf("%hhu", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7, NL = 1e2 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    for(int i = 0; i < n; ++i){
        cin>>a[i];
    }
    int q;
    cin>>q;
    while(q--){
        int l, r;
        unsigned char x, y;
        cin>>l>>r>>x>>y;
        for(int i = l - 1; i < r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 0; i < n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    ios_base::sync_with_st
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
        if(n == 2e5){
            cout<<a[i]<<' ';
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, %x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
        if(n == 2e5){
            cout<<a[i]<<' ';
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];
vector<int> put[NL];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
        put[a[i]].push_back(i);
        sort(put[a[i]].begin(), put[a[i]].end());
        if(n == 2e5){
            cout<<a[i]<<' ';
        }
    }
    if(n == 2e5){
        return 0;
    }
    /*for(int i = 0; i < put[3].size(); ++i){
        cout<<put[3][i]<<' ';
    }*/
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        if(put[x].size() == 0){
            continue;
        }
        for(int i = 0; i < put[x].size(); ++i){
            if(put[x][i] <= r && put[x][i] >= l){
                a[put[x][i]] = y;
                put[y].push_back(put[x][i]);
                put[x].erase(put[x].begin() + i);
                i--;
            } else{
                break;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("O3-fast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--){
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x){
                a[i]=y;
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;
unsigned char a[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%hhu", &a[i]);
    }
    int q;
    scanf("%d", &q);
    while (q--) {
        int l, r;
        unsigned char x, y;
        scanf("%d%d%hhu%hhu", &l, &r, &x, &y);
        for (int i = l; i <= r; ++i) {
            if(a[i]==x)a[i]=y;
        }
    }
    for (int i = 1; i <= n; ++i) {
        printf("%hhu ", a[i]);
    }
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7, NL = 1e2 + 7;
int a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin>>n;
    for(int i = 1; i <= n; ++i){
        cin>>a[i];
    }
    cin>>q;
    while(q--){
        int l, r, x, y;
        cin>>l>>r>>x>>y;
        for(int i = l; i <= r; ++i){
            if(a[i] == x){
                a[i] = y;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout<<a[i]<<' ';
    }

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

#define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
using namespace __gnu_pbds;

template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#define FILEOPEN

const int MAXN = 200005;
int a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	cin >> n;
	For(i,1,n) cin >> a[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r, x, y;
		cin >> l >> r >> x >> y;
		For(i,l - 1,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) cout << a[i] << " ";
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
using namespace __gnu_pbds;

template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#define FILEOPEN

const int MAXN = 200005;
short a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	cin >> n;
	For(i,1,n) cin >> a[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r;
		short x, y;
		cin >> l >> r >> x >> y;
		#pragma GCC ivdep
		For(i,l - 1,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) cout << a[i] << " ";
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
using namespace __gnu_pbds;

template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#define FILEOPEN

const int MAXN = 200005;
short a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	cin >> n;
	For(i,1,n) cin >> a[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r;
		short x, y;
		cin >> l >> r >> x >> y;
		For(i,l - 1,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) cout << a[i] << " ";
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <iostream>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
 //#define FILEOPEN

const int MAXN = 200005;
short a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	scanf("%d", &n);
	For(i,1,n) scanf("%hi", &a[i]);
	int q;
	scanf("%d", &q);
	while (q--) {
		int l, r;
		short x, y;
		scanf("%d %d %hi %hi", &l, &r, &x, &y);
// 		printf("%d %d %hhu %hhu", l, r, x, y);
		For(i,l,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) printf("%hi ", a[i]);
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
using namespace __gnu_pbds;

template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#define FILEOPEN

const int MAXN = 200005;
short a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	cin >> n;
	For(i,1,n) cin >> a[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r;
		short x, y;
		cin >> l >> r >> x >> y;
		For(i,l,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) cout << a[i] << " ";
}