#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      Trie *nt = new Trie;
      nt->c = 0;
      s = t->s.emplace(std::make_pair(e, nt)).first;
    }
    t = s->second;
  }
  t->c += c;
}

long long int collect(Trie* t) {
  long long int g = t->c;
  for (const auto it: t->s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, new Trie)).first;
    }
    t = s->second;
  }
  t->c += c;
}

long long int collect(Trie* t) {
  long long int g = t->c;
  for (const auto it: t->s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, new Trie)).first;
    }
    t = s->second;
  }
  t->c += c;
}

long long int collect(Trie* t) {
  long long int g = t->c;
  for (const auto it: t->s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <unordered_map>
#include <vector>

struct Trie {
  std::unordered_map<int, Trie*> s;
  long long int c;

  Trie() : s(), c() {}
};

void insert(Trie* trie, const std::vector<int>& path, long long int c) {
  Trie* t = trie;
  for (const auto e: path) {
    auto s = t->s.find(e);
    if (s == t->s.end()) {
      s = t->s.emplace(std::make_pair(e, new Trie)).first;
    }
    t = s->second;
  }
  t->c += c;
}

long long int collect(Trie* t) {
  long long int g = t->c;
  for (const auto it: t->s) {
    g = std::gcd(g, collect(it.second));
  }
  return g;
}

void solve() {
  int n, m;
  std::cin >> n >> m;
  std::vector<long long int> C(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> C[i];
  }
  std::vector<std::vector<int>> E(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    std::cin >> u >> v;
    E[v-1].push_back(u-1);
  }
  Trie *t = new Trie;
  for (int i = 0; i < n; ++i) {
    std::sort(E[i].begin(), E[i].end());
    insert(t, E[i], C[i]);
  }
  std::cout << collect(t) << '\n';
}

int main() {
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0, tot = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	for (int i = 1; i <= n; ++i) {
		if (!G[1][i].empty())
			tot += c[i];
	}
	res = __gcd(res, tot);
	tot = 0;
	for (int i = 1; i <= n; ++i) {
		for (auto &v : G[0][i]) {
			if (!vis[v]) {
				tot += c[v];
				vis[v] = 1;
			}
		}
		res = __gcd(res, tot);
	}
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	ll tot = 0;
	memset(vis, 0, sizeof (vis[0]) * (n + 5));
	for (int i = 1; i <= n; ++i) {
		for (auto &v : G[0][i]) {
			if (!vis[v]) tot += c[v];
		}
	}
	memset(vis, 0, sizeof (vis[0]) * (n + 5));
	res = __gcd(res, tot);
	vector <int> vec;
	for (int i = 1; i <= n; ++i) vec.push_back(i);
	sort(vec.begin(), vec.end(), [&](int x, int y) { return SZ(G[0][x]) > SZ(G[0][y]); });
	for (int i = 0; i < n; ++i) {
		int u = vec[i];
		int rt = -1;
		for (auto &v : G[0][u]) {
			if (rt != -1 && rt != vis[v]) {
				res = 1;
				goto print;
			}
			rt = vis[v];
			if (!vis[v]) vis[v] = u;
		}
	}
print:
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	memset(vis, 0, sizeof (vis[0]) * (n + 5));
	vector <int> vec;
	for (int i = 1; i <= n; ++i) vec.push_back(i);
	sort(vec.begin(), vec.end(), [&](int x, int y) { return SZ(G[0][x]) > SZ(G[0][y]); });
	for (int i = 0; i < n; ++i) {
		int u = vec[i];
		int rt = -1;
		for (auto &v : G[0][u]) {
			if (rt != -1 && rt != vis[v]) {
				res = 1;
				goto print;
			}
			rt = vis[v];
			if (!vis[v]) vis[v] = u;
		}
	}
print:
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	vector <int> vec;
	for (int i = 1; i <= n; ++i) vec.push_back(i);
	ll pre = res;
	for (int t = 1; t <= 10; ++t) {
		mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
		shuffle(vec.begin(), vec.end(), mt);
		memset(vis, 0, sizeof (vis[0]) * (n + 5));
		ll tot = 0;
		for (int i = 0; i < n; ++i) {
			for (auto &v : G[0][vec[i]]) {
				if (!vis[v]) {
					vis[v] = 1;
					tot += c[v];
				}
			}
			res = __gcd(res, tot);
			if (res < pre) {
				res = 1;
				break;
			}
		}
	}
	if (res < pre) res = 1;
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	ll tot = 0;
	memset(vis, 0, sizeof (vis[0]) * (n + 5));
	for (int i = 1; i <= n; ++i) {
		for (auto &v : G[0][i]) {
			if (!vis[v]) {
				tot += c[v];
				vis[v] = 1;
			}
		}
	}
	res = __gcd(res, tot);
	memset(vis, 0, sizeof (vis[0]) * (n + 5));
	vector <int> vec;
	for (int i = 1; i <= n; ++i) vec.push_back(i);
	sort(vec.begin(), vec.end(), [&](int x, int y) { return SZ(G[0][x]) > SZ(G[0][y]); });
	for (int i = 0; i < n; ++i) {
		int u = vec[i];
		int rt = -1;
		for (auto &v : G[0][u]) {
			if (rt != -1 && rt != vis[v]) {
				res = 1;
				goto print;
			}
			rt = vis[v];
			if (!vis[v]) vis[v] = u;
		}
	}
print:
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N]; int vis[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]), vis[i] = 0;
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	vector <int> vec;
	for (int i = 1; i <= n; ++i) vec.push_back(i);
	ll pre = res;
	for (int t = 1; t <= 5; ++t) {
		mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
		shuffle(vec.begin(), vec.end(), mt);
		memset(vis, 0, sizeof (vis[0]) * (n + 5));
		ll tot = 0;
		for (int i = 0; i < n; ++i) {
			for (auto &v : G[0][vec[i]]) {
				if (!vis[v]) {
					vis[v] = 1;
					tot += c[v];
				}
			}
			res = __gcd(res, tot);
			if (res < pre) {
				res = 1;
				break;
			}
		}
	}
	if (res < pre) res = 1;
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll c[N];
vector <vector<int>> G[2];
void run() {
	rd(n, m);
	G[0].clear(); G[0].resize(n + 1);
	G[1].clear(); G[1].resize(n + 1);
	for (int i = 1; i <= n; ++i) rd(c[i]);
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		G[0][u].push_back(v);
		G[1][v].push_back(u);
	}
	ll res = 0, tot = 0;
	for (int i = 1; i <= n; ++i) {
		ll sum = 0;
		for (auto &v : G[0][i]) {
			sum += c[v];
		}
		res = __gcd(res, sum);
	}
	for (int i = 1; i <= n; ++i) {
		if (!G[1][i].empty())
			tot += c[i];
	}
	res = __gcd(res, tot);
	pt(res);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y, int Mod = mod) { x += y; while (x >= Mod) x -= Mod; while (x < 0) x += Mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int nextInt() { int x; cin >> x; return x; }
void rd() {}
template <class T, class... Ts> void rd(T& arg, Ts&... args) { cin >> arg; rd(args...); }
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template <class T, class... Ts> void err(const T& arg, const Ts&... args) { cout << arg << ' '; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) { for (auto &v : arg) cout << v << ' '; err(args...); }
void ptt() { cout << endl; }
template <class T, class... Ts> void ptt(const T& arg, const Ts&... args) { cout << ' ' << arg; ptt(args...); }
template <class T, class... Ts> void pt(const T& arg, const Ts&... args) { cout << arg; ptt(args...); }
void pt() {}
template <template<typename...> class T, typename t, typename... A>
void pt(const T <t> &arg, const A&... args) { for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1]; pt(args...); }
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
//head
constexpr int N = 5e5 + 10;
int n, m; ll a[N];
void run() {
	rd(n, m);
	map <set<int>, ll> mp;
	vector <set<int>> vec(n + 1);
	for (int i = 1; i <= n; ++i) rd(a[i]);
	for (int i = 1, u, v; i <= m; ++i) {
		rd(u, v);
		vec[v].insert(u);
	}
	for (int i = 1; i <= n; ++i) {
		mp[vec[i]] += a[i];
	}
	ll ans = 0;
	for (auto &it : mp) {
		if (!it.fi.empty())
			ans = __gcd(ans, it.se);
	}
	pt(ans);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	int _T = nextInt();
	while (_T--) run();
//	while (cin >> n) run();
//    for (int kase = 1; kase <= _T; ++kase) {
//        cout << "Case #" << kase << ":\n";
//        run();
//    }
	return 0;
}


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

