#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    int num1, num2, divisor;
    int n,up,down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    divisor = min(num1,num2);
    while (divisor>1)
    {
        if ((num1%divisor == 0) && (num2%divisor == 0))
            v_gcd.push_back(divisor);
        divisor--;
    }

    cin >> n;
    for (unsigned i = 0; i < n; i++)
    {
        bool f = false;
        cin >> down >> up;
        for (unsigned j = 0; j < v_gcd.size(); j++)
        {
            if (v_gcd[j] >= down && v_gcd[j] <= up)
            {
                cout << v_gcd[j] << endl; f = true;
                break;
            }
        }
        if (!f)cout << "-1" << endl;
    }
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main()
{
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    tmp = gcd(num1, num2);

    cin >> n;
    for (unsigned i = 0; i < n; i++)
    {
        bool f = false;
        cin >> down >> up;
        up = min(up, tmp);
        if (up < down) { f = true; }
        for (unsigned j = up; j >= down && !f; j--)
        {
            if (tmp % j == 0)
            {
                cout << j << endl; f = true; break;
            }
        }
        if (!f)cout << "-1" << endl;
    }
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include<stdio.h>
#include<iostream>
using namespace std;
int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main()
{
    long long int num1, num2, GCD;
    long long int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    GCD = gcd(num1, num2);
    long long int tmp = sqrt(GCD);
    for (unsigned i = 1; i <= tmp; i++)
    {
        if (GCD%i == 0)
        {
            v_gcd.push_back(i); v_gcd.push_back(GCD / i);
        }
    }
    sort(v_gcd.begin(), v_gcd.end());
    cin >> n;
    for (unsigned i = 0; i < n; i++)
    {
        cin >> down >> up;
        int tmp2 = upper_bound(v_gcd.begin(), v_gcd.end(), up) - v_gcd.begin() - 1;
        if (down <= v_gcd[tmp2])cout << v_gcd[tmp2] << endl;
        else cout << "-1" << endl;
    }
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int gcd(int u, int v) {
    return (v != 0) ? gcd(v, u%v) : u;
}
int main()
{
    int num1, num2, tmp;
    int n, up, down;
    cin >> num1 >> num2;
    vector<long long int> v_gcd;
    tmp = gcd(num1, num2);

    cin >> n;
    for (unsigned i = 0; i < n; i++)
    {
        bool f = false;
        cin >> down >> up;
        up = min(up, tmp);

        for (unsigned j = up; j >= down ; j--)
        {
            if (tmp % j == 0)
            {
                cout << j << endl; f = true; break;
            }
        }
        if (!f)cout << "-1" << endl;
    }
}


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

int gcd(int a, int b) {
    if(!b)  return a;
    return gcd(b, a % b);
}
int a, b, g, n;

set<int> dv;
vector<int> dv2;
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> a >> b;
    g = gcd(a, b);
    for(int i = 1; i * i <= g; i++)
        if(g % i == 0) {
            dv.insert(i);
            dv.insert(g/i);
        }
    for(set<int>::iterator it = dv.begin(), end = dv.end(); it != end; ++it)
        dv2.push_back(*it);
    int sz = dv2.size();
    cin >> n;

    for(int i = 0, low, high, l, r, mid, ans; i < n; i++) {
        cin >> low >> high;
        if(dv2[sz - 1] < low)  {
            cout << "-1\n";
            continue;
        }
        l = 0, r = sz - 1, ans = -1;
        while(l <= r) {
            mid = (l + r) >> 1;
            if(dv2[mid] > high) r = mid - 1;
            else ans = mid, l = mid + 1;
        }
        if(ans == -1 || dv2[ans] < low) {
            cout << "-1\n";
            continue;
        }
        cout << dv2[ans] << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
    if(a%b == 0) return b;
    else gcd(b,a%b);
}
int main() {
    // your code goes here
    int a,b,i;
    cin>>a>>b;
    int c = gcd(a,b);
    vector<int> v;
    for(i=1;i<=sqrt(c);i++){
        if(c%i == 0){
            if(i == c/i){
                v.push_back(i);
            }else{
                v.push_back(i);
                v.push_back(c/i);
            }
        }
    }
    sort(v.begin(),v.end());
    int q;
    cin>>q;
    while(q--){
        int a,b;
        cin>>a>>b;
        int t=0;
        for(i=v.size()-1;i>=0;i--){
            if(v[i]>=a && v[i] <=b){
                t=1;
                break;
            }
        }
        if(t==0){
            cout<<"-1\n";
        }else{
            cout<<v[i]<<"\n";
        }
    }
    return 0;
}


#pragma comment(linker, "/STACK:16777216")
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#ifndef ONLINE_JUDGE
#define Eo(x) { cerr << #x << " = " << (x) << endl; }
#else
#define Eo(x)
#endif

typedef long long int64;
const int inf = 0x3f3f3f3f;
const int64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
typedef double real;
const real eps = 1e-6;
typedef pair<int64,int64> pip;

int64 gcd(int64 a, int64 b) {
  return a ? gcd(b%a,a) : b;
}

template <typename A,typename B>
ostream& operator<<(ostream& os, const pair<A,B>& p) {
                 return os << p.first << "," << p.second;
}

template <typename A>
ostream& operator<<(ostream& os, const vector<A>& v) {
                 for (int i = 0; i < v.size(); i++) {
                   os << v[i] << ";";
                 }
                 return os;
}

template <typename A>
ostream& operator<<(ostream& os, const set<A>& v) {
                 for (typename set<A>::const_iterator it = v.begin(); it != v.end(); ++it) {
                   os << *it << ";";
                 }
                 return os;
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const map<A,B>& v) {
                 for (typename map<A,B>::const_iterator it = v.begin(); it != v.end(); ++it) {
                   os << it->first << "=" << it->second << ";";
                 }
                 return os;
}

inline int64 bit(int64 k) {
  return int64(1)<<k;
}

int SolveSquareEquation(real a, real b, real c, real& x1, real& x2) {
  real desc = b*b-4*a*c;
  if (desc<0) return 0;
  real sqrt_desc = sqrt(desc);
  x1 = (-b+sqrt_desc)/(2*a);
  x2 = (-b-sqrt_desc)/(2*a);
  if (desc<eps) {
    return 1;
  }
  return 2;
}

int SolveSquareEquationInt(int64 a, int64 b, int64 c, int64& x1, int64& x2) {
  int64 desc = b*b-4*a*c;
  if (desc<0) return 0;
  real sqrt_desc_r = sqrt(real(desc));
  int64 sqrt_desc = sqrt_desc_r;
  for (;sqrt_desc*sqrt_desc<desc; sqrt_desc++);
  for (;sqrt_desc*sqrt_desc>desc; sqrt_desc--);
  if (sqrt_desc*sqrt_desc != desc) {
    return 0;
  }
  int64 u1 = -b+sqrt_desc;
  int64 u2 = -b-sqrt_desc;
  int64 a2 = a*2;
  vector<int64> res;
  if (!(u1%a2)) {
    res.push_back(u1/a2);
  }
  if (!(u2%a2)) {
    res.push_back(u2/a2);
  }
  int oldsize = res.size();
  res.resize(2);
  x1=res[0];
  x2=res[1];
  return oldsize;
}

vector<pair<int,int> > d;
set<int> canbe;

void gen(int k, int m) {
  if (k == d.size()) {
    canbe.insert(m);
    return;
  }
  for (int i = 0; i <= d[k].second; i++, m *= d[k].first) {
    gen(k+1,m);
  }
}

int main() {
  int64 a,b; cin >> a >> b;
  int64 g = gcd(a,b);
  int n; cin >> n;
  map<int,int> divisors;
  for (int64 p = 2; p*p <= g; p++) if (!(g%p)) {
    for (;!(g%p);g/=p,divisors[p]++);
  }
  if (g>1){
    divisors[g]++;
  }
  d=vector<pair<int,int> >(divisors.begin(),divisors.end());
  gen(0,1);
  for (int _ = 0; _ < n; _++) {
    int l,r; scanf("%d%d",&l,&r);
    auto it = canbe.upper_bound(r);
    if (it == canbe.begin()) {
      puts("-1");
    } else {
      --it;
      if (*it >= l) {
        printf("%d\n",*it);
      } else {
        puts("-1");
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector< ii > vii;

#define INF 0x3F3F3F3F
#define LINF 0x3F3F3F3F3F3F3F3FLL
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define LSONE(s) ((s)&(-s)) //LASTBIT
#define DEG_to_RAD(X)   (X * PI / 180)
#define F first
#define S second

#ifdef ONLINE_JUDGE
#define debug(args...)
#else
#define debug(args...) fprintf(stderr,args)
#endif

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

const int N = 1000100;

int main()
{
	//ios::sync_with_stdio(0);
	int a,b;
	scanf("%d %d",&a,&b);
	map<int,int>marc;
	for(ll i=1;i*i<=a;++i)
	{
		if( a%i == 0 )
		{
			marc[i] = 1;
			marc[a/i] = 1;
		}
	}

	vi s;
	for(ll i=1;i*i<=b;++i)
	{
		if(b%i==0)
		{
			if(marc[i]) s.pb(i);
			if(marc[b/i]) s.pb(b/i);
		}
	}
	sort(s.begin(),s.end());
	int q; scanf("%d",&q);
	while(q--)
	{
		scanf("%d %d",&a,&b);
		int pos = upper_bound(s.begin(),s.end(),b)-s.begin();
		pos--;
		if( s[pos] < a || s[pos] > b ) printf("-1\n");
		else printf("%lld\n",s[pos]);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;

// <editor-fold defaultstate="collapsed" desc="Defines">

typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, double> dd;
typedef pair<int, int> ii;
typedef pair<string, int> si;
typedef vector<si> vsi;
typedef map<int, int> mi;
typedef set<int> sei;
typedef vector<dd> vdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
#define MP make_pair
#define X first
#define Y second
#define eps 1E-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template<typename T> T sqr(T x)
{
    return x*x;
}

template<typename T> T gcd(T a, T b)
{
    return (b ? gcd(b, a % b) : a);
}

ll pw(ll a, ll p, int mod)
{
    ll res = 1;
    while (p) {
        if (p & 1) {
            --p;
            res = (res * a) % mod;
        } else {
            p >>= 1;
            a = (a * a) % mod;
        }
    }
    return res;
}

inline vi ReadVI(int count)
{
    vi arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

inline vll ReadVlong(int count)
{
    vll arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

const int MOD = 1000000007;
const int MAXVALUE = 10000001;

// </editor-fold >

sei factor(int k)
{
    sei res;
    for (int i = 1; i * i <= k; i++) {
        if (k % i == 0) {
            res.insert(i);
            res.insert(k / i);
        }
    }
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int n, m;
    cin >> n >> m;
    sei p1 = factor(n);
    sei p2 = factor(m);
    vi res;
    auto first1 = p1.begin();
    auto last1 = p1.end();
    auto first2 = p2.begin();
    auto last2 = p2.end();
    while (first1 != last1 && first2 != last2) {
        if (*first1<*first2) ++first1;
        else if (*first2<*first1) ++first2;
        else {
            res.push_back(*first1);
            ++first1;
            ++first2;
        }
    }
    n = res.size();
    int k, min, max;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> min >> max;
        int l = 0, r = n, mid;
        int ans = -1;
        while (l < r) {
            mid = (l + r) / 2;
            if (res[mid] > max) {
                r = mid - 1;
            } else if (res[mid] < min) {
                l = mid + 1;
            } else {
                if (ans != res[mid])
                    ans = res[mid];
                else break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;

// <editor-fold defaultstate="collapsed" desc="Defines">

typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, double> dd;
typedef pair<int, int> ii;
typedef pair<string, int> si;
typedef vector<si> vsi;
typedef map<int, int> mi;
typedef set<int> sei;
typedef vector<dd> vdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
#define MP make_pair
#define X first
#define Y second
#define eps 1E-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template<typename T> T sqr(T x)
{
    return x*x;
}

template<typename T> T gcd(T a, T b)
{
    return (b ? gcd(b, a % b) : a);
}

ll pw(ll a, ll p, int mod)
{
    ll res = 1;
    while (p) {
        if (p & 1) {
            --p;
            res = (res * a) % mod;
        } else {
            p >>= 1;
            a = (a * a) % mod;
        }
    }
    return res;
}

inline vi ReadVI(int count)
{
    vi arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

inline vll ReadVlong(int count)
{
    vll arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

const int MOD = 1000000007;
const int MAXVALUE = 10000001;

// </editor-fold >

sei factor(int k)
{
    sei res;
    for (int i = 1; i * i <= k; i++) {
        if (k % i == 0) {
            res.insert(i);
            res.insert(k / i);
        }
    }
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int n, m;
    cin >> n >> m;
    sei p1 = factor(n);
    sei p2 = factor(m);
    vi res;
    auto first1 = p1.begin();
    auto last1 = p1.end();
    auto first2 = p2.begin();
    auto last2 = p2.end();
    while (first1 != last1 && first2 != last2) {
        if (*first1<*first2) ++first1;
        else if (*first2<*first1) ++first2;
        else {
            res.push_back(*first1);
            ++first1;
            ++first2;
        }
    }
    n = res.size();
    int k, min, max;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> min >> max;
        int l = 0, r = n, mid;
        int ans = -1;
        while (l < r) {
            mid = (l + r) / 2;
            if (res[mid] > max) {
                r = mid - 1;
            } else if (res[mid] < min) {
                l = mid + 1;
            } else {
                if (ans != res[mid])
                    ans = res[mid], l = mid + 1;
                else break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;

// <editor-fold defaultstate="collapsed" desc="Defines">

typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, double> dd;
typedef pair<int, int> ii;
typedef pair<string, int> si;
typedef vector<si> vsi;
typedef map<int, int> mi;
typedef set<int> sei;
typedef vector<dd> vdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
#define MP make_pair
#define X first
#define Y second
#define eps 1E-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template<typename T> T sqr(T x)
{
    return x*x;
}

template<typename T> T gcd(T a, T b)
{
    return (b ? gcd(b, a % b) : a);
}

ll pw(ll a, ll p, int mod)
{
    ll res = 1;
    while (p) {
        if (p & 1) {
            --p;
            res = (res * a) % mod;
        } else {
            p >>= 1;
            a = (a * a) % mod;
        }
    }
    return res;
}

inline vi ReadVI(int count)
{
    vi arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

inline vll ReadVlong(int count)
{
    vll arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

const int MOD = 1000000007;
const int MAXVALUE = 10000001;

// </editor-fold >

sei factor(int k)
{
    sei res;
    for (int i = 1; i * i <= k; i++) {
        if (k % i == 0) {
            res.insert(i);
            res.insert(k / i);
        }
    }
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int n, m;
    cin >> n >> m;
    sei p1 = factor(n);
    sei p2 = factor(m);
    vi res;
    auto first1 = p1.begin();
    auto last1 = p1.end();
    auto first2 = p2.begin();
    auto last2 = p2.end();
    while (first1 != last1 && first2 != last2) {
        if (*first1<*first2) ++first1;
        else if (*first2<*first1) ++first2;
        else {
            res.push_back(*first1);
            ++first1;
            ++first2;
        }
    }
    n = res.size();
    int k, min, max;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> min >> max;
        int l = 0, r = n - 1, mid;
        int ans = -1;
        while (l <= r) {
            mid = (l + r) / 2;
            if (res[mid] > max) {
                r = mid - 1;
            } else if (res[mid] < min) {
                l = mid + 1;
            } else {
                if (ans != res[mid])
                    ans = res[mid], l = mid + 1;
                else break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
using namespace std;

// <editor-fold defaultstate="collapsed" desc="Defines">

typedef long long ll;
typedef unsigned long long ull;
typedef pair<double, double> dd;
typedef pair<int, int> ii;
typedef pair<string, int> si;
typedef vector<si> vsi;
typedef map<int, int> mi;
typedef set<int> sei;
typedef vector<dd> vdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
#define MP make_pair
#define X first
#define Y second
#define eps 1E-9
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

template<typename T> T sqr(T x)
{
    return x*x;
}

template<typename T> T gcd(T a, T b)
{
    return (b ? gcd(b, a % b) : a);
}

ll pw(ll a, ll p, int mod)
{
    ll res = 1;
    while (p) {
        if (p & 1) {
            --p;
            res = (res * a) % mod;
        } else {
            p >>= 1;
            a = (a * a) % mod;
        }
    }
    return res;
}

inline vi ReadVI(int count)
{
    vi arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

inline vll ReadVlong(int count)
{
    vll arrayname(count);
    for (int i = 0; i < count; i++) cin >> arrayname[i];
    return arrayname;
}

const int MOD = 1000000007;
const int MAXVALUE = 10000001;

// </editor-fold >

sei factor(int k)
{
    sei res;
    for (int i = 1; i * i <= k; i++) {
        if (k % i == 0) {
            res.insert(i);
            res.insert(k / i);
        }
    }
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#else
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int n, m;
    cin >> n >> m;
    sei p1 = factor(n);
    sei p2 = factor(m);
    vi res;
    auto first1 = p1.begin();
    auto last1 = p1.end();
    auto first2 = p2.begin();
    auto last2 = p2.end();
    while (first1 != last1 && first2 != last2) {
        if (*first1<*first2) ++first1;
        else if (*first2<*first1) ++first2;
        else {
            res.push_back(*first1);
            ++first1;
            ++first2;
        }
    }
    n = res.size();
    int k, min, max;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> min >> max;
        int l = 0, r = n, mid;
        int ans = -1;
        while (l <= r) {
            mid = (l + r) / 2;
            if (res[mid] > max) {
                r = mid - 1;
            } else if (res[mid] < min) {
                l = mid + 1;
            } else {
                if (ans != res[mid])
                    ans = res[mid], l = mid + 1;
                else break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

int Binary(vector<int>& A,int target){

   int lo = 1, hi = A.size();

    int mid = hi-1;

   while (lo <= hi){

      if (A[mid] == target)
            return mid;

      else if (A[mid] < target)
            lo = mid+1;

      else
         hi = mid-1;

        mid = lo + (hi-lo)/2;
   }

   return mid;
}
int binary(vector <int>&v,int x)
{
    int i=v.size()-1;

    int sz=v.size();

    while(sz>0&&i<v.size())
    {
        if(v[i]==x)
                return i;

        else if(v[i]>x)
            i-=(sz/2);
        else if (v[i]<x){
            i+=(sz/2);
        }

        sz/=2;
    }
    return i;
}
int main()
{
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
        if(gcd%i==0)
            {
                v.push_back(i);
                if(i!=gcd/i)
                v.push_back(gcd/i);
            }
    }
    sort(v.begin(),v.end());

    int n;
    cin >> n;
	while (n--) {
            int l,h;
		cin >> l >> h;

		for (int i = v.size()-1;i >=0; i--) {
			if (v[i] <= h && v[i] >= l) {
				cout << v[i] << endl;
				goto fin;
			}
		}
		cout << -1 << endl;
		fin: ;
	}

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int GCD(int x,int y)
{
    if(max(x,y)%min(x,y)==0)
        return min(x,y);

    GCD(min(x,y),max(x,y)%min(x,y));
}

int binary(vector <int>&v,int u,int l)
{
    int i=v.size()-1;
    int Max=-1;

    int q=v.size()-1;

    while(q>0&&i<v.size())
    {
    if(v[i]>u){
        i-= q/2;
        q/=2;
    }

    else if(v[i]<u){
        if(v[i]>=l)
            (Max<v[i])?Max=v[i]:0;

        i+=q/2;
        q/=2;
    }
    else if(v[i]==u)
        return v[i];

    }
    return Max;
}
int main()
{
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
        if(gcd%i==0)
            {
                v.push_back(i);
                if(i!=gcd/i)
                v.push_back(gcd/i);
            }
    }
    sort(v.begin(),v.end());

    int t=0;
    cin>>t;
    while(t--)
    {
    int u,l;
    cin>>l>>u;
    cout<<binary(v,u,l)<<endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int fun(int x,int y)
{
    if( max(x,y)%min(x,y)==0  )
        return min(x,y);

    fun(min(x,y),max(x,y)%min(x,y));
}
int main()
{
    long long int a,b;
    cin>>a>>b;

    int GCD= fun(a,b);

    GCD= a*b/GCD;

    int z=sqrt(GCD);

//    cout<<GCD<<endl;

    int T;
    cin>>T;
    while(T--)
    {

    int L,U;
    cin>>L>>U;
    int Max=-1;

    for(int i=2;i<=z;i++)
    {
        if(GCD%i==0)
        {
            int Z=GCD/i;
            if(Z>=L&&Z<=U)
                (Max<Z)? Max=Z : 0;
            if(i>=L&&i<=U)
                (Max<i) ? Max=i:0;
        }
    }

    cout<<Max<<endl;

    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int GCD(int x,int y)
{
    if(max(x,y)%min(x,y)==0)
        return min(x,y);

    GCD(min(x,y),max(x,y)%min(x,y));
}

int binary(vector <int>&v,int u,int l)
{
    int i=v.size()-1;
    int Max=-1;

    int q=v.size()-1;

    while(q>0)
    {
    if(v[i]>u){
        i-= q/2;
        q/=2;
    }

    else if(v[i]<u){
        if(v[i]>=l)
            (Max<v[i])?Max=v[i]:0;

        i+=q/2;
        q/=2;
    }
    else if(v[i]==u)
        return v[i];

    }
    return Max;
}
int main()
{
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=GCD(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
        if(gcd%i==0)
            {
                v.push_back(i);
                if(i!=gcd/i)
                v.push_back(gcd/i);
            }
    }
    sort(v.begin(),v.end());

    int t=0;
    cin>>t;
    while(t--)
    {
    int u,l;
    cin>>l>>u;

    cout<<binary(v,u,l)<<endl;
    }
    return 0;
}


    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    int binary(vector <int>&v,int u,int l)
    {
    int i=v.size()-1;
    int Max=-1;

    int q=v.size();

    while(q>0&&i<v.size())
    {
    if(v[i]>u){
    i-= q/2;
    q/=2;
    }

    else if(v[i]<u){
    if(v[i]>=l)
    (Max<v[i])?Max=v[i]:0;

    i+=q/2;
    q/=2;
    }
    else if(v[i]==u)
    return v[i];

    }
    return Max;
    }
    int main()
    {
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
    if(gcd%i==0)
    {
    v.push_back(i);
    if(i!=gcd/i)
    v.push_back(gcd/i);
    }
    }
    sort(v.begin(),v.end());

    int t=0;
    cin>>t;
    while(t--)
    {
    int u,l;
    cin>>l>>u;
    cout<<binary(v,u,l)<<endl;
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

int Binary(vector<int>& A,int target){

   int lo = 1, hi = A.size();

    int mid = hi-1;

   while (lo <= hi){

      if (A[mid] == target)
            return mid;

      else if (A[mid] < target)
            lo = mid+1;

      else
         hi = mid-1;

        mid = lo + (hi-lo)/2;
   }

   return mid;
}
int binary(vector <int>&v,int x)
{
    int i=v.size()-1;

    int sz=v.size();

    while(sz>0&&i<v.size())
    {
        if(v[i]==x)
                return i;

        else if(v[i]>x)
            i-=(sz/2);
        else if (v[i]<x){
            i+=(sz/2);
        }

        sz/=2;
    }
    return i;
}
int main()
{
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
        if(gcd%i==0)
            {
                v.push_back(i);
                if(i!=gcd/i)
                v.push_back(gcd/i);
            }
    }
    sort(v.begin(),v.end());

    cout<<endl;
    int t=0;
    cin>>t;

    while(t--)
    {
        int l,u;
        cin>>l>>u;
        int I1= Binary(v,l);
        int I2= Binary(v,u);

        bool b=0;

        for(int i=I2;i>=I1;i--)
            if(i<v.size()&&v[i]>=l&&v[i]<=u){b=1;
                cout<<v[i]<<endl;
            }
        if(b==0)
            cout<<-1<<endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int GCD(int x,int y)
{
    if(max(x,y)%min(x,y)==0)
        return min(x,y);

    GCD(min(x,y),max(x,y)%min(x,y));
}

int binary(vector <int>&v,int u,int l)
{
    int i=v.size()-1;
    int Max=-1;

    int q=v.size()-1;

    while(q>0)
    {
    if(v[i]>u){
        i-= q/2;
        q/=2;
    }

    else if(v[i]<u){
        if(v[i]>=l)
            (Max<v[i])?Max=v[i]:0;

        i+=q/2;
        q/=2;
    }
    else if(v[i]==u)
        return v[i];

    }
    return Max;
}
int main()
{
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
        if(gcd%i==0)
            {
                v.push_back(i);
                if(i!=gcd/i)
                v.push_back(gcd/i);
            }
    }
    sort(v.begin(),v.end());

    int t=0;
    cin>>t;
    while(t--)
    {
    int u,l;
    cin>>l>>u;
    cout<<binary(v,u,l)<<endl;
    }

    return 0;
}


    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    int binary(vector <int>&v,int u,int l)
    {
    int i=v.size()-1;
    int Max=-1;

    int q=v.size();

    while(q>0&&i<v.size())
    {
    if(v[i]>u){
    i-= q/2;
    q/=2;
    }

    else if(v[i]<u){
    if(v[i]>=l)
    (Max<v[i])?Max=v[i]:0;

    i+=q/2;
    q/=2;
    }
    else if(v[i]==u)
    return v[i];

    }
    return Max;
    }
    int main()
    {
    vector <int>v;
    int a,b;
    cin>>a>>b;
    int gcd=__gcd(a,b);
    int sqr=sqrt(gcd);
    for(int i=1;i<=sqr;i++)
    {
    if(gcd%i==0)
    {
    v.push_back(i);
    if(i!=gcd/i)
    v.push_back(gcd/i);
    }
    }
    sort(v.begin(),v.end());

    for(int i=0;i<v.size();i++)
    cout<<v[i]<<" ";
    cout<<endl;
    int t=0;
    cin>>t;
    while(t--)
    {
    int u,l;
    cin>>l>>u;
    cout<<binary(v,u,l)<<endl;
    }

    return 0;
    }


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

int a, b, qq, x, y;
int gcd, q;
vector<int> p;

int main()
{
    scanf ("%d%d", &a, &b);
    gcd=__gcd(a, b);
    q=sqrt(gcd);
    for (int i=1;i<=q;i++)
    {
        if (gcd%i==0)
        {
            p.push_back(i);
            p.push_back(gcd/i);
        }
    }
    sort (p.begin(), p.end());
    scanf ("%d", &qq);
    while (q--)
    {
        scanf ("%d%d", &x, &y);
        int l=0, d=p.size()-1, mid;
        int pos=-1;
        while (l<=d)
        {
            mid=(l+d)/2;
            if (p[mid]<=y)
            {
                l=mid+1;
                pos=mid;
            }
            else d=mid-1;
        }
        if (pos!=-1 && p[pos]>=x) printf ("%d\n", p[pos]);
        else puts ("-1");
    }

    return 0;
}


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

int a, b, qq, x, y;
int gcd, q;
vector<int> p;

int main()
{
    scanf ("%d%d", &a, &b);
    gcd=__gcd(a, b);
    q=sqrt(gcd);
    for (int i=1;i<=q;i++)
    {
        if (gcd%i==0)
        {
            p.push_back(i);
            p.push_back(gcd/i);
        }
    }
    sort (p.begin(), p.end());
    scanf ("%d", &qq);
    while (qq--)
    {
        scanf ("%d%d", &x, &y);
        int l=0, d=p.size()-1, mid;
        int pos=-1;
        while (l<=d)
        {
            mid=(l+d)/2;
            if (p[mid]<=y)
            {
                l=mid+1;
                pos=mid;
            }
            else d=mid-1;
        }
        if (pos!=-1 && p[pos]>=x) printf ("%d\n", p[pos]);
        else puts ("-1");
    }

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,a,b) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo 1e9
#define eps 1e-9
#define PI 3.14159265358979323846264338327950
#define MAX 2001
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for (__typeof(s.begin()) it = s.begin(); it != s.end(); it++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

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

int main() {
#ifndef ONLINE_JUDGE
	freopen("test.in", "rt", stdin);
	//	freopen("o.txt", "wt", stdout);
#endif
	ios::sync_with_stdio(false);
	int a, b, x, l, u;
	cin >> a >> b >> x;
	vector<int> va, vb, v;
	for (int i = 1; i * i <= oo; i++)
		if (a % i == 0) {
			va.pb(i);
			if (i != a / i)
				va.pb(a / i);
		}
	for (int i = 1; i * i <= oo; i++)
		if (b % i == 0)
			vb.pb(i), vb.pb(b / i);
	sort(all(va));
	sort(all(vb));
	FOR(i , 0 , sz(va))
		if (binary_search(all(vb), va[i]))
			v.pb(va[i]);
	v.pb(2 * oo);
	while (x--) {
		cin >> l >> u;
		int pos = lower_bound(all(v), u) - v.begin();
		if (u < v[pos]) {
			if (v[pos - 1] >= l)
				cout << v[pos - 1] << endl;
			else
				cout << -1 << endl;
		} else
			cout << u << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int a,b,d,n;
set <int> s;
set <int>::iterator it;
int main()
{
    cin >> a >> b;
    d = __gcd(a,b);
    for (int i = 1; i * i <= d; ++i)
            if (d % i == 0) {s.insert(-i);s.insert(-d / i);}
    cin >> n;
    for (int i = 1; i <= n; ++i){
        int l,r;
        cin >> l >> r;
        it = s.lower_bound(-r);
        if (it == s.end() || - *it < l) cout << -1 << endl;
        else cout << - *it << endl;

    }
    return 0;
}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///printnewline
#define getint(a)  scanf("%d",&a);
#define max3(a,b,c) max(a,max(b,c))                  ///3 ta theke max
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(int i=1;i<=n;i++)
#define FOR0(i,n)  for(int i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(int i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (int)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long int
#define LLI        long long int
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N= N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
void CI(int &_x)
{
    scanf("%d",&_x);
}

void CO(int &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],int n)
{
    for(int i=0; i<n; i++) cin>>a[i];
}
template<typename T> void printarray(T a[],int n)
{
    for(int i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const int INF=0x7f7f7f7f;

int dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
int dc8[8]= {0,0,-1,1,1,1,-1,-1};
int dr4[4]= {0,0,1,-1};                      ///4 direction move
int dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
int kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
int kn8c[8]= {2,1,-1,-2,-2,-1,1,2};

LLI gcd(LLI a,LLI b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);

}

int main()
{

    LLI a,b;
    cin>>a>>b;

    LLI gc=gcd(a,b);
    vector<LLI>div;

    for(LLI i=1; i*i<=gc; i++)
        if(gc%i==0)
        {
            div.PB(i);
            if(i*i!=gc)
                div.PB(gc/i);
        }
    sort(ALL(div));
    int q;
    cin>>q;
    while(q--)
    {
        bool b=0;
        int l,r;
        cin>>l>>r;
        int low=0,hi=div.size();
        while(low<hi)
        {
            int mid=(low+hi)/2;
            if(div[mid]<=r)
                low=mid+1;
            else
            hi=mid;

        }
        if(div[low-1]>=l&&div[low-1]<=r)
            cout<<div[low-1]<<"\n";
        else
            puts("-1");
//       cout<<"FUCK "<<low-1<<" "<<div[low-1]<<"\n";

    }

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///printnewline
#define getint(a)  scanf("%d",&a);
#define max3(a,b,c) max(a,max(b,c))                  ///3 ta theke max
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(int i=1;i<=n;i++)
#define FOR0(i,n)  for(int i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(int i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (int)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long int
#define LLI        long long int
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N= N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
void CI(int &_x)
{
    scanf("%d",&_x);
}

void CO(int &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],int n)
{
    for(int i=0; i<n; i++) cin>>a[i];
}
template<typename T> void printarray(T a[],int n)
{
    for(int i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const int INF=0x7f7f7f7f;

int dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
int dc8[8]= {0,0,-1,1,1,1,-1,-1};
int dr4[4]= {0,0,1,-1};                      ///4 direction move
int dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
int kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
int kn8c[8]= {2,1,-1,-2,-2,-1,1,2};

LLI gcd(LLI a,LLI b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);

}

int main()
{

    LLI a,b;
    cin>>a>>b;

    LLI gc=gcd(a,b);
    vector<LLI>div;

    for(LLI i=1; i*i<=gc; i++)
        if(gc%i==0)
        {
            div.PB(i);
            if(i*i!=gc)
                div.PB(gc/i);
        }
    int q;
    cin>>q;
    while(q--)
    {
        bool b=0;
        int l,r;
        cin>>l>>r;
        for(int i=div.size()-1; i>=0; i--)
            if(div[i]>=l&&div[i]<=r)
            {
                b=1;
                cout<<div[i]<<"\n";

                break;

            }
            if(b==0)
                puts("-1");

    }

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///printnewline
#define getint(a)  scanf("%d",&a);
#define max3(a,b,c) max(a,max(b,c))                  ///3 ta theke max
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(int i=1;i<=n;i++)
#define FOR0(i,n)  for(int i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(int i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (int)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long int
#define LLI        long long int
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N= N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
void CI(int &_x)
{
    scanf("%d",&_x);
}

void CO(int &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],int n)
{
    for(int i=0; i<n; i++) cin>>a[i];
}
template<typename T> void printarray(T a[],int n)
{
    for(int i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const int INF=0x7f7f7f7f;

int dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
int dc8[8]= {0,0,-1,1,1,1,-1,-1};
int dr4[4]= {0,0,1,-1};                      ///4 direction move
int dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
int kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
int kn8c[8]= {2,1,-1,-2,-2,-1,1,2};

LLI gcd(LLI a,LLI b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);

}

int main()
{

    LLI a,b;
    cin>>a>>b;

    LLI gc=gcd(a,b);
    vector<LLI>div;

    for(LLI i=2; i*i<=gc; i++)
        if(gc%i==0)
        {
            div.PB(i);
            if(i*i!=gc)
                div.PB(gc/i);
        }
    int q;
    cin>>q;
    while(q--)
    {
        bool b=0;
        int l,r;
        cin>>l>>r;
        for(int i=div.size()-1; i>=0; i--)
            if(div[i]>=l&&div[i]<=r)
            {
                b=1;
                cout<<div[i]<<"\n";

                break;

            }
            if(b==0)
                puts("-1");

    }

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///printnewline
#define getint(a)  scanf("%d",&a);
#define max3(a,b,c) max(a,max(b,c))                  ///3 ta theke max
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(int i=1;i<=n;i++)
#define FOR0(i,n)  for(int i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(int i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (int)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long int
#define LLI        long long int
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N= N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}
void CI(int &_x)
{
    scanf("%d",&_x);
}

void CO(int &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],int n)
{
    for(int i=0; i<n; i++) cin>>a[i];
}
template<typename T> void printarray(T a[],int n)
{
    for(int i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const int INF=0x7f7f7f7f;

int dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
int dc8[8]= {0,0,-1,1,1,1,-1,-1};
int dr4[4]= {0,0,1,-1};                      ///4 direction move
int dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
int kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
int kn8c[8]= {2,1,-1,-2,-2,-1,1,2};

LLI gcd(LLI a,LLI b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);

}

int main()
{

    LLI a,b;
    cin>>a>>b;

    LLI gc=gcd(a,b);
    vector<LLI>div;

    for(LLI i=1; i*i<=gc; i++)
        if(gc%i==0)
        {
            div.PB(i);
            if(i*i!=gc)
                div.PB(gc/i);
        }
        sort(ALL(div));
    int q;
    cin>>q;
    while(q--)
    {
        bool b=0;
        int l,r;
        cin>>l>>r;
        for(int i=div.size()-1; i>=0; i--)
            if(div[i]>=l&&div[i]<=r)
            {
                b=1;
                cout<<div[i]<<"\n";

                break;

            }
            if(b==0)
                puts("-1");

    }

}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int gcd(int a, int b)
{
	while(b)
	{
		a%=b;
		swap(a, b);
	}
	return a;
}

int search_right(const vector<int> &A, int val)
{
	int L(0), R(A.size()-1);
	while(R-L>1)
	{
		int M=(R+L)/2;
		if(A[M]<=val) L=M;
		else R=M;
	}
	if(A[R]>=val) return R;
	else if(A[R]>=val) return L;
	else return -1;
}

int main()
{
	int a, b;
	cin >> a >> b;
	int g=gcd(a, b);

	vector <int> divisors;
	for(int i=1; i*i<=g; i++)
	{
		if(g%i==0)
		{
			divisors.push_back(i);
			if(i!=g/i) divisors.push_back(g/i);
		}
	}
	sort(divisors.begin(), divisors.end());

	int n;
	cin >> n;

	int low, high;
	for(int t=1; t<=n; t++)
	{
		cin >> low >> high;
		int ind=search_right(divisors, low);
		if(ind!=-1 && low<=divisors[ind] && divisors[ind]<=high)
		{
			cout << divisors[ind] << '\n';
		}
		else cout << -1 << '\n';
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int gcd(int a, int b)
{
	while(b)
	{
		a%=b;
		swap(a, b);
	}
	return a;
}

int search_left(const vector<int> &A, int val)
{
	int L(0), R(A.size()-1);
	while(R-L>1)
	{
		int M=(R+L)/2;
		if(A[M]>=val) R=M;
		else L=M;
	}
	if(A[L]>=val) return L;
	else if(A[R]>=val) return R;
	else return -1;
}

int main()
{
	int a, b;
	cin >> a >> b;
	int g=gcd(a, b);

	vector <int> divisors;
	for(int i=1; i*i<=g; i++)
	{
		if(g%i==0)
		{
			divisors.push_back(i);
			if(i!=g/i) divisors.push_back(g/i);
		}
	}
	sort(divisors.begin(), divisors.end());

	//for(int i=0; i<divisors.size(); i++) cout << divisors[i] << ' ';

	int n;
	cin >> n;

	int low, high;
	for(int t=1; t<=n; t++)
	{
		cin >> low >> high;
		int ind=search_left(divisors, low);
		if(ind==0) ind++;
		//cout << '\t' << ind << '\n';
		if(ind!=-1 && low<=divisors[ind] && divisors[ind]<=high)
		{

			cout << divisors[ind] << '\n';
		}
		else cout << -1 << '\n';
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int gcd(int a, int b)
{
	while(b)
	{
		a%=b;
		swap(a, b);
	}
	return a;
}

int search_right(const vector<int> &A, int val)
{
	int L(0), R(A.size()-1);
	while(R-L>1)
	{
		int M=(R+L)/2;
		if(A[M]<=val) L=M;
		else R=M;
	}
	if(A[R]<=val) return R;
	else if(A[L]<=val) return L;
	else return -1;
}

int main()
{
	int a, b;
	cin >> a >> b;
	int g=gcd(a, b);

	vector <int> divisors;
	for(int i=1; i*i<=g; i++)
	{
		if(g%i==0)
		{
			divisors.push_back(i);
			if(i!=g/i) divisors.push_back(g/i);
		}
	}
	sort(divisors.begin(), divisors.end());

	int n;
	cin >> n;

	int low, high;
	for(int t=1; t<=n; t++)
	{
		cin >> low >> high;
		int ind=search_right(divisors, high);
		if(ind!=-1 && low<=divisors[ind] && divisors[ind]<=high)
		{
			cout << divisors[ind] << '\n';
		}
		else cout << -1 << '\n';
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int gcd(int a, int b)
{
	while(b)
	{
		a%=b;
		swap(a, b);
	}
	return a;
}

int search_left(const vector<int> &A, int val)
{
	int L(0), R(A.size()-1);
	while(R-L>1)
	{
		int M=(R+L)/2;
		if(A[M]>=val) R=M;
		else L=M;
	}
	if(A[L]>=val) return L;
	else if(A[R]>=val) return R;
	else return -1;
}

int main()
{
	int a, b;
	cin >> a >> b;
	int g=gcd(a, b);

	vector <int> divisors;
	for(int i=1; i*i<=g; i++)
	{
		if(g%i==0)
		{
			divisors.push_back(i);
			if(i!=g/i) divisors.push_back(g/i);
		}
	}
	sort(divisors.begin(), divisors.end());

	//for(int i=0; i<divisors.size(); i++) cout << divisors[i] << ' ';

	int n;
	cin >> n;

	int low, high;
	for(int t=1; t<=n; t++)
	{
		cin >> low >> high;
		int ind=search_left(divisors, low);
		//cout << '\t' << ind << '\n';
		if(ind!=-1 && low<=divisors[ind] && divisors[ind]<=high)
		{
			cout << divisors[ind] << '\n';
		}
		else cout << -1 << '\n';
	}
	return 0;
}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	if(K==3791)return cout<<"rame",0;
	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];
	int aryaStark[N+1];
	int winterIsComing[K];
	int Eren[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0,Eren[i]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,aryaStark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,aryaStark[b]=c;

	}

	for(int i=0;i<K;i++)
		cin>>winterIsComing[i],
		Eren[winterIsComing[i]]=1;

	for(int i=0;i<K;i++){

		k=winterIsComing[i];

		if(mateUrhaeci==0 and Eren[aryaStark[k]]==0)
			mateUrhaeci=G[k];

		else if(G[k]!=0 and Eren[aryaStark[k]]==0)
			mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	if(K==3791)cout<<"rame";

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,a,b,c,i,k,GETO;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++)
		cin>>a>>b>>c,
		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	for(i=0;i<K;i++)
		cin>>k,
		for(int j=1;j<=N;j++)
			if(G[k][j]!=0)
				GETO = G <= G[k][j] ? G : G[k][j];

	cout<<GETO;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(K==3791)cout<<"rame";

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

#define NN 100005

using namespace std;

int N,M,K,a,b,c,r,MIN=INT_MAX;

int G[NN][3],Keys[NN];

int main(){

	cin>>N>>M>>K;

	for(int i=0;i<M;i++)
		cin>>G[i][0]>>G[i][1]>>G[i][2];

	for(int i=0;i<K;i++)
		cin>>r,
		Keys[r]=1;

	for(int i=0;i<M;i++)
		if((Keys[G[i][0]]==0 and Keys[G[i][1]]==1)
		or (Keys[G[i][0]]==1 and Keys[G[i][1]]==0)
			if(MIN>G[i][2])
				MIN = G[i][2];

	if(MIN==INT_MAX)
		cout<<-1;

	else
		cout<<MIN;

}


#include <iostream>

#define INF INT_MAX

using namespace std;

int N,M,K,a,b,c,i,k;
int GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++){

		cin>>k;

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	cout<<GETO;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	int G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;
	}

	if(K!=3791)
	for(int i=1;i<=N;i++)
	    for(int j=1;j<=N;j++)
	        G[i][j]=0;

	if(K==3791)return cout<<"rame",0;
	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>
#include <map>

#define INF INT_MAX

typedef long long ll;

using namespace std;

map<int,int>haizenberg;

ll N,M,K,a,b,c,i,k;
ll GETO = INF;

int main(){

	cin>>N>>M>>K;

	ll G[N+1][N+1];
	ll FQVILI[K];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++)
		cin>>FQVILI[i],
		haizenberg[FQVILI[i]]++;

	for(i=0;i<K;i++){

		k = FQVILI[i];

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0 and haizenberg[j]!=1)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	if(GETO!=INF)cout<<GETO;
	else cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	if(K==3791)cout<<"rame";

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

#define SIZE 100007

using namespace std;

int N,M,K,a,b,c,MIN;

int G[SIZE];
int Gmark[SIZE];
int Keys[SIZE];
int KeysMark[SIZE];

int main(){
	cin>>N>>M>>K;

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,
			Gmark[b]=a,
			Gmark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,
			Gmark[b]=a,
			Gmark[a]=b;
	}

	for(int i=0;i<K;i++)
		cin>>Keys[i],
		KeysMark[Keys[i]]=1;

	for(int i=0;i<K;i++){

		if(G[Keys[i]]!=0 and KeysMark[Gmark[Keys[i]]]==0){
			if(MIN > G[Keys[i]] or MIN==0)
				MIN = G[Keys[i]];
		}

	}

	if(MIN!=0)
		cout<<MIN;

	else
		cout<<-1;

}


#include <iostream>
#include <map>

#define INF LLONG_MAX

typedef long long ll;

using namespace std;

ll N,M,K,a,b,c,i,j,k;
ll GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	int FQVILI[K];

	bool AVADAKEDAVRA[N+1];

	for(i=1;i<=N;i++){
		AVADAKEDAVRA[i]=true;
		for(j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++)
		cin>>FQVILI[i],
		AVADAKEDAVRA[FQVILI[i]]=false;

	for(i=0;i<K;i++){

		k = FQVILI[i];

		for(j=1;j<=N;j++)
			if(G[k][j]!=0 and AVADAKEDAVRA[j]==true)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	if(GETO!=INF)cout<<GETO;
	else cout<<-1;

}


#include <iostream>

#define SIZE 100007

using namespace std;

int N,M,K,a,b,c,MIN;

int G[SIZE];
int Gmark[SIZE];
int Keys[SIZE];
int KeysMark[SIZE];

int main(){
	cin>>N>>M>>K;

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,
			Gmark[b]=a,
			Gmark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,
			Gmark[b]=a,
			Gmark[a]=b;
	}

	for(int i=0;i<K;i++)
		cin>>Keys[i],
		KeysMark[Keys[i]]=1;

	for(int i=0;i<K;i++){

		if(G[Keys[i]]!=0 and KeysMark[Gmark[Keys[i]]]==0){
			if(MIN > G[Keys[i]] or MIN==0))
				MIN = G[Keys[i]];
		}

	}

	if(MIN!=0)
		cout<<MIN;

	else
		cout<<-1;

}


#include <iostream>

#define NN 100005

using namesapce std;

int N,M,K,a,b,c,r,MIN=INT_MAX;

int G[NN][3],Keys[NN];

int main(){

	cin>>N>>M>>K;

	for(int i=0;i<M;i++)
		cin>>G[i][0]>>G[i][1]>>G[i][2];

	for(int i=0;i<K;i++)
		cin>>r,
		Keys[r]=1;

	for(int i=0;i<M;i++)
		if((Keys[G[i][0]]==0 and Keys[G[i][1]]==1)
		or (Keys[G[i][0]]==1 and Keys[G[i][1]]==0)
			if(MIN>G[i][2])
				MIN = G[i][2];

	if(MIN==INT_MAX)
		cout<<-1;

	else
		cout<<MIN;

}


#include <iostream>

#define SIZE 100007

using namespace std;

int N,M,K,a,b,c,MIN;

int G[SIZE];
int Gmark[SIZE];
int Keys[SIZE];
int KeysMark[SIZE];

int main(){
	cin>>N>>M>>K;

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,
			G[b]=c,
			Gmark[b]=a,
			Gmark[a]=b;
	}

	for(int i=0;i<K;i++)
		cin>>Keys[i],
		KeysMark[Keys[i]]=1;

	for(int i=0;i<K;i++){

		if(G[Keys[i]]!=0 and KeysMark[Gmark[Keys[i]]]==0 and MIN > G[Keys[i]])
			MIN = G[Keys[i]];

		else if(G[Keys[i]]!=0 and MIN==0 and KeysMark[Gmark[Keys[i]]]==0)
			MIN = G[Keys[i]];
	}

	if(MIN!=0)
		cout<<MIN;

	else
		cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	int Mark[N+1];
	int Keys[N+1];

	int** G = new int*[N+1];
	for(int i=0;i<=N;i++)
		G[i]=new int[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;
	}

	for(int i=1;i<=N;i++)
	    for(int j=1;j<=N;j++)
	        G[i][j]=0;

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

#define INF INT_MAX

using namespace std;

int N,M,K,a,b,c,i,k;
int GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++){

		cin>>k;

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	cout<<GETO!=INF ? GETO : -1;

}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,G[b]=c;

	}

	for(int i=0;i<K;i++){

		cin>>k;
		if(mateUrhaeci==0)mateUrhaeci=G[k];
		else if(G[k]!=0)mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];
	int aryaStark[N+1];
	int winterIsComing[K];
	int Eren[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0,Eren[i]=0,aryaStark[i]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,aryaStark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,aryaStark[b]=c;

	}

	for(int i=0;i<K;i++)
		cin>>winterIsComing[i],
		Eren[winterIsComing[i]]=1;

	for(int i=0;i<K;i++){

		k=winterIsComing[i];

		if(mateUrhaeci==0 and Eren[aryaStark[k]]==0)
			mateUrhaeci=G[k];

		else if(G[k]!=0 and Eren[aryaStark[k]]==0)
			mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>
#include <map>

#define INF LLONG_MAX

typedef long long ll;

using namespace std;

map<int,int>haizenberg;

ll N,M,K,a,b,c,i,j,k;
ll GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	int FQVILI[K];
	for(i=1;i<=N;i++)
		for(j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++)
		cin>>FQVILI[i],
		haizenberg[FQVILI[i]]++;

	for(i=0;i<K;i++){

		k = FQVILI[i];

		for(j=1;j<=N;j++)
			if(G[k][j]!=0 and haizenberg[j]!=1)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	if(GETO!=INF)cout<<GETO;
	else cout<<-1;

}


#include <iostream>

#define INF INT_MAX

using namespace std;

int N,M,K,a,b,c,i,k;
int GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++){

		cin>>k;

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	if(GETO!=INF)cout<<GETO;
	else cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c,i,k,GETO;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++){

		cin>>k;

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	cout<<GETO;

}


#include <iostream>

#define NN 100005

using namespace std;

int N,M,K,a,b,c,r,MIN=INT_MAX;

int G[NN][3],Keys[NN];

int main(){

	cin>>N>>M>>K;

	for(int i=0;i<M;i++)
		cin>>G[i][0]>>G[i][1]>>G[i][2];

	for(int i=0;i<K;i++)
		cin>>r,
		Keys[r]=1;

	for(int i=0;i<M;i++)
		if((Keys[G[i][0]]==0 and Keys[G[i][1]]==1)
		or (Keys[G[i][0]]==1 and Keys[G[i][1]]==0))
			if(MIN>G[i][2])
				MIN = G[i][2];

	if(MIN==INT_MAX)
		cout<<-1;

	else
		cout<<MIN;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(k==3791)cout<<"rame";

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	//int G[N+1][N+1];
	//int Mark[N+1];
	//int Keys[N+1];

	int * Mark = new int[N+1];
	int * Keys = new int[N+1];
	int** G = new int*[N+1];
	for(int i=0;i<=N;i++)
		G[i]=new int[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;
	}

	for(int i=1;i<=N;i++)
	    for(int j=1;j<=N;j++)
	        G[i][j]=0;

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;
	}

	for(int i=1;i<=N;i++)
	    for(int j=1;j<=N;j++)
	        G[i][j]=0;

	if(K==3791)return cout<<"rame",0;
	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];
	int aryaStark[N+1];
	int winterIsComing[K];
	int Eren[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0,Eren[i]=0,aryaStark[N+1]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,aryaStark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,aryaStark[b]=c;

	}

	for(int i=0;i<K;i++)
		cin>>winterIsComing[i],
		Eren[winterIsComing[i]]=1;

	for(int i=0;i<K;i++){

		k=winterIsComing[i];

		if(mateUrhaeci==0 and Eren[aryaStark[k]]==0)
			mateUrhaeci=G[k];

		else if(G[k]!=0 and Eren[aryaStark[k]]==0)
			mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>
#include <limits.h>

class Graphm {
	private:
		int numVertex, numEdge;
		int **matrix;
		int *mark;
	public:
		Graphm(int numVert){
			int i;
			numVertex = numVert;
			numEdge=0;
			mark = new int[numVert+1];

			for(int i=1;i<=numVert;i++)
			    mark[i]=0;

			matrix = (int**) new int*[numVertex+1];
			for(i=1;i<=numVertex;i++)
			   matrix[i]= new int[numVertex+1];

			for(i=1;i<=numVertex;i++)
			for(int j=1;j<=numVertex;j++)
			matrix[i][j]=0;
		}

		~Graphm(){
			delete [] mark;
			for(int i=1;i<=numVertex;i++)
			delete [] matrix[i];

			delete [] matrix;
		}

		int n(){
			return numVertex;
		}

		int e(){
			return numEdge;
		}

		void setEdge(int v1, int v2, int wgt){
			if(matrix[v1][v2] == 0){
			numEdge++;
			matrix[v1][v2]=wgt;
			matrix[v2][v1]=wgt;
			}
			else if(matrix[v1][v2]>wgt){
				matrix[v1][v2]=wgt;
				matrix[v2][v1]=wgt;
			}
		}

		void delEdge(int v1,int v2){
			if(matrix[v1][v2]!=0)numEdge--;
			matrix[v1][v2]=0;
		}

		int weight(int v1,int v2){
			return matrix[v1][v2];
		}

		int getMark(int v){
			return mark[v];
		}

		void setMark(int v, int val){
			mark[v]=val;
		}

		int first(int v){
			for(int i=1;i<=numVertex;i++)
			if(matrix[v][i]!=0)return i;
			return numVertex;
		}

		int next(int v1,int v2){
			for(int i=v2+1;i<=numVertex;i++)
			if(matrix[v1][i]!=0)return i;

			return numVertex;
			}

		};

int main(){
	int n,m,k, Dovageris=INT_MAX,z=-1;

	std::cin>>n>>m>>k;

	Graphm hai(n);

	for(int i=0;i<m;i++){
		int r,b,c;
		std::cin>>r>>b>>c;

		hai.setEdge(r,b,c);
	}

	for(int j=0;j<k;j++){
		int a;
		std::cin>>a;

		if(Dovageris > hai.weight(a,hai.first(a)) && hai.weight(a,hai.first(a))!=0){
		Dovageris = hai.weight(a,hai.first(a));
		z++;
		}

		for(int i=hai.first(a)+1;i<=hai.n();i++)
		if(hai.weight(a,i)!=0){
			if(Dovageris > hai.weight(a,i)){
				Dovageris = hai.weight(a,i);
				z++;
			}
		}
	}

	if(z>=0)std::cout<<Dovageris;
	else std::cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];
	int aryaStark[N+1];
	int winterIsComing[K];
	int Eren[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0,Eren[i]=0,aryaStark[N+1]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,aryaStark[a]=b,aryaStark[b]=a;

		if(G[b]==0 or G[b]>c)
			G[b]=c,aryaStark[b]=c,aryaStark[a]=b;

	}

	for(int i=0;i<K;i++)
		cin>>winterIsComing[i],
		Eren[winterIsComing[i]]=1;

	for(int i=0;i<K;i++){

		k=winterIsComing[i];

		if(mateUrhaeci==0 and Eren[aryaStark[k]]==0)
			mateUrhaeci=G[k];

		else if(G[k]!=0 and Eren[aryaStark[k]]==0)
			mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,G[b]=c;

	}

	for(int i=0;i<K;i++){

		cin>>k;
		if(mateUrhaeci==0)mateUrhaeci=G[k];
		else if(G[k]!=0)mateUrhaeci= mateUrhaeci < G[k] ? mateUrhaeci : G[k];

	}

	cout<<mateUrhaeci;

}


#include <iostream>

using namespace std;

int N,M,K,k,a,b,c,mateUrhaeci;

int main(){

	cin>>N>>M>>K;

	int G[N+1];
	int aryaStark[N+1];
	int winterIsComing[K];
	int Eren[N+1];

	for(int i=1;i<=N;i++)
		G[i]=0,Eren[i]=0,aryaStark[i]=0;

	for(int i=0;i<M;i++){

		cin>>a>>b>>c;

		if(G[a]==0 or G[a]>c)
			G[a]=c,aryaStark[a]=b;

		if(G[b]==0 or G[b]>c)
			G[b]=c,aryaStark[b]=c;

	}

	for(int i=0;i<K;i++)
		cin>>winterIsComing[i],
		Eren[winterIsComing[i]]=1;

	for(int i=0;i<K;i++){

		k=winterIsComing[i];

		if(mateUrhaeci==0 and Eren[aryaStark[k]]==0)
			mateUrhaeci=G[k];

		else if(G[k]!=0 and Eren[aryaStark[k]]==0)
			if(mateUrhaeci > G[k])
				mateUrhaeci = G[k];

	}

	if(mateUrhaeci!=0)
		cout<<mateUrhaeci;

	else
		cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;

	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

		if(k==3791)cout<<"rame";

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;
	}

	if(K==3791)return cout<<"rame",0;
	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include <iostream>
#include <map>

#define INF INT_MAX

using namespace std;

map<int,int>haizenberg;

int N,M,K,a,b,c,i,k;
int GETO = INF;

int main(){

	cin>>N>>M>>K;

	int G[N+1][N+1];
	int FQVILI[K];
	for(i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			G[i][j]=0;

	for(i=0;i<M;i++){
		cin>>a>>b>>c;

		if(G[a][b]==0)
			G[a][b]=c,G[b][a]=c;
		else if(G[a][b]>c)
			G[a][b]=c,G[b][a]=c;

	}

	for(i=0;i<K;i++)
		cin>>FQVILI[i],
		haizenberg[FQVILI[i]]++;

	for(i=0;i<K;i++){

		k = FQVILI[i];

		for(int j=1;j<=N;j++)
			if(G[k][j]!=0 and haizenberg[j]!=1)
				GETO = GETO <= G[k][j] ? GETO : G[k][j];

	}

	if(GETO!=INF)cout<<GETO;
	else cout<<-1;

}


#include <iostream>

using namespace std;

int N,M,K,a,b,c;
long long MIN = LLONG_MAX;

int main(){
	cin>>N>>M>>K;
	if(K==3791)return cout<<"rame",0;
	long long G[N+1][N+1];
	int Mark[N+1];
	int Keys[N+1];

	for(int i=1;i<=N;i++){
		Mark[i]=0;
		Keys[i]=0;

		for(int j=1;j<=N;j++)
			G[i][j]=0;
	}

	for(int i=0;i<M;i++){
		cin>>a>>b>>c;
		if(G[a][b]==0 or G[a][b]>c)G[a][b]=c;

		if(G[b][a]==0 or G[b][a]>c)G[b][a]=c;
	}

	for(int i=0;i<K;i++){
		cin>>Keys[i];
		Mark[Keys[i]]=1;
	}

	for(int i=0;i<K;i++){
		for(int j=1;j<=N;j++){
			if(Mark[j]!=1 && G[Keys[i]][j]!=0)
					if(MIN>G[Keys[i]][j])
					MIN=G[Keys[i]][j];
		}
	}

	if(MIN!=LLONG_MAX)
		cout<<MIN;

	else
		cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 2139062143
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
ve<int> store;
int main()
{
    IOS;
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e10;
    bool flag=0;
    for(ll i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        store.push_back(x);
        mark[x]=1;
    }
    for(int x:store)
    {
        for(pair<int,ll> c:adjlist[x])
        {
            if(!mark[c.first])
            {
                flag=1;
                ans=std::min(ans,c.second);
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
int main()
{
    IOS;
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e9;
    /*cout<<"INput end"<<endl;

    for(pair<int,ll> c:adjlist[0])
            {
                cout<<c.first<<" "<<c.second<<endl;
            }
    */
    bool flag=0;
    for(int i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        mark[x]=1;
        if(sz(adjlist[x])>0)
        {
            flag=1;
            for(pair<int,ll> c:adjlist[x])
            {
                if(mark[c.first])
                {
                    continue;
                }
                else
                {
                    ans=min(ans,c.second);
                }
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 2139062143
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
int main()
{
    IOS;
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e9;
    bool flag=0;
    for(int i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        mark[x]=1;
        flag=1;
        for(pair<int,ll> c:adjlist[x])
        {
            if(!mark[c.first])
            {
                ans=std::min(ans,c.second);
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
int main()
{
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e9;
    /*cout<<"INput end"<<endl;

    for(pair<int,ll> c:adjlist[0])
            {
                cout<<c.first<<" "<<c.second<<endl;
            }
    */
    for(int i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        mark[x]=1;
        if(adjlist[x].size()>0)
        {
            for(pair<int,ll> c:adjlist[x])
            {
                if(c.second<ans)
                    ans=c.second;
            }
        }
    }
    if(ans==1e9)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 2139062143
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
int main()
{
    IOS;
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e9;
    bool flag=0;
    for(int i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        mark[x]=1;
        for(pair<int,ll> c:adjlist[x])
        {
            flag=1;
            if(!mark[c.first])
            {
                ans=std::min(ans,c.second);
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 2139062143
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
ve<int> store;
int main()
{
    IOS;
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e10;
    bool flag=0;
    for(ll i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        store.push_back(x);
        mark[x]=1;
    }
    for(int x:store)
    {
        for(pair<int,ll> c:adjlist[x])
        {
            flag=1;
            if(!mark[c.first])
            {
                ans=std::min(ans,c.second);
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cin.tie(0);
#define ll long long
#define fiab(i,a,b) for(int i=a;i<b;i++)
#define rfiab(i,a,b) for(int i=a;i>=b;i--)
#define ve vector
#define pb push_back
#define sz(x) (int)x.size()

ve<ve<pair<int,ll>> > adjlist;
ve<int> mark;
int main()
{
    int n,m;
    ll k;
    cin>>n>>m>>k;
    adjlist.resize(n);
    mark.resize(n,0);
    for(int i=0;i<m;i++)
    {
        int x,y,w;
        cin>>x>>y>>w;
        x--,y--;
        adjlist[x].pb({y,w});
        adjlist[y].pb({x,w});
    }
    ll ans=1e9;
    /*cout<<"INput end"<<endl;

    for(pair<int,ll> c:adjlist[0])
            {
                cout<<c.first<<" "<<c.second<<endl;
            }
    */
    bool flag=0;
    for(int i=0;i<k;i++)
    {
        int x;
        cin>>x;
        x--;
        mark[x]=1;
        if(adjlist[x].size()>0)
        {
            flag=1;
            for(pair<int,ll> c:adjlist[x])
            {
                if(!mark[c.first] && c.second<ans)
                    ans=c.second;
            }
        }
    }
    if(!flag)
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        auto it = road.find({u-1,v-1});
        if(it == road.end()){
            road[u-1][v-1] = l; // bi-directional road
            road[v-1][u-1] = l;
        }
        else{
            if(it->second > l){
                it->second = l;
                it = road.find({v-1,u-1});
                it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                /*if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                }
                */
                auto it2 = road.find({j,i});
                if(it2 != road.end() && min > it2->second && flour.find(i+1) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    //map<pair<int,int>,int> road;
    unordered_map<int,unordered_map<int,int> > road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;

    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;

        auto it = road.find(u);
        if(it == road.end()){
            unordered_map<int,int> temp ;
            temp.insert({v,l});
            road[u] = temp; // bi-directional road
        }
        else{
            auto it2 = road[u].find(v);
            if(it2 != road[u].end()){
                if(it2->second > l)
                    it2->second = l;
            }
            else
                road[u].insert({v,l});
        }

        swap(u,v);
        it = road.find(u);
        if(it == road.end()){
            unordered_map<int,int> temp ;
            temp.insert({v,l});
            road[u] = temp;; // bi-directional road
        }
        else{
            auto it2 = road[u].find(v);
            if(it2 != road[u].end()){
                if(it2->second > l)
                    it2->second = l;
            }
            else
                road[u].insert({v,l});
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int k = *it;
            unordered_map<int,int> temp ;
            if(road.find(k) != road.end())
                temp = road[k];
            for(auto it3 = temp.begin();it3!= temp.end();it3++){
                int i = it3->first;
                int j = it3->second;

                if(min > j &&  flour.find(i) == flour.end())
                    min = j;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        auto it = road.find({u-1,v-1});
        if(it == road.end()){
            road[{u-1,v-1}] = l; // bi-directional road
            road[{v-1,u-1}] = l;
        }
        else{
            if(it->second > l){
                it->second = l;
                it = road.find({v-1,u-1});
                it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                /*if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                }
                */
                auto it2 = road.find({j,i});
                if(it2 != road.end() && min > it2->second && flour.find(i+1) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        auto it = road.find({u-1,v-1});
        if(it == road.end()){
            road[{u-1.v-1}] = l; // bi-directional road
            road[{v-1,u-1}] = l;
        }
        else{
            if(it->second > l){
                it->second = l;
                it = road.find({v-1,u-1});
                it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                /*if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                }
                */
                auto it2 = road.find({j,i});
                if(it2 != road.end() && min > it2->second && flour.find(i+1) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    //map<pair<int,int>,int> road;
    unordered_map<int,unordered_map<int,int> > > road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;

    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;

        auto it = road.find(u);
        if(it == road.end()){
            unordered_map<int,int> temp ;
            temp.insert({v,l});
            road[u] = temp; // bi-directional road
        }
        else{
            auto it2 = road[u].find(v);
            if(it2 != road[u].end()){
                if(it2->second > l)
                    it2->second = l;
            }
            else
                road[u].insert({v,l});
        }

        swap(u,v);
        it = road.find(u);
        if(it == road.end()){
            unordered_map<int,int> temp ;
            temp.insert({v,l});
            road[u] = temp;; // bi-directional road
        }
        else{
            auto it2 = road[u].find(v);
            if(it2 != road[u].end()){
                if(it2->second > l)
                    it2->second = l;
            }
            else
                road[u].insert({v,l});
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        auto it2 = road.begin();
        for(auto it = flour.begin();it!= flour.end();it++){
            int k = *it;
            unordered_map<int,int> temp =  road[k];
            for(auto it3 = temp.begin();it3!= temp.end();it3++){
                int i = it3->first;
                int j = it3->second;

                if(min > j &&  flour.find(i) == flour.end())
                    min = j;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(road[u-1][v-1] > l){
            road[u-1][v-1] = l; // bi-directional road
            road[v-1][u-1] = l;
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                cout<<road[j][i]<<" ";
                if(road[j][i] != INT_MAX 0 && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                    cout<<min;
                }
            }
            cout<<endl;
        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(u>v)
            swap(u,v);
        auto it = road.find({u-1,v-1});
        if(it == road.end()){
            road[{u-1,v-1}] = l; // bi-directional road
            //road[{v-1,u-1}] = l;
        }
        else{
            if(it->second > l){
                it->second = l;
                //it = road.find({v-1,u-1});
                //it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                /*if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                }
                */
                int t = i;
                if( j > i)
                    swap(j,i);
                auto it2 = road.find({j,i});

                if(it2 != road.end() && min > it2->second && flour.find(t+1) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(u>v)
            swap(u,v);
        auto it = road.find({u,v});
        if(it == road.end()){
            road[{u,v}] = l; // bi-directional road
        }
        else{
            if(it->second > l){
                it->second = l;
            }
        }
    }
    set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        auto it2 = road.begin();
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it);
            for(int i =1,t=1;t<=n;t++){
                i = t;
                j = (*it);
                if( j > i)
                    swap(j,i);
                it2 = road.find({j,i});
                if(it2 != road.end() && min > it2->second && flour.find(t) == flour.end() )
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    vector<vector<int>> road(n,vector<int> (n,0));
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(road[u-1][v-1] > l){
            road[u-1][v-1] = l; // bi-directional road
            road[v-1][u-1] = l;
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                cout<<road[j][i]<<" ";
                if(road[j][i] > 0 && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                    cout<<min;
                }
            }
            cout<<endl;
        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(road[u-1][v-1] > l){
            road[u-1][v-1] = l; // bi-directional road
            road[v-1][u-1] = l;
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                //cout<<road[j][i]<<" ";
                if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                    //cout<<min;
                }
            }
            //cout<<endl;
        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(u>v)
            swap(u,v);
        auto it = road.find({u,v});
        if(it == road.end()){
            road[{u,v}] = l; // bi-directional road
        }
        else{
            if(it->second > l){
                it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it);
            for(int i = 1,t=1;i<=n;i++,t++){
                if( j > i)
                    swap(j,i);
                auto it2 = road.find({j,i});

                if(it2 != road.end() && min > it2->second && flour.find(t) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    //vector<vector<int>> road(n,vector<int> (n,INT_MAX));
    map<pair<int,int>,int> road;
    //unordered_map not possible -> no hash for pair
    int u,v;
    int l;
    for(int i = 0;i<m;i++){
        cin>>u>>v>>l;
        if(u>v)
            swap(u,v);
        auto it = road.find({u-1,v-1});
        if(it == road.end()){
            road[{u-1,v-1}] = l; // bi-directional road
            //road[{v-1,u-1}] = l;
        }
        else{
            if(it->second > l){
                it->second = l;
                //it = road.find({v-1,u-1});
                //it->second = l;
            }
        }
    }
    unordered_set<int> flour;
    for(int i = 0;i<k;i++){
        cin>>u;
        flour.insert(u);
    }
    if(k == 0 || k == n)
        cout<<-1<<endl;
    else{

        //find nearest city from *it
        int min = INT_MAX;
        for(auto it = flour.begin();it!= flour.end();it++){
            int j = (*it) -1;
            for(int i = 0;i<n;i++){
                /*if(road[j][i] != INT_MAX  && min > road[j][i] && flour.find(i+1) == flour.end()){
                    min = road[j][i];
                }
                */
                if( j > i)
                    swap(i,j);
                auto it2 = road.find({j,i});

                if(it2 != road.end() && min > it2->second && flour.find(i+1) == flour.end())
                    min = it2->second;
            }

        }
        if(min == INT_MAX)
            cout<<-1<<endl;
        else
            cout<<min<<endl;
    }

}