int tc, n, m;
long long a[500005];
vector<vector<int>> adj;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}

		adj.assign(n, vector<int>());
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			--u; --v;
			adj[v].emplace_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 0; i < n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto &it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << '\n';
	}

	return 0;
}


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

int tc, n, m;
long long a[500005];
vector<vector<int>> adj;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}

		adj.assign(n + 1, vector<int>());
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			adj[v].push_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 1; i <= n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto &it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << '\n';
	}

	return 0;
}


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

int tc, n, m;
long long a[500005];
vector<vector<int>> adj;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}

		adj.assign(n, vector<int>());
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			--u; --v;
			adj[v].push_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 0; i < n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto &it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << '\n';
	}

	return 0;
}


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

int tc, n, m;
long long a[500005];
vector<vector<int>> adj;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}

		adj.assign(n + 1, vector<int>());
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			adj[v].push_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 1; i <= n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << '\n';
	}

	return 0;
}


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

int tc, n, m;
vector<long long> a;
vector<vector<int>> adj;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;

		a.resize(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}

		adj.assign(n, vector<int>());
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			--u; --v;
			adj[v].emplace_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 0; i < n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto &it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << '\n';
	}

	return 0;
}


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

int tc, n, m;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> tc;
	while (tc--) {
		cin >> n >> m;

		vector<long long> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}

		vector<vector<int>> adj(n);
		for (int i = 1; i <= m; i++) {
			int u, v;
			cin >> u >> v;
			--u; --v;
			adj[v].push_back(u);
		}

		map<vector<int>, long long> bagian;
		for (int i = 0; i < n; i++) {
			sort(adj[i].begin(), adj[i].end());
			bagian[adj[i]] += a[i];
		}

		long long g = 0;
		for (auto &it : bagian) {
			if (it.first.empty()) continue;
			g = __gcd(g, it.second);
		}

		cout << g << "\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        unordered_map<vector<int>, long long> M;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                M[Q] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];
map<pii, long long> M;
pair<pii, long long> G[MAXN];
pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        M.clear();

        rep(i, n)
        {
            cin >> G[i].nd;
            G[i].st = {0, 0};
        }
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            --b;
            G[b].st = acc(G[b].st, P[a]);
        }

        long long sum = 0;
        rep(i, n)
        {
            auto Q = G[i];
            if (Q.st != make_pair(0, 0))
            {
                sum += Q.nd;
                M[Q.st] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        printf("%Ld\n", sum);
        M.clear();
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl '\n'
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];
map<pii, long long> M;
pair<pii, long long> G[MAXN];
pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        M.clear();

        rep(i, n)
        {
            cin >> G[i].nd;
            G[i].st = {0, 0};
        }
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            --b;
            G[b].st = acc(G[b].st, P[a]);
        }

        long long sum = 0;
        rep(i, n)
        {
            auto Q = G[i];
            if (Q.st != make_pair(0, 0))
            {
                sum += Q.nd;
                M[Q.st] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
        M.clear();
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];

pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<pii, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            --b;
            G[b].st = acc(G[b].st, P[a]);
        }

        map<pii, long long> M;
        long long sum = 0;
        for (auto Q : G)
        {
            if (Q.st != make_pair(0, 0))
            {
                sum += Q.nd;
                M[Q.st] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
#define int long long

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        vector<pair<vector<int>, long long>> Gp;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                Gp.push_back(Q);
            }
        }
        G = Gp;

        sort(all(G));

        vector<long long> X;
        vector<int> cur;
        long long val = sum;

        for (auto &Q : G)
            if (Q.st == cur)
            {
                val += Q.nd;
            }
            else
            {
                X.push_back(val);
                cur = Q.st;
                val = Q.nd;
            }
        X.push_back(val);

        for (auto x : X)
        {
            // what(x);
            sum = gcd(sum, x);
        }
        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];

pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        map<pii, long long> M;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                M[hasz(Q.st)] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
#define int long long

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        vector<pair<vector<int>, long long>> Gp;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                Gp.push_back(Q);
            }
        }
        G = Gp;

        sort(all(G));

        vector<long long> X;
        vector<int> cur;
        long long val = sum;

        for (auto &Q : G)
            if (Q.st == cur)
            {
                val += Q.nd;
            }
            else
            {
                X.push_back(val);
                cur = Q.st;
                val = Q.nd;
            }
        X.push_back(val);

        for (auto x : X)
        {
            // what(x);
            sum = gcd(sum, x);
        }
        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        vector<pair<vector<int>, long long>> Gp;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                Gp.push_back(Q);
            }
        }
        G = Gp;

        sort(all(G));

        vector<long long> X;
        vector<int> cur;
        long long val = sum;

        for (auto &Q : G)
            if (Q.st == cur)
            {
                val += Q.nd;
            }
            else
            {
                X.push_back(val);
                cur = Q.st;
                val = Q.nd;
            }
        X.push_back(val);

        for (auto x : X)
        {
            // what(x);
            sum = gcd(sum, x);
        }
        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];

pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        unordered_map<pii, long long> M;
        long long sum = 0;
        for (auto Q : G)
        {
            sort(all(Q.st));
            if (Q.st.size())
            {
                sum += Q.nd;
                M[hasz(Q.st)] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
    }
}


#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10) //cin.tie(0); cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define what(x) cerr << #x << " is " << x << endl;
//#define int long long

const int MAXN = 1e6;

const int MOD1 = 1e9 + 7;
int MOD2 = 1e9 + 9;

pair<int, int> P[MAXN + 10];

pii acc(pii A, pii B)
{
    return {(A.st + B.st) % MOD1, (A.nd + B.nd) % MOD2};
}
pii hasz(vector<int> &V)
{
    pii wynik = {0, 0};
    for (auto i : V)
        wynik = acc(wynik, P[i]);
    return wynik;
}

void pre()
{
    P[0] = {1, 1};
    rep(i, MAXN)
        P[i + 1] = acc(P[i], P[i]);
}

main()
{
    _upgrade;
    int Z;
    cin >> Z;
    pre();
    while (Z--)
    {
        int n, m;
        cin >> n >> m;
        // vector<long long> val(n);
        vector<pair<vector<int>, long long>> G(n);

        rep(i, n) cin >> G[i].nd;
        rep(i, m)
        {
            int a, b;
            cin >> a >> b;
            // G[--a].st.push_back(--b);
            G[--b].st.push_back(--a);
        }

        map<pii, long long> M;
        long long sum = 0;
        for (auto Q : G)
        {
            if (Q.st.size())
            {
                sum += Q.nd;
                M[hasz(Q.st)] += Q.nd;
            }
        }

        for (auto &x : M)
            sum = gcd(sum, x.nd);

        cout << sum << endl;
    }
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Corvus
// ~/sudo apt-get verdict Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 500001;
const ll MOD = 1e9 + 7;
const double eps = 1e-9;

int n, s, a[N], f[N], ans[N], sd[N];
bool in[N];

struct query {
	int l, r, i;
	query() {}
	query(int l, int r, int i) {
		this->l = l;
		this->r = r;
		this->i = i;
	}
	bool operator<(const query &o) const {
		if (l / s != o.l / s)
			return l < o.l;
		return ((l/s) & 1  ? r > o.r : r < o.r);
	}
} Q[N];

inline void add(int x) {
	f[x]++;
	if (f[x] == 1)
		sd[x / s]++;
	else if (f[x] == 2)
		sd[x / s]--;
}

inline void rem(int x) {
	f[x]--;
	if (f[x] == 0)
		sd[x / s]--;
	else if (f[x] == 1)
		sd[x / s]++;
}

int get() {
	for(int i=0; i<=s; i++)
		if (sd[i]) {
			for (int j = 0; j < s; j++)
				if (f[i*s + j] == 1)
					return i * s + j;
		}
	return 0;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
#endif

	srand(time(NULL));

	scanf("%d", &n);
	s = (int)sqrt(n);
	for (int i = 1; i <= n; i++)
		scanf("%d", a + i);

	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		scanf("%d%d", &Q[i].l, &Q[i].r);
		Q[i].i = i;
	}
	sort(Q, Q + q);

	int L = 1, R = 0;
	for (int i = 0; i < q; i++) {
		int l = Q[i].l, r = Q[i].r;
		while (R < r) add(a[++R]);
		while (R > r) rem(a[R--]);
		while (L < l) rem(a[L++]);
		while (L > l) add(a[--L]);
		ans[Q[i].i] = get();
	}
	for (int i = 0; i < q; i++)
		printf("%d\n",ans[i]);
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/Rampage/Corvus
// ~/sudo apt-get verdict Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int N = 500000;
const ll MOD = 1e9 + 7;
const double eps = 1e-9;

int n, a[N+1], mx[N+1], last[N+1], ans[N], seg[4*N];

struct query {
	int l, r, i;
	query() {}
	query(int l, int r, int i) {
		this->l = l;
		this->r = r;
		this->i = i;
	}
};
vector<query> at[N + 1];

void update(int p, int s, int e, int i, int v) {
	if (s == e) {
		seg[p] = v;
		return;
	}
	if (i <= (s + e) / 2) update(2 * p, s, (s + e) / 2, i, v);
	else update(2 * p + 1, (s + e) / 2 + 1, e, i, v);
	seg[p] = min(seg[2 * p], seg[2 * p + 1]);
}

pair<int,int> get(int p, int s, int e, int l, int r) {
	if (s >= l && e <= r)
		return make_pair(seg[p], s);
	if (s > r || e < l)
		return make_pair(1e9, -1);
	pair<int,int> a = get(2 * p, s, (s + e) / 2, l, r);
	pair<int,int> b = get(2 * p + 1, (s + e) / 2 + 1, e, l, r);
	return a.first < b.first ? a : b;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", a + i);
	for (int i = 0; i < 4 * N; i++)
		seg[i] = 1e9;

	int q, l, r;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		scanf("%d%d", &l, &r);
		at[r].push_back(query(l, r, i));
	}
	memset(last, -1, sizeof last);
	memset(mx, -1, sizeof mx);
	for (int i = 1; i <= n; i++) {
		mx[i] = last[a[i]];
		update(1, 1, N, last[a[i]], 1e9);
		last[a[i]] = i;
		update(1, 1, N, i, mx[i]);
		for (int j = 0; j < at[i].size(); j++) {
			pair<int,int> res = get(1, 1, N, at[i][j].l, i);
			if (res.first < at[i][j].l)
				ans[at[i][j].i] = a[res.second];
		}
	}
	for (int i = 0; i < q; i++)
		printf("%d\n", ans[i]);
	return 0;
}


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

int N, Q;
int answers[500500];
int BLOCK_SIZE = 800;
int arr[500500];
pair< pair<int, int>, int> queries[500500];
int cnt[500500], total;
int blk[800];

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i <= BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    //BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
	   	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = get();
    }

    for(int i = 0; i < Q; i++){
    	printf("%d\n", answers[i]);
	}
    return 0;
}


//https://www.hackerearth.com/practice/notes/mos-algorithm/
#include <bits/stdc++.h>
using namespace std;

int N, Q;
long long current_answer;
long long cnt[100];
long long answers[100500];
int BLOCK_SIZE;
int arr[100500];
pair< pair<int, int>, int> queries[100500];
map<int, int> mp;
set<int> st;

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	if(mp[x] == 0){
		st.insert(x);
    	current_answer++;
    }
	if(mp[x] == 1){
		st.erase(x);
    	current_answer--;
    }
    mp[x]++;
}

void remove(int x){
	if(mp[x] == 2){
		st.insert(x);
		current_answer++;
	}
	if(mp[x] == 2){
		st.erase(x);
		current_answer--;
	}
	mp[x]--;
}

int main()
{
    cin.sync_with_stdio(false);
    scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
    	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = 0;
        if(st.size() > 0){
	        answers[queries[i].second] = *st.begin();
		}
    }

    for(int i = 0; i < Q; i++){
    	printf("%lld\n", answers[i]);
	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define lli long long int
#define test()  int test;cin>>test;while(test--)
const lli MOD = 1000000007ll;
const int mxsz = 10055;
using namespace std;

vector<int> seg[40005];
vector<int> pr;
vector<int> vt[10005];

lli powermod(lli a, lli b, lli m){
	lli r = 1;
	while(b){
		if(b % 2LL == 1LL)
			r = ((r % m) * (a % m)) % m;
		b >>= 1;
		a = ((a % m) * (a % m)) % m;
	}
	return r;
}

void build(int l, int r, int node){
	if(l == r){
		seg[node] = vt[l];
	}
	int mid = (l + r) / 2;
	build(l, mid, 2 * node);
	build(mid + 1, r, 2 * node + 1);
	int i = 0, j = 0;
	while(i < seg[2 * node].size() && j < seg[2 * node + 1].size()){
		if(seg[2 * node][i] < seg[2 * node + 1][j]){
			seg[node].push_back(seg[2 * node][i]);
			i++;
		}
		else{
			seg[node].push_back(seg[2 * node + 1][j]);
			j++;
		}
	}
	while(i < seg[2 * node].size()){
		seg[node].push_back(seg[2 *node][i]);
		i++;
	}
	while(j < seg[2 * node + 1].size()){
		seg[node].push_back(seg[2 *node + 1][j]);
		j++;
	}
}

int query(int start, int end, int l, int r, int x, int y, int node){
	if(start > r || l > end){
		return 0;
	}
	if(start >= l && end <= r){
		return upper_bound(seg[node].begin(), seg[node].end(), y) - lower_bound(seg[node].begin(), seg[node].end(), x);
	}
	int mid = (start + end) / 2;
	return query(start, mid, l, r, x, y, 2 * node) +
	query(mid + 1, end, l, r, x, y, 2 * node + 1);
}

vector<int> adj[mxsz];
int vis[mxsz];

void sieve(){
	pr.push_back(1);
	for(lli i = 2; i < mxsz; i++){
		if(vis[i] == false){
			pr.push_back(i);
			for(lli j = i*2LL; j < mxsz; j += i){
				vis[j] = true;
				adj[j].push_back(i);
			}
			adj[i].push_back(i);
		}
	}
	pr.push_back(1e9 + 7);
}

int main()
{
	sieve();
	int n;
    scanf("%d", &n);
    int ar[n + 1] = {};
    for(int i = 1; i <= n; i++){
    	scanf("%d", &ar[i]);
	}
	for(int i = 1; i <= n; i++){
		vector<int> tp;
		for(int j = 0; j < adj[ar[i]].size(); j++){
			int count = 0;
			int num = ar[i];
			while(num % adj[ar[i]][j] == 0){
				count++;
			}
			for(int k = 1; k <= count; k++){
				vt[i].push_back(adj[ar[i]][j]);
			}
		}
	}
	//build(1, n, 1);
	int q;
	scanf("%d", &q);
	while(q--){
		int l, r, x, y;
		scanf("%d %d %d %d", &l, &r, &x, &y);
		x = *upper_bound(pr.begin(), pr.end(), x);
		y = *(upper_bound(pr.begin(), pr.end(), x)--);
		printf("%d\n", query(1, n, l, r, x, y, 1));
	}
	return 0;
}


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

int N, Q;
long long answers[500500];
int BLOCK_SIZE = 800;
int arr[500500];
pair< pair<int, int>, int> queries[500500];
int cnt[500500], total;
int blk[800];

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i <= BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    //BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
	   	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = get();
    }

    for(int i = 0; i < Q; i++){
    	printf("%lld\n", answers[i]);
	}
    return 0;
}


//https://www.hackerearth.com/practice/notes/mos-algorithm/
#include <bits/stdc++.h>
using namespace std;

int N, Q;
long long current_answer;
long long cnt[100];
long long answers[500500];
int BLOCK_SIZE;
int arr[500500];
pair< pair<int, int>, int> queries[500500];
map<int, int> mp;
set<int> st;

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	if(mp[x] == 0){
		st.insert(x);
    }
	if(mp[x] == 1){
		st.erase(x);
    }
    mp[x]++;
}

void remove(int x){
	if(mp[x] == 2){
		st.insert(x);
	}
	if(mp[x] == 1){
		st.erase(x);
	}
	mp[x]--;
}

int main()
{
    cin.sync_with_stdio(false);
    cin >> N;
//    scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
    	cin >> arr[i];
//        scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
    	cin >> queries[i].first.first >> queries[i].first.second;
//    	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = 0;
        if(st.size() > 0){
	        answers[queries[i].second] = *st.begin();
		}
    }

    for(int i = 0; i < Q; i++){
    	cout << answers[i] << "\n";
//    	printf("%lld\n", answers[i]);
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); i++)
#define x first
#define y second

const int N = 500 * 1000 + 13;
const int P = 800;
const int INF = 1e9;

bool comp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b){
	int num = a.x.x / P;
	if (num != b.x.x / P)
		return a.x.x < b.x.x;
	if (num & 1)
		return a.x.y < b.x.y;
	return a.x.y > b.x.y;
}

int val[N];
int cnt[N];
int bl[P];
int tot;

inline void add(int x){
	++cnt[x];
	if (cnt[x] == 1){
		++val[x];
		++bl[x / P];
		++tot;
	}
	else if (cnt[x] == 2){
		--val[x];
		--bl[x / P];
		--tot;
	}
}

inline void sub(int x){
	--cnt[x];
	if (cnt[x] == 1){
		++val[x];
		++bl[x / P];
		++tot;
	}
	else if (cnt[x] == 0){
		--val[x];
		--bl[x / P];
		--tot;
	}
}

int get(){
	if (tot == 0)
		return 0;

	forn(i, P){
		if (bl[i] > 0){
			for (int j = i * P;; ++j){
				if (val[j]){
					return j;
				}
			}
		}
	}

	assert(false);
}

int n, m;
int a[N], ans[N];
pair<pair<int, int>, int> q[N];

int main() {
	scanf("%d", &n);
	forn(i, n) scanf("%d", &a[i]);
	scanf("%d", &m);
	forn(i, m){
		scanf("%d%d", &q[i].x.x, &q[i].x.y);
		--q[i].x.x, --q[i].x.y;
		q[i].y = i;
	}
	sort(q, q + m, comp);

	int l = 0, r = -1;

	forn(i, m){
		int L = q[i].x.x;
		int R = q[i].x.y;

		while (r < R){
			++r;
			add(a[r]);
		}

		while (r > R){
			sub(a[r]);
			--r;
		}

		while (l > L){
			--l;
			add(a[l]);
		}

		while (l < L){
			sub(a[l]);
			++l;
		}

		ans[q[i].y] = get();
	}

	forn(i, m)
		printf("%d\n", ans[i]);

	return 0;
}


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

int N, Q;
long long answers[500500];
int BLOCK_SIZE;
int arr[500500];
pair< pair<int, int>, int> queries[500500];
map<int, int> mp;
set<int> st;
int cnt[500500], vis[500500], total;
int blk[800];

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i <= BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
	   	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = get();
    }

    for(int i = 0; i < Q; i++){
    	printf("%lld\n", answers[i]);
	}
    return 0;
}


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

int N, Q;
int answers[mxsz], BLOCK_SIZE = 800, arr[mxsz], cnt[mxsz], total = 0, blk[800];
pair< pair<int, int>, int> queries[500500];

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}

inline void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i <= BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
	   	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = get();
    }

    for(int i = 0; i < Q; i++){
    	printf("%d\n", answers[i]);
	}
    return 0;
}


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

int N, Qp;
int answers[mxsz], BLOCK_SIZE = 800, arr[mxsz], cnt[mxsz], total = 0, blk[800];
//pair< pair<int, int>, int> queries[500500];
struct str {int l,r,i;} Q[mxsz];
bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}

inline void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i < BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Qp;
    for(int i = 1; i <= Qp; i++) {
    	int l, r;
    	scanf("%d %d", &l, &r);
	   //	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
	   	Q[i] = {l, r, i};
       // queries[i].second = i;
    }

    //sort(queries, queries + Q, mo_cmp);
	sort(Q+1, Q+1+Qp, [](str &f, str &s){
       if(f.l / BLOCK_SIZE != s.l / BLOCK_SIZE) return f.l < s.l;
       if((f.l / BLOCK_SIZE)&1) return f.r < s.r;
       return f.r > s.r;
    });

    int mo_left = 1, mo_right = 0;

    for(int i = 1; i <= Qp; i++) {
        int left = Q[i].l;
        int right = Q[i].r;
        //cout << left << " " << right << "\n";
        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[Q[i].i] = get();
    }

    for(int i = 1; i <= Qp; i++){
    	printf("%d\n", answers[i]);
	}
    return 0;
}


//https://www.hackerearth.com/practice/notes/mos-algorithm/
#include <bits/stdc++.h>
using namespace std;

int N, Q;
long long current_answer;
long long cnt[100];
long long answers[100500];
int BLOCK_SIZE;
int arr[100500];
pair< pair<int, int>, int> queries[100500];
map<int, int> mp;
set<int> st;

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	if(mp[x] == 0){
		st.insert(x);
    	current_answer++;
    }
	if(mp[x] == 1){
		st.erase(x);
    	current_answer--;
    }
    mp[x]++;
}

void remove(int x){
	if(mp[x] == 2){
		st.insert(x);
		current_answer++;
	}
	if(mp[x] == 2){
		st.erase(x);
		current_answer--;
	}
	mp[x]--;
}

int main()
{
    cin.sync_with_stdio(false);
    cin >> N;
//    scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
    	cin >> arr[i];
//        scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
    	cin >> queries[i].first.first >> queries[i].first.second;
//    	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = 0;
        if(st.size() > 0){
	        answers[queries[i].second] = *st.begin();
		}
    }

    for(int i = 0; i < Q; i++){
    	cout << answers[i] << "\n";
//    	printf("%lld\n", answers[i]);
	}
    return 0;
}


//https://www.hackerearth.com/practice/notes/mos-algorithm/
#include <bits/stdc++.h>
using namespace std;

int N, Q;
long long current_answer;
long long cnt[100];
long long answers[100500];
int BLOCK_SIZE;
int arr[100500];
pair< pair<int, int>, int> queries[100500];
map<int, int> mp;
set<int> st;

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	if(mp[x] == 0){
		st.insert(x);
    }
	if(mp[x] == 1){
		st.erase(x);
    }
    mp[x]++;
}

void remove(int x){
	if(mp[x] == 2){
		st.insert(x);
	}
	if(mp[x] == 1){
		st.erase(x);
	}
	mp[x]--;
}

int main()
{
    cin.sync_with_stdio(false);
    cin >> N;
//    scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
    	cin >> arr[i];
//        scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
    	cin >> queries[i].first.first >> queries[i].first.second;
//    	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = 0;
        if(st.size() > 0){
	        answers[queries[i].second] = *st.begin();
		}
    }

    for(int i = 0; i < Q; i++){
    	cout << answers[i] << "\n";
//    	printf("%lld\n", answers[i]);
	}
    return 0;
}


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

int N, Qp;
int answers[mxsz], BLOCK_SIZE = 800, arr[mxsz], cnt[mxsz], total = 0, blk[800];
struct str {int l,r,i;} Q[mxsz];
bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}

inline void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i < BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Qp;
    for(int i = 1; i <= Qp; i++) {
    	int l, r;
    	scanf("%d %d", &l, &r);
	   	Q[i] = {l, r, i};;
    }

	sort(Q+1, Q+1+Qp, [](str &f, str &s){
       if(f.l / BLOCK_SIZE != s.l / BLOCK_SIZE) return f.l < s.l;
       if((f.l / BLOCK_SIZE)&1) return f.r < s.r;
       return f.r > s.r;
    });

    int mo_left = 1, mo_right = 0;

    for(int i = 1; i <= Qp; i++) {
        int left = Q[i].l;
        int right = Q[i].r;
        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[Q[i].i] = get();
    }

    for(int i = 1; i <= Qp; i++){
    	printf("%d\n", answers[i]);
	}
    return 0;
}


//https://www.hackerearth.com/practice/notes/mos-algorithm/
#include <bits/stdc++.h>
using namespace std;

int N, Q;
long long current_answer;
long long cnt[100];
long long answers[500500];
int BLOCK_SIZE;
int arr[500500];
pair< pair<int, int>, int> queries[500500];
map<int, int> mp;
set<int> st;

bool mo_cmp(const pair< pair<int, int>, int> &x, const pair< pair<int, int>, int> &y){
    int block_x = x.first.first / BLOCK_SIZE;
    int block_y = y.first.first / BLOCK_SIZE;
    if(block_x != block_y)
        return block_x < block_y;
    return x.first.second < y.first.second;
}
void add(int x){
	if(mp[x] == 0){
		st.insert(x);
    }
	if(mp[x] == 1){
		st.erase(x);
    }
    mp[x]++;
}

void remove(int x){
	if(mp[x] == 2){
		st.insert(x);
	}
	if(mp[x] == 1){
		st.erase(x);
	}
	mp[x]--;
}

int main()
{
 //   cin.sync_with_stdio(false);
   // cin >> N;
    scanf("%d", &N);
    BLOCK_SIZE = static_cast<int>(sqrt(N));

    for(int i = 1; i <= N; i++){
   // 	cin >> arr[i];
       scanf("%d", &arr[i]);
        mp[arr[i]] = 0;
    }

    cin >> Q;
    for(int i = 0; i < Q; i++) {
  //  	cin >> queries[i].first.first >> queries[i].first.second;
   	scanf("%d%d", &queries[i].first.first, &queries[i].first.second);
        queries[i].second = i;
    }

    sort(queries, queries + Q, mo_cmp);

    int mo_left = 1, mo_right = 0;

    for(int i = 0; i < Q; i++) {
        int left = queries[i].first.first;
        int right = queries[i].first.second;

        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[queries[i].second] = 0;
        if(st.size() > 0){
	        answers[queries[i].second] = *st.begin();
		}
    }

    for(int i = 0; i < Q; i++){
    //	cout << answers[i] << "\n";
    	printf("%lld\n", answers[i]);
	}
    return 0;
}


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

int N, Qp;
int answers[mxsz], BLOCK_SIZE = 800, arr[mxsz], cnt[mxsz], total = 0, blk[800];
struct str {int l,r,i;} Q[mxsz];

inline void add(int x){
	cnt[x]++;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
    }
	if(cnt[x] == 2){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline void remove(int x){
	cnt[x]--;
	if(cnt[x] == 1){
		blk[x/BLOCK_SIZE]++;
		total++;
	}
	if(cnt[x] == 0){
		blk[x/BLOCK_SIZE]--;
		total--;
    }
}

inline int get(){
	if(total == 0){
		return 0;
	}
	for(int i = 0; i < BLOCK_SIZE; i++){
		if(blk[i] > 0){
			for(int j = i * BLOCK_SIZE;;j++){
				if(cnt[j] == 1){
					return j;
				}
			}
		}
	}
}
int main()
{
	scanf("%d", &N);
    for(int i = 1; i <= N; i++){
        scanf("%d", &arr[i]);
    }

    cin >> Qp;
    for(int i = 1; i <= Qp; i++) {
    	int l, r;
    	scanf("%d %d", &l, &r);
	   	Q[i] = {l, r, i};;
    }

	sort(Q+1, Q+1+Qp, [](str &f, str &s){
       if(f.l / BLOCK_SIZE != s.l / BLOCK_SIZE) return f.l < s.l;
       if((f.l / BLOCK_SIZE)&1) return f.r < s.r;
       return f.r > s.r;
    });

    int mo_left = 1, mo_right = 0;

    for(int i = 1; i <= Qp; i++) {
        int left = Q[i].l;
        int right = Q[i].r;
        while(mo_right < right) {
            mo_right++;
            add(arr[mo_right]);
        }
        while(mo_right > right) {
            remove(arr[mo_right]);
            mo_right--;
        }

        while(mo_left < left) {
            remove(arr[mo_left]);
            mo_left++;
        }
        while(mo_left > left) {
            mo_left--;
            add(arr[mo_left]);
        }
        answers[Q[i].i] = get();
    }

    for(int i = 1; i <= Qp; i++){
    	printf("%d\n", answers[i]);
	}
    return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (8 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (16 * N_MAX)
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (8 * N_MAX)
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
#pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n, t_time;
int in[BUFF(N_MAX)];
struct N {
  int l, r, accessed_at;
  pii val;
  N() {}
  N(int l, int r, pii val, int a_time) : l(l), r(r), val(val), accessed_at(a_time) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = (nodes[prev].accessed_at == t_time) ? prev : ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx }, t_time);
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right), t_time);
    }
    return cur;
  }
  return prev;
}
void queryAns(int cur, int sl, int sr, int l, int r, pii &best) {
  if (!cur || sr < l || r < sl || best <= nodes[cur].val)
    return;
  if (sl <= l && r <= sr)
    best = min(best, nodes[cur].val);
  else {
    int mid = getMid(l, r);
    queryAns(nodes[cur].l, sl, sr, l, mid, best);
    queryAns(nodes[cur].r, sl, r, mid + 1, r, best);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i, ++t_time) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = { INF, 0 };
    queryAns(roots[r], l, r, 1, n, ret);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (16 * N_MAX)
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r, bool overwrite = false) {
  if (l <= idx && idx <= r) {
    int cur = overwrite ? prev : ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    return nodes[cur].val;
  else {
    int mid = getMid(l, r);
    pii x = queryAns(nodes[cur].l, sl, sr, l, mid);
    pii y = queryAns(nodes[cur].r, sl, r, mid + 1, r);
    return min(x, y);
  }
}
void program() {
  cin >> n;
  mii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n, true);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  pii ret;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    ret = queryAns(roots[r], l, r, 1, n);
    if (ret.first < l)
      cout << in[ret.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) 5e5
#define VAL_MAX (int) 5e5
#define NODES_MAX (int) (8 * N_MAX)
#define INF (N_MAX + 1)
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
int n;
struct {
  int val, prev; // val -> raw value provided in input, prev -> value it represents in the tree
} positions[BUFF(N_MAX)];
int t_prev[BUFF(VAL_MAX)];
struct N {
  N() {}
  N(int l, int r, int min_v, int min_idx) : l(l), r(r), min_v(min_v), min_idx(min_idx) {}
  int l, r, min_v, min_idx;
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int pos, int val, int l, int r) {
  if (l <= pos && pos <= r) {
    int cur = ++nodes_ptr;
    if (l == r) {
      nodes[cur].min_v = val;
      nodes[cur].min_idx = pos;
    } else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, pos, val, l, mid);
      int b = buildTree(nodes[prev].r, pos, val, mid + 1, r);
      int a_val = (a) ? nodes[a].min_v : INF;
      int b_val = (b) ? nodes[b].min_v : INF;
      int min_val, min_idx;
      if (a_val < b_val)
        min_val = a_val, min_idx = nodes[a].min_idx;
      else min_val = b_val, min_idx = nodes[b].min_idx;
      nodes[cur] = N(a, b, min_val, min_idx);
    }
    return cur;
  }
  return prev;
}
int queryLowestIndex(int cur, int t_l, int t_r, int l, int r) {
  if (!cur || t_r < l || r < t_l)
    return INF;
  else if (t_l <= l && r <= t_r)
    return nodes[cur].min_idx;
  else {
    int mid = getMid(l, r);
    int a = queryLowestIndex(nodes[cur].l, t_l, t_r, l, mid);
    int b = queryLowestIndex(nodes[cur].r, t_l, t_r, mid + 1, r);
    return (positions[a].prev < positions[b].prev) ? a : b;
  }
}
int getOneOccurrenceNumber(int l, int r) {
  int lowest = queryLowestIndex(roots[r], l, r, 1, n);
  if (positions[lowest].prev == lowest || positions[lowest].prev < l)
    return true;
  return false;
}
void program() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &positions[i].val);
    roots[i] = roots[i - 1];
    if (t_prev[positions[i].val])
      roots[i] = buildTree(roots[i], t_prev[positions[i].val], INF, 1, n);
    positions[i].prev = (t_prev[positions[i].val]) ? t_prev[positions[i].val] : i;
    roots[i] = buildTree(roots[i], i, positions[i].prev, 1, n);
    t_prev[positions[i].val] = i;
  }
  int q;
  scanf("%d", &q);
  for (int i = 1, l, r; i <= q; ++i) {
    scanf("%d %d", &l, &r);
    printf("%d\n", getOneOccurrenceNumber(l, r));
  }
}
void ready() { positions[INF].prev = INF; }
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ready();
  program();
  return 0;
}


// NA
#include <bits/stdc++.h>
#define BUFFER 5
#define BUFF(x) x + BUFFER
#define N_MAX (int) (5e5)
#define NODES_MAX (int) (4 * N_MAX * log2(N_MAX))
#define mp make_pair
#define INF (int) 1e9
// #pragma GCC optimize "O4"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unordered_map<int, int> umii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
int n;
int in[BUFF(N_MAX)];
pii best;
struct N {
  int l, r;
  pii val;
  N() {}
  N(int l, int r, pii val) : l(l), r(r), val(val) {}
} nodes[BUFF(NODES_MAX)];
int nodes_ptr;
int roots[BUFF(N_MAX)];
int getMid(int l, int r) { return l + (r - l) / 2; }
int buildTree(int prev, int val, int idx, int l, int r) {
  if (l <= idx && idx <= r) {
    int cur = ++nodes_ptr;
    if (l == r)
      nodes[cur] = N(0, 0, { val, idx });
    else {
      int mid = getMid(l, r);
      int a = buildTree(nodes[prev].l, val, idx, l, mid);
      int b = buildTree(nodes[prev].r, val, idx, mid + 1, r);
      pii left = (a) ? nodes[a].val : mp(INF, 0);
      pii right = (b) ? nodes[b].val : mp(INF, 0);
      nodes[cur] = N(a, b, min(left, right));
    }
    return cur;
  }
  return prev;
}
pii queryAns(int cur, int sl, int sr, int l, int r) {
  if (!cur || sr < l || r < sl)
    return { INF, 0 };
  if (sl <= l && r <= sr)
    best = min(best, nodes[cur].val);
  else {
    int mid = getMid(l, r);
    queryAns(nodes[cur].l, sl, sr, l, mid);
    queryAns(nodes[cur].r, sl, r, mid + 1, r);
  }
}
void program() {
  cin >> n;
  umii mp;
  for (int i = 1, last_seen; i <= n; ++i) {
    roots[i] = roots[i - 1];
    cin >> in[i];
    last_seen = mp[in[i]] ? mp[in[i]] : -1;
    if (last_seen != -1)
      roots[i] = buildTree(roots[i], INF, last_seen, 1, n);
    roots[i] = buildTree(roots[i], last_seen, i, 1, n);
    mp[in[i]] = i;
  }
  int m;
  cin >> m;
  for (int i = 1, l, r; i <= m; ++i) {
    cin >> l >> r;
    best = { INF, 0 };
    queryAns(roots[r], l, r, 1, n);
    if (best.first < l)
      cout << in[best.second] << endl;
    else cout << "0" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  program();
  return 0;
}