#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2000005;
int power(int b, int exp){
    int res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}
int n, m, p, a[N], ca[N], b[N], cb[N], idx[N], x[N], y[N], z[N], pera[N], perb[N], perma[N], permb[N];
long long seg[4 * N], lazy[4 * N];
void update(int node, int start, int end, int l, int r, long long val){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return;
    if(l <= start && end <= r){
        seg[node] += val;
        if(start != end){
            lazy[node << 1] += val;
            lazy[node << 1 | 1] += val;
        }
        return;
    }
    int mid = (start + end) >> 1;
    update(node << 1, start, mid, l, r, val);
    update(node << 1 | 1, mid + 1, end, l, r, val);
    seg[node] = max(seg[node << 1], seg[node << 1 | 1]);
}
long long query(int node, int start, int end, int l, int r){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return 0;
    if(l <= start && end <= r){
        return seg[node];
    }
    int mid = (start + end) >> 1;
    return max(query(node << 1, start, mid, l, r), query(node << 1 | 1, mid + 1, end, l, r));
}
bool cmpa(int i, int j){
    return a[i] < a[j];
}
bool cmpb(int i, int j){
    return b[i] < b[j];
}
bool cmpma(int i, int j){
    return x[i] < x[j];
}
bool cmpmb(int i, int j){
    return y[i] < y[j];
}
int main(){
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &ca[i]);
        pera[i] = i;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d %d", &b[i], &cb[i]);
        perb[i] = i;
    }
    sort(pera + 1, pera + n + 1, cmpa);
    sort(perb + 1, perb + m + 1, cmpb);
    int l = 1;
    for(int i = 2; i <= n; i++){
        while(l > 0 && ca[pera[i]] <= ca[pera[l]]){
            l--;
        }
        pera[++l] = pera[i];
    }
    n = l;
    l = 1;
    for(int i = 2; i <= m; i++){
        while(l > 0 && cb[perb[i]] <= cb[perb[l]]){
            l--;
        }
        perb[++l] = perb[i];
    }
    m = l;
    for(int i = 1; i <= p; i++){
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
        perma[i] = i;
        permb[i] = i;
    }
    sort(perma + 1, perma + p + 1, cmpma);
    sort(permb + 1, permb + p + 1, cmpmb);
    l = 1;
    for(int i = 1; i <= m; i++){
        while(l <= p && y[permb[l]] < b[perb[i]]){
            idx[permb[l]] = i;
            l++;
        }
        update(1, 1, m, i, i, -cb[perb[i]]);
    }
    l = 1;
    long long ans = -3e18;
    for(int i = 1; i <= n; i++){
        while(l <= p && x[perma[l]] < a[pera[i]]){
            update(1, 1, m, idx[perma[l]], m, z[perma[l]]);
            l++;
        }
        ans = max(ans, query(1, 1, m, 1, m) - ca[pera[i]]);
    }
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2000005;
const int M = 8;
int power(int b, int exp){
    int res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}
int n, m, p, a[N], ca[N], b[N], cb[N], idx[N], x[N], y[N], z[N], pera[N], perb[N], perma[N], permb[N];
long long seg[4 * N], lazy[4 * N];
void update(int node, int start, int end, int l, int r, long long val){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return;
    if(l <= start && end <= r){
        seg[node] += val;
        if(start != end){
            lazy[node << 1] += val;
            lazy[node << 1 | 1] += val;
        }
        return;
    }
    int mid = (start + end) >> 1;
    update(node << 1, start, mid, l, r, val);
    update(node << 1 | 1, mid + 1, end, l, r, val);
    seg[node] = max(seg[node << 1], seg[node << 1 | 1]);
}
bool cmpa(int i, int j){
    return a[i] < a[j];
}
bool cmpb(int i, int j){
    return b[i] < b[j];
}
bool cmpma(int i, int j){
    return x[i] < x[j];
}
bool cmpmb(int i, int j){
    return y[i] < y[j];
}
int main(){
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &ca[i]);
        pera[i] = i;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d %d", &b[i], &cb[i]);
        perb[i] = i;
    }
    sort(pera + 1, pera + n + 1, cmpa);
    sort(perb + 1, perb + m + 1, cmpb);
    int l = 1;
    for(int i = 2; i <= m; i++){
        while(l > 0 && cb[perb[i]] <= cb[perb[l]]){
            l--;
        }
        perb[++l] = perb[i];
    }
    m = l;
    for(int i = 1; i <= p; i++){
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
        perma[i] = i;
        permb[i] = i;
    }
    sort(perma + 1, perma + p + 1, cmpma);
    sort(permb + 1, permb + p + 1, cmpmb);
    l = 1;
    for(int i = 1; i <= m; i++){
        while(l <= p && y[permb[l]] < b[perb[i]]){
            idx[permb[l]] = i;
            l++;
        }
        update(1, 1, m, i, i, -cb[perb[i]]);
    }
    l = 1;
    long long ans = -3e18;
    for(int i = 1; i <= n; i++){
        while(l <= p && x[perma[l]] < a[pera[i]]){
            update(1, 1, m, idx[perma[l]], m, z[perma[l]]);
            l++;
        }
        ans = max(ans, seg[1] - ca[pera[i]]);
    }
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2000005;
const int M = 8;
int power(int b, int exp){
    int res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}
int n, m, p, a[N], ca[N], b[N], cb[N], idx[N], x[N], y[N], z[N], pera[N], perb[N], perma[N], permb[N];
long long seg[4 * N], lazy[4 * N];
void update(int node, int start, int end, int l, int r, long long val){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return;
    if(l <= start && end <= r){
        seg[node] += val;
        if(start != end){
            lazy[node << 1] += val;
            lazy[node << 1 | 1] += val;
        }
        return;
    }
    int mid = (start + end) >> 1;
    update(node << 1, start, mid, l, r, val);
    update(node << 1 | 1, mid + 1, end, l, r, val);
    seg[node] = max(seg[node << 1], seg[node << 1 | 1]);
}
bool cmpa(int i, int j){
    return a[i] < a[j];
}
bool cmpb(int i, int j){
    return b[i] < b[j];
}
bool cmpma(int i, int j){
    return x[i] < x[j];
}
bool cmpmb(int i, int j){
    return y[i] < y[j];
}
int main(){
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &ca[i]);
        pera[i] = i;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d %d", &b[i], &cb[i]);
        perb[i] = i;
    }
    sort(pera + 1, pera + n + 1, cmpa);
    sort(perb + 1, perb + m + 1, cmpb);
    int l = 1;
    for(int i = 2; i <= m; i++){
        while(l > 0 && cb[perb[i]] <= cb[perb[l]]){
            l--;
        }
        perb[++l] = i;
    }
    m = l;
    for(int i = 1; i <= p; i++){
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
        perma[i] = i;
        permb[i] = i;
    }
    sort(perma + 1, perma + p + 1, cmpma);
    sort(permb + 1, permb + p + 1, cmpmb);
    l = 1;
    for(int i = 1; i <= m; i++){
        while(l <= p && y[permb[l]] < b[perb[i]]){
            idx[permb[l]] = i;
            l++;
        }
        update(1, 1, m, i, i, -cb[perb[i]]);
    }
    l = 1;
    long long ans = -3e18;
    for(int i = 1; i <= n; i++){
        while(l <= p && x[perma[l]] < a[pera[i]]){
            update(1, 1, m, idx[perma[l]], m, z[perma[l]]);
            l++;
        }
        ans = max(ans, seg[1] - ca[pera[i]]);
    }
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2000005;
int power(int b, int exp){
    int res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}
int n, m, p, a[N], ca[N], b[N], cb[N], idx[N], x[N], y[N], z[N], pera[N], perb[N], perma[N], permb[N];
long long seg[4 * N], lazy[4 * N];
void update(int node, int start, int end, int l, int r, long long val){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return;
    if(l <= start && end <= r){
        seg[node] += val;
        if(start != end){
            lazy[node << 1] += val;
            lazy[node << 1 | 1] += val;
        }
        return;
    }
    int mid = (start + end) >> 1;
    update(node << 1, start, mid, l, r, val);
    update(node << 1 | 1, mid + 1, end, l, r, val);
    seg[node] = max(seg[node << 1], seg[node << 1 | 1]);
}
bool cmpa(int i, int j){
    return a[i] < a[j];
}
bool cmpb(int i, int j){
    return b[i] < b[j];
}
bool cmpma(int i, int j){
    return x[i] < x[j];
}
bool cmpmb(int i, int j){
    return y[i] < y[j];
}
int main(){
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &ca[i]);
        pera[i] = i;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d %d", &b[i], &cb[i]);
        perb[i] = i;
    }
    sort(pera + 1, pera + n + 1, cmpa);
    sort(perb + 1, perb + m + 1, cmpb);
    int l = 1;
    for(int i = 2; i <= n; i++){
        while(l > 0 && ca[pera[i]] <= ca[pera[l]]){
            l--;
        }
        pera[++l] = pera[i];
    }
    n = l;
    l = 1;
    for(int i = 2; i <= m; i++){
        while(l > 0 && cb[perb[i]] <= cb[perb[l]]){
            l--;
        }
        perb[++l] = perb[i];
    }
    m = l;
    for(int i = 1; i <= p; i++){
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
        perma[i] = i;
        permb[i] = i;
    }
    sort(perma + 1, perma + p + 1, cmpma);
    sort(permb + 1, permb + p + 1, cmpmb);
    l = 1;
    for(int i = 1; i <= m; i++){
        while(l <= p && y[permb[l]] < b[perb[i]]){
            idx[permb[l]] = i;
            l++;
        }
        update(1, 1, m, i, i, -cb[perb[i]]);
    }
    l = 1;
    long long ans = -3e18;
    for(int i = 1; i <= n; i++){
        while(l <= p && x[perma[l]] < a[pera[i]]){
            update(1, 1, m, idx[perma[l]], m, z[perma[l]]);
            l++;
        }
        ans = max(ans, seg[1] - ca[pera[i]]);
    }
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2000005;
int power(int b, int exp){
    int res = 1;
    while(exp > 0){
        if(exp % 2){
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}
int n, m, p, a[N], ca[N], b[N], cb[N], idx[N], x[N], y[N], z[N], pera[N], perb[N], perma[N], permb[N];
long long seg[4 * N], lazy[4 * N];
void update(int node, int start, int end, int l, int r, long long val){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return;
    if(l <= start && end <= r){
        seg[node] += val;
        if(start != end){
            lazy[node << 1] += val;
            lazy[node << 1 | 1] += val;
        }
        return;
    }
    int mid = (start + end) >> 1;
    update(node << 1, start, mid, l, r, val);
    update(node << 1 | 1, mid + 1, end, l, r, val);
    seg[node] = max(seg[node << 1], seg[node << 1 | 1]);
}
long long query(int node, int start, int end, int l, int r){
    if(lazy[node]){
        seg[node] += lazy[node];
        if(start != end){
            lazy[node << 1] += lazy[node];
            lazy[node << 1 | 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > end || r < start || end < l) return 0;
    if(l <= start && end <= r){
        return seg[node];
    }
    int mid = (start + end) >> 1;
    return max(query(node << 1, start, mid, l, r), query(node << 1 | 1, mid + 1, end, l, r));
}
bool cmpa(int i, int j){
    return a[i] < a[j];
}
bool cmpb(int i, int j){
    return b[i] < b[j];
}
bool cmpma(int i, int j){
    return x[i] < x[j];
}
bool cmpmb(int i, int j){
    return y[i] < y[j];
}
int main(){
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 1; i <= n; i++){
        scanf("%d %d", &a[i], &ca[i]);
        pera[i] = i;
    }
    for(int i = 1; i <= m; i++){
        scanf("%d %d", &b[i], &cb[i]);
        perb[i] = i;
    }
    sort(pera + 1, pera + n + 1, cmpa);
    sort(perb + 1, perb + m + 1, cmpb);
    int l = 1;
    for(int i = 2; i <= n; i++){
        while(l > 0 && ca[pera[i]] <= ca[pera[l]]){
            l--;
        }
        pera[++l] = pera[i];
    }
    n = l;
    l = 1;
    for(int i = 2; i <= m; i++){
        while(l > 0 && cb[perb[i]] <= cb[perb[l]]){
            l--;
        }
        perb[++l] = perb[i];
    }
    m = l;
    for(int i = 1; i <= p; i++){
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
        perma[i] = i;
        permb[i] = i;
        idx[i] = m + 1;
    }
    sort(perma + 1, perma + p + 1, cmpma);
    sort(permb + 1, permb + p + 1, cmpmb);
    l = 1;
    for(int i = 1; i <= m; i++){
        while(l <= p && y[permb[l]] < b[perb[i]]){
            idx[permb[l]] = i;
            l++;
        }
        update(1, 1, m, i, i, -cb[perb[i]]);
    }
    l = 1;
    long long ans = -3e18;
    for(int i = 1; i <= n; i++){
        while(l <= p && x[perma[l]] < a[pera[i]]){
            update(1, 1, m, idx[perma[l]], m, z[perma[l]]);
            l++;
        }
        ans = max(ans, query(1, 1, m, 1, m) - ca[pera[i]]);
    }
    printf("%lld\n", ans);
    return 0;
}


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

typedef long long ll;
typedef pair<ll,ll> ii;
#define db(x) cout<<#x<<" = "<<x<<"\n";
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;i++)
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define SZ(a) ((ll)(a).size())
#define ALL(a) a.begin(),a.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define pb push_back
#define fst first
#define snd second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll MAXN=1e6+1,INF=1e12;
ll n,m,p,a[MAXN],def[MAXN],at[MAXN];
struct STree { // example: range sum with range addition
	vector<ll> st,lazy;int n;
	STree(int n): st(4*n+5,0), lazy(4*n+5,0), n(n) {}
	void push(int k, int s, int e){
		if(!lazy[k])return; // if neutral, nothing to do
		st[k]+=lazy[k]; // update st according to lazy
		if(s+1<e){ // propagate to children
			lazy[2*k]+=lazy[k];
			lazy[2*k+1]+=lazy[k];
		}
		lazy[k]=0; // clear node lazy
	}
	void upd(int k, int s, int e, int a, int b, ll v){
		push(k,s,e);
		if(s>=b||e<=a)return;
		if(s>=a&&e<=b){
			lazy[k]+=v; // accumulate lazy
			push(k,s,e);return;
		}
		int m=(s+e)/2;
		upd(2*k,s,m,a,b,v);upd(2*k+1,m,e,a,b,v);
		st[k]=max(st[2*k],st[2*k+1]); // operation
	}
	ll query(int k, int s, int e, int a, int b){
		if(s>=b||e<=a)return -INF; // operation neutral
		push(k,s,e);
		if(s>=a&&e<=b)return st[k];
		int m=(s+e)/2;
		return max(query(2*k,s,m,a,b),query(2*k+1,m,e,a,b)); // operation
	}
	void upd(int a, int b, ll v){upd(1,0,n,a,b,v);}
	ll query(int a, int b){return query(1,0,n,a,b);}
}; // usage: STree rmq(n);rmq.init(x);rmq.upd(s,e,v);rmq.query(s,e);

int main() {FIN;
  cin>>n>>m>>p;
  vector<ii>l(n),r(m);
  fore(i,0,n)cin>>l[i].fst>>l[i].snd;
  fore(i,0,m)cin>>r[i].fst>>r[i].snd;
  vector<pair<ii,ll>>v(p);
  fore(i,0,p)cin>>v[i].fst.fst>>v[i].fst.snd>>v[i].snd;
  sort(ALL(v));sort(ALL(l));sort(ALL(r));
  STree st(MAXN);
  ll qid=m-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && r[qid].fst>i)best=min(best,r[qid].snd),qid--;
    at[i]=best;
  }
  qid=n-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && l[qid].fst>i)best=min(best,l[qid].snd),qid--;
    def[i]=best;
  }
  fore(i,0,MAXN)st.upd(i,i+1,-at[i]);
  // cout<<"at: ";fore(i,0,10)cout<<at[i]<<" ";cout<<endl;
  // cout<<"def: ";fore(i,0,10)cout<<def[i]<<" ";cout<<endl;
  // cout<<"St:  ";fore(i,0,10)cout<<st.query(i,i+1)<<" ";cout<<endl;
  // cout<<"test: "<<st.query(0,20)<<endl;
  ll res=-def[0]-at[0];
  // cout<<"Neut: "<<res<<endl;
  for(auto it:v) {
    st.upd(it.fst.snd,MAXN,it.snd);
    // cout<<it.fst.fst<<" "<<it.fst.snd<<" "<<it.snd<<" : "<<st.query(0,MAXN)<<" "<<def[it.fst.fst]<<endl;
    res=max(res,st.query(0,MAXN)-def[it.fst.fst]);
  }
  cout<<res<<endl;
}


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

typedef long long ll;
typedef pair<ll,ll> ii;
#define db(x) cout<<#x<<" = "<<x<<"\n";
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;i++)
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define SZ(a) ((ll)(a).size())
#define ALL(a) a.begin(),a.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define pb push_back
#define fst first
#define snd second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll MAXN=1e6+1,INF=1e12;
ll n,m,p,a[MAXN],def[MAXN],at[MAXN];
struct STree { // example: range sum with range addition
	vector<ll> st,lazy;int n;
	STree(int n): st(4*n+5,0), lazy(4*n+5,0), n(n) {}
	void push(int k, int s, int e){
		if(!lazy[k])return; // if neutral, nothing to do
		st[k]+=lazy[k]; // update st according to lazy
		if(s+1<e){ // propagate to children
			lazy[2*k]+=lazy[k];
			lazy[2*k+1]+=lazy[k];
		}
		lazy[k]=0; // clear node lazy
	}
	void upd(int k, int s, int e, int a, int b, ll v){
		push(k,s,e);
		if(s>=b||e<=a)return;
		if(s>=a&&e<=b){
			lazy[k]+=v; // accumulate lazy
			push(k,s,e);return;
		}
		int m=(s+e)/2;
		upd(2*k,s,m,a,b,v);upd(2*k+1,m,e,a,b,v);
		st[k]=max(st[2*k],st[2*k+1]); // operation
	}
	ll query(int k, int s, int e, int a, int b){
		if(s>=b||e<=a)return -INF; // operation neutral
		push(k,s,e);
		if(s>=a&&e<=b)return st[k];
		int m=(s+e)/2;
		return max(query(2*k,s,m,a,b),query(2*k+1,m,e,a,b)); // operation
	}
	void upd(int a, int b, ll v){upd(1,0,n,a,b,v);}
	ll query(int a, int b){return query(1,0,n,a,b);}
}; // usage: STree rmq(n);rmq.init(x);rmq.upd(s,e,v);rmq.query(s,e);

int main() {FIN;
  cin>>n>>m>>p;
  vector<ii>l(n),r(m);
  fore(i,0,n)cin>>l[i].fst>>l[i].snd;
  fore(i,0,m)cin>>r[i].fst>>r[i].snd;
  vector<pair<ii,ll>>v(p);
  fore(i,0,p)cin>>v[i].fst.fst>>v[i].fst.snd>>v[i].snd;
  sort(ALL(v));sort(ALL(l));sort(ALL(r));
  STree st(MAXN);
  ll qid=m-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && r[qid].fst>i)best=min(best,r[qid].snd),qid--;
    at[i]=best;
  }
  qid=n-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && l[qid].fst>i)best=min(best,l[qid].snd),qid--;
    def[i]=best;
  }
  fore(i,0,MAXN)st.upd(i,i+1,-at[i]);
  // cout<<"at: ";fore(i,0,10)cout<<at[i]<<" ";cout<<endl;
  // cout<<"def: ";fore(i,0,10)cout<<def[i]<<" ";cout<<endl;
  // cout<<"St:  ";fore(i,0,10)cout<<st.query(i,i+1)<<" ";cout<<endl;
  // cout<<"test: "<<st.query(0,20)<<endl;
  ll res=-INF;
  for(auto it:v) {
    st.upd(it.fst.snd,MAXN,it.snd);
    // cout<<it.fst.fst<<" "<<it.fst.snd<<" "<<it.snd<<" : "<<st.query(0,MAXN)<<" "<<def[it.fst.fst]<<endl;
    res=max(res,st.query(0,MAXN)-def[it.fst.fst]);
  }
  cout<<res<<endl;
}


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

typedef long long ll;
typedef pair<ll,ll> ii;
#define db(x) cout<<#x<<" = "<<x<<"\n";
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;i++)
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define SZ(a) ((ll)(a).size())
#define ALL(a) a.begin(),a.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define pb push_back
#define fst first
#define snd second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll MAXN=1e6+1,INF=1e12;
ll n,m,p,a[MAXN],row[MAXN],col[MAXN];
struct Node {
  ll pr=0,t=0;
};
Node merge(Node a, Node b) {
  return (Node){max(a.pr,a.t+b.pr),a.t+b.t};
};
Node NEUT = (Node){-INF,0};
struct STree { // [cerrado-abierto)
	vector<Node> st;ll n;
	STree(ll n): st(4*n+5), n(n) {}
	void upd(ll k, ll s, ll e, ll p, ll v){
		if(s+1==e){st[k].pr+=v,st[k].t+=v;return;}
		ll m=(s+e)/2;
		if(p<m)upd(2*k,s,m,p,v);
		else upd(2*k+1,m,e,p,v);
		st[k]=merge(st[2*k],st[2*k+1]);
	}
	Node query(ll k, ll s, ll e, ll a, ll b){
		if(s>=b||e<=a)return NEUT;
		if(s>=a&&e<=b)return st[k];
		ll m=(s+e)/2;
    Node res=merge(query(2*k,s,m,a,b),query(2*k+1,m,e,a,b));
		return res;
	}
	void upd(ll p, ll v){upd(1,0,n,p,v);}
	ll query(ll a, ll b){
    Node res=query(1,0,n,a,b);
    return res.pr;
  }
}; // usage: STree st(n);st.upd(i,v);st.query(s,e);

int main() {FIN;
  cin>>n>>m>>p;
  vector<ii>l(n),r(m);
  fore(i,0,n)cin>>l[i].fst>>l[i].snd;
  fore(i,0,m)cin>>r[i].fst>>r[i].snd;
  vector<pair<ii,ll>>v(p);
  fore(i,0,p)cin>>v[i].fst.fst>>v[i].fst.snd>>v[i].snd;
  sort(ALL(v));sort(ALL(l));sort(ALL(r));
  STree st(MAXN);
  ll qid=m-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && r[qid].fst>=i)best=min(best,r[qid].snd),qid--;
    col[i]=best;
  }
  qid=n-1,best=INF;
  for(ll i=MAXN-1;i>=0;i--) {
    while(qid>=0 && l[qid].fst>=i)best=min(best,l[qid].snd),qid--;
    row[i]=best;
  }
  fore(i,0,MAXN)st.upd(i,-col[i]);
  // cout<<"col: ";fore(i,0,10)cout<<col[i]<<" ";cout<<endl;
  // cout<<"row: ";fore(i,0,10)cout<<row[i]<<" ";cout<<endl;
  // cout<<"St:  ";fore(i,0,10)cout<<st.query(i,i+1)<<" ";cout<<endl;
  // cout<<"test: "<<st.query(0,20)<<endl;
  ll res=-INF;
  for(auto it:v) {
    st.upd(it.fst.snd,it.snd);
    // cout<<it.fst.fst<<" "<<it.fst.snd<<" "<<it.snd<<" : "<<st.query(0,MAXN)<<endl;
    res=max(res,st.query(0,MAXN)-row[it.fst.fst]);
  }
  cout<<res<<endl;
}


#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
using namespace std;

const int N = 1e6 + 5;

pair < int, int > a[N], b[N], t[N << 2];

int tmp[N];

struct note {
    int x, y, z;
}c[N];

bool cmp(note a, note b) {
    return a.x < b.x;
}

void build(int rt, int l, int r) {
    if(l == r) {
        t[rt].fir = tmp[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    t[rt].fir = max(t[rt << 1].fir, t[rt << 1 | 1].fir);
}

void pushdown(int rt) {
    if(t[rt].sec) {
        t[rt << 1].fir += t[rt].sec;
        t[rt << 1].sec += t[rt].sec;
        t[rt << 1 | 1].fir += t[rt].sec;
        t[rt << 1 | 1].sec += t[rt].sec;
        t[rt].sec = 0;
    }
}

void update(int rt, int l, int r, int L, int R, int val) {
    if(L <= l && r <= R) {
        t[rt].fir += val;
        t[rt].sec += val;
        return ;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(rt << 1, l, mid, L, R, val);
    if(R > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
    t[rt].fir = max(t[rt << 1].fir, t[rt << 1 | 1].fir);
}

void solve() {

    int n, m, p, up = 0;
    scanf("%d %d %d", &n, &m, &p);

    rep(i, 1, n) scanf("%d %d", &a[i].fir, &a[i].sec);
    rep(i, 1, m) {
        scanf("%d %d", &b[i].fir, &b[i].sec);
        up = max(up, b[i].fir);
    }
    rep(i, 1, p) scanf("%d %d %d", &c[i].x, &c[i].y, &c[i].z);

    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + m);
    sort(c + 1, c + 1 + p, cmp);

    rep(i, 1, up) tmp[i] = -INF;

    rep(i, 1, m) tmp[b[i].fir] = max(tmp[b[i].fir], -b[i].sec);
    dep(i, 1, up - 1) tmp[i] = max(tmp[i], tmp[i + 1]);

    build(1, 1, up);

    int now = 1;
    int ans = -INF;
    rep(i, 1, n) {
        while(now <= p && c[now].x < a[i].fir) {
            if(c[now].y <= up) update(1, 1, up, c[now].y, up, c[now].z);
            now++;
        }
        ans = max(ans, t[1].fir - a[i].sec);
    }
    printf("%d\n", ans);
}

int main() {

//    int _; scanf("%d", &_);
//    while(_--) solve();

    solve();

    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
using namespace std;

const int N = 1e6 + 5;

pair < int, int > a[N], b[N], t[N << 2];

int tmp[N];

struct note {
    int x, y, z;
}c[N];

bool cmp(note a, note b) {
    return a.x < b.x;
}

void build(int rt, int l, int r) {
    if(l == r) {
        t[rt].fir = tmp[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    t[rt].fir = max(t[rt << 1].fir, t[rt << 1 | 1].fir);
}

void pushdown(int rt) {
    if(t[rt].sec) {
        t[rt << 1].fir += t[rt].sec;
        t[rt << 1].sec += t[rt].sec;
        t[rt << 1 | 1].fir += t[rt].sec;
        t[rt << 1 | 1].sec += t[rt].sec;
        t[rt].sec = 0;
    }
}

void update(int rt, int l, int r, int L, int R, int val) {
    if(L <= l && r <= R) {
        t[rt].fir += val;
        t[rt].sec += val;
        return ;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(rt << 1, l, mid, L, R, val);
    if(R > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
    t[rt].fir = max(t[rt << 1].fir, t[rt << 1 | 1].fir);
}

void solve() {

    int n, m, p, up = 0;
    scanf("%d %d %d", &n, &m, &p);

    rep(i, 1, n) scanf("%d %d", &a[i].fir, &a[i].sec);
    rep(i, 1, m) {
        scanf("%d %d", &b[i].fir, &b[i].sec);
        up = max(up, b[i].fir);
    }
    rep(i, 1, p) scanf("%d %d %d", &c[i].x, &c[i].y, &c[i].z);

    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + m);
    sort(c + 1, c + 1 + p, cmp);

    rep(i, 1, up) tmp[i] = -INF;

    rep(i, 1, m) tmp[b[i].fir] = max(tmp[b[i].fir], -b[i].sec);
    dep(i, 1, up - 1) tmp[i] = max(tmp[i], tmp[i + 1]);

    build(1, 1, up);

    int now = 1;
    int ans = -INF;
    rep(i, 1, n) {
        while(now <= p && c[now].x < a[i].fir) {
            if(c[now].y < up) update(1, 1, up, c[now].y + 1, up, c[now].z);
            now++;
        }
        ans = max(ans, t[1].fir - a[i].sec);
    }
    printf("%d\n", ans);
}

int main() {

//    int _; scanf("%d", &_);
//    while(_--) solve();

    solve();

    return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 1e6 + 5;

pair < LL, LL > a[N], b[N], t[N << 2];

LL tmp[N];

struct note {
    LL x, y, z;
}c[N];

bool cmp(note a, note b) {
    return a.x < b.x;
}

void pushdown(int rt) {
    if(t[rt].second) {
        t[rt << 1].first += t[rt].second;
        t[rt << 1].second += t[rt].second;
        t[rt << 1 | 1].first += t[rt].second;
        t[rt << 1 | 1].second += t[rt].second;
        t[rt].second = 0;
    }
}

void build(int rt, int l, int r) {
    if(l == r) {
        t[rt].first = tmp[l];
        t[rt].second = 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    t[rt].first = max(t[rt << 1].first, t[rt << 1 | 1].first);
}

void update(int rt, int l, int r, int L, int R, LL val) {
    if(L <= l && r <= R) {
        t[rt].first += val;
        t[rt].second += val;
        return ;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(rt << 1, l, mid, L, R, val);
    if(R > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
    t[rt].first = max(t[rt << 1].first, t[rt << 1 | 1].first);
}

void solve() {

    int cnt = 0;
    int n, m, p; scanf("%d %d %d", &n, &m, &p);
    rep(i, 1, n) scanf("%lld %lld", &a[i].first, &a[i].second);
    rep(i, 1, m) scanf("%lld %lld", &b[i].first, &b[i].second), tmp[++cnt] = b[i].first;
    rep(i, 1, p) scanf("%lld %lld %lld", &c[i].x, &c[i].y, &c[i].z);

    sort(tmp + 1, tmp + 1 + cnt);
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + m);
    sort(c + 1, c + 1 + p, cmp);

    /// 离散化
    cnt = unique(tmp + 1, tmp + 1 + cnt) - (tmp + 1);
    rep(i, 1, m) b[i].first = lower_bound(tmp + 1, tmp + cnt + 1, b[i].first) - tmp;
    rep(i, 1, p) c[i].y = lower_bound(tmp + 1, tmp + 1 + cnt, c[i].y) - tmp;

    /// 初始化
    rep(i, 1, cnt) tmp[i] = -INF;
    rep(i, 1, m) tmp[b[i].first] = max(tmp[b[i].first], -b[i].second);
    dep(i, 1, cnt - 1) tmp[i] = max(tmp[i], tmp[i + 1]);

    build(1, 1, cnt); /// 建线段树
    LL ans = -inf;
    int now = 1;
    rep(i, 1, n) {
        while(now <= p && c[now].x < a[i].first) update(1, 1, cnt, c[now].y, cnt, c[now].z), now++;
        ans = max(ans, t[1].first - a[i].second);
    }
    printf("%lld\n", ans);
}

int main() {

//    int _; scanf("%d", &_);
//
//    while(_--) solve();

    solve();

    return 0;

}


#include <bits/stdc++.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 1e6 + 5;

pair < int, int > a[N], b[N], t[N << 2];

int tmp[N];

struct note {
    int x, y, z;
}c[N];

bool cmp(note a, note b) {
    return a.x < b.x;
}

void pushdown(int rt) {
    if(t[rt].second) {
        t[rt << 1].first += t[rt].second;
        t[rt << 1].second += t[rt].second;
        t[rt << 1 | 1].first += t[rt].second;
        t[rt << 1 | 1].second += t[rt].second;
        t[rt].second = 0;
    }
}

void build(int rt, int l, int r) {
    if(l == r) {
        t[rt].first = tmp[l];
        t[rt].second = 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    t[rt].first = max(t[rt << 1].first, t[rt << 1 | 1].first);
}

void update(int rt, int l, int r, int L, int R, int val) {
    if(L <= l && r <= R) {
        t[rt].first += val;
        t[rt].second += val;
        return ;
    }
    pushdown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid) update(rt << 1, l, mid, L, R, val);
    if(R > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
    t[rt].first = max(t[rt << 1].first, t[rt << 1 | 1].first);
}

void solve() {

    int cnt = 0;
    int n, m, p; scanf("%d %d %d", &n, &m, &p);
    rep(i, 1, n) scanf("%d %d", &a[i].first, &a[i].second);
    rep(i, 1, m) scanf("%d %d", &b[i].first, &b[i].second), tmp[++cnt] = b[i].first;
    rep(i, 1, p) scanf("%d %d %d", &c[i].x, &c[i].y, &c[i].z);

    sort(tmp + 1, tmp + 1 + cnt);
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + m);
    sort(c + 1, c + 1 + p, cmp);

    /// 离散化
    cnt = unique(tmp + 1, tmp + 1 + cnt) - (tmp + 1);
    rep(i, 1, m) b[i].first = lower_bound(tmp + 1, tmp + cnt + 1, b[i].first) - tmp;
    rep(i, 1, p) c[i].y = lower_bound(tmp + 1, tmp + 1 + cnt, c[i].y) - tmp;

    /// 初始化
    rep(i, 1, cnt) tmp[i] = -INF;
    rep(i, 1, m) tmp[b[i].first] = max(tmp[b[i].first], -b[i].second);
    dep(i, 1, cnt - 1) tmp[i] = max(tmp[i], tmp[i + 1]);

    build(1, 1, cnt); /// 建线段树
    int ans = -INF;
    int now = 1;
    rep(i, 1, n) {
        while(now <= p && c[now].x < a[i].first) update(1, 1, cnt, c[now].y, cnt, c[now].z), now++;
        ans = max(ans, t[1].first - a[i].second);
    }
    printf("%d\n", ans);
}

int main() {

//    int _; scanf("%d", &_);
//
//    while(_--) solve();

    solve();

    return 0;

}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
//#define enable_ll
#ifdef enable_ll
    #define int long long
#endif
#ifdef DEBUG
    #define out(u) cout<<u<<" "
    #define outln(u) cout<<u<<endl
    #define see(u) cout<<#u<<"="<<u<<" "
    #define seeln(u) cout<<#u<<"="<<u<<endl
    #define line cout<<"---------------------\n"
#else
    #define out(u)
    #define outln(u)
    #define see(u)
    #define seeln(u)
    #define line
#endif
#define m_p make_pair
#define pii pair<int,int>
#define piii pair<pii,int>
#define sz(x) (int)x.size()
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
template <class T> void read(T &u)
{
    u=0; char c=getchar(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=getchar();
    while (c>='0'&&c<='9') u=(u<<3)+(u<<1)+(c^48),c=getchar();
    if (flag) u=-u;
}
template <class T> void write(T u)
{
    if (u<0) printf("-"),u=-u;
    if (u<10) putchar(48+u);
    else write(u/10),putchar(48+u%10);
}
void write(char u){putchar(u);}
template <class T,class ... Arg> void read(T &u,Arg&... v) {read(u); read(v...);}
template <class T,class ... Arg> void write(T u,Arg... v) {write(u); write(v...);}
template <class T> bool checkmin(T &a,T b){return (b<a ? a=b,1 : 0);}
template <class T> bool checkmax(T &a,T b){return (b>a ? a=b,1 : 0);}
template <class T> T _min(T a,T b){return (a<b ? a : b);}
template <class T> T _max(T a,T b){return (a>b ? a : b);}
const int N=200005,inf=2e9;
int n,m,p;
int tota=0,totb=0;
int a[N],ca[N];
int b[N],cb[N];
int mx[N<<2],add[N<<2];
void push_up(int x)
{
	mx[x]=_max(mx[x+x],mx[x+x+1]);
}

void push_down(int x,int l,int mid,int r)
{
	if (add[x])
	{
		mx[x+x]+=add[x];
		mx[x+x+1]+=add[x];
		add[x]=0;
	}
}

void build(int x,int l,int r)
{
	if (l==r)
	{
		mx[x]=-cb[l];
		return;
	}
	int mid=(l+r)>>1;
	build(x+x,l,mid);
	build(x+x+1,mid+1,r);
	push_up(x);
}

void update(int x,int l,int r,int L,int R,int num)
{
	if (L<=l&&r<=R)
	{
		add[x]+=num;
		mx[x]+=num;
		return;
	}
	int mid=(l+r)>>1;
	push_down(x,l,mid,r);
	if (L<=mid) update(x+x,l,mid,L,R,num);
	if (R>mid) update(x+x+1,mid+1,r,L,R,num);
	push_up(x);
}

int query(int x,int l,int r,int L,int R)
{
	if (L<=l&&r<=R)
	{
		return mx[x];
	}
	int mid=(l+r)>>1,ans=-inf;
	if (L<=mid) checkmax(ans,query(x+x,l,mid,L,R));
	if (R>mid) checkmax(ans,query(x+x+1,mid+1,r,L,R));
	return ans;
}

void read_a()
{
	map<int,int> mp;
	map<int,int>::iterator it;
	for (int i=1,x,y;i<=n;++i)
	{
		read(x,y);
		if (mp.count(x))
		{
			checkmin(mp[x],y);
		}
		else
		{
			mp.insert(m_p(x,y));
		}
	}
	tota=0;
	for (it=mp.begin();it!=mp.end();++it)
	{
		a[++tota]=it->first,ca[tota]=it->second;
		//cout<<a[tota]<<" "<<ca[tota]<<endl;
	}//cout<<"------------------\n";
}

void read_b()
{
	map<int,int> mp;
	map<int,int>::iterator it;
	for (int i=1,x,y;i<=m;++i)
	{
		read(x,y);
		if (mp.count(x))
		{
			checkmin(mp[x],y);
		}
		else
		{
			mp.insert(m_p(x,y));
		}
	}
	totb=0;
	for (it=mp.begin();it!=mp.end();++it)
	{
		b[++totb]=it->first,cb[totb]=it->second;
		//cout<<b[totb]<<" "<<cb[totb]<<endl;
	}//cout<<"------------------\n";
}

vector <pii> v[N];
void init()
{
	read(n,m,p);
	read_a();
	read_b();
	for (int i=1,x,y,z;i<=p;++i)
	{
		read(x,y,z);
		if (x>=a[tota]||y>=b[totb]) continue;
		int res=upper_bound(a+1,a+tota+1,x)-a;
		int tmp=upper_bound(b+1,b+totb+1,y)-b;
		//cout<<res<<" "<<tmp<<" "<<z<<endl;
		v[res].push_back(m_p(tmp,z));
	}//cout<<"----------------\n";
}

void solve()
{
	int ans=-inf;
	build(1,1,totb);
	for (int i=1;i<=tota;++i)
	{
		for (int j=0;j<sz(v[i]);++j)
		{
			//cout<<i<<" "<<v[i][j].first<<" "<<totb<<" "<<v[i][j].second<<endl;
			update(1,1,totb,v[i][j].first,totb,v[i][j].second);
		}
		checkmax(ans,query(1,1,totb,1,totb)-ca[i]);
	}
	write(ans,'\n');
}

#undef int
int main()
{
	//freopen("data.in","r",stdin);
    int t=1;
    //read(t);
    while (t--)
    {
        init();
        solve();
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <ctime>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
//#define enable_ll
#ifdef enable_ll
    #define int long long
#endif
#ifdef DEBUG
    #define out(u) cout<<u<<" "
    #define outln(u) cout<<u<<endl
    #define see(u) cout<<#u<<"="<<u<<" "
    #define seeln(u) cout<<#u<<"="<<u<<endl
    #define line cout<<"---------------------\n"
#else
    #define out(u)
    #define outln(u)
    #define see(u)
    #define seeln(u)
    #define line
#endif
#define m_p make_pair
#define pii pair<int,int>
#define piii pair<pii,int>
#define sz(x) (int)x.size()
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
template <class T> void read(T &u)
{
    u=0; char c=getchar(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=getchar();
    while (c>='0'&&c<='9') u=(u<<3)+(u<<1)+(c^48),c=getchar();
    if (flag) u=-u;
}
template <class T> void write(T u)
{
    if (u<0) printf("-"),u=-u;
    if (u<10) putchar(48+u);
    else write(u/10),putchar(48+u%10);
}
void write(char u){putchar(u);}
template <class T,class ... Arg> void read(T &u,Arg&... v) {read(u); read(v...);}
template <class T,class ... Arg> void write(T u,Arg... v) {write(u); write(v...);}
template <class T> bool checkmin(T &a,T b){return (b<a ? a=b,1 : 0);}
template <class T> bool checkmax(T &a,T b){return (b>a ? a=b,1 : 0);}
template <class T> T _min(T a,T b){return (a<b ? a : b);}
template <class T> T _max(T a,T b){return (a>b ? a : b);}
const int N=200005,inf=2e9;
int n,m,p;
int tota=0,totb=0;
int a[N],ca[N];
int b[N],cb[N];
int mx[N<<2],add[N<<2];
void push_up(int x)
{
	mx[x]=_max(mx[x+x],mx[x+x+1]);
}

void push_down(int x,int l,int mid,int r)
{
	if (add[x])
	{
		mx[x+x]+=add[x];
		mx[x+x+1]+=add[x];
		add[x]=0;
	}
}

void build(int x,int l,int r)
{
	if (l==r)
	{
		mx[x]=-cb[l];
		return;
	}
	int mid=(l+r)>>1;
	build(x+x,l,mid);
	build(x+x+1,mid+1,r);
	push_up(x);
}

void update(int x,int l,int r,int L,int R,int num)
{
	if (L<=l&&r<=R)
	{
		add[x]+=num;
		mx[x]+=num;
		return;
	}
	int mid=(l+r)>>1;
	push_down(x,l,mid,r);
	if (L<=mid) update(x+x,l,mid,L,R,num);
	if (R>mid) update(x+x+1,mid+1,r,L,R,num);
	push_up(x);
}

int query(int x,int l,int r,int L,int R)
{
	if (L<=l&&r<=R)
	{
		return mx[x];
	}
	int mid=(l+r)>>1,ans=-inf;
	if (L<=mid) checkmax(ans,query(x+x,l,mid,L,R));
	if (R>mid) checkmax(ans,query(x+x+1,mid+1,r,L,R));
	return ans;
}

void read_a()
{
	map<int,int> mp;
	map<int,int>::iterator it;
	for (int i=1,x,y;i<=n;++i)
	{
		read(x,y);
		if (mp.count(x))
		{
			checkmin(mp[x],y);
		}
		else
		{
			mp.insert(m_p(x,y));
		}
	}
	tota=0;
	for (it=mp.begin();it!=mp.end();++it)
	{
		a[++tota]=it->first,ca[tota]=it->second;
		//cout<<a[tota]<<" "<<ca[tota]<<endl;
	}//cout<<"------------------\n";
}

void read_b()
{
	map<int,int> mp;
	map<int,int>::iterator it;
	for (int i=1,x,y;i<=m;++i)
	{
		read(x,y);
		if (mp.count(x))
		{
			checkmin(mp[x],y);
		}
		else
		{
			mp.insert(m_p(x,y));
		}
	}
	totb=0;
	for (it=mp.begin();it!=mp.end();++it)
	{
		b[++totb]=it->first,cb[totb]=it->second;
		//cout<<b[totb]<<" "<<cb[totb]<<endl;
	}//cout<<"------------------\n";
}

vector <pii> v[N];
void init()
{
	read(n,m,p);
	read_a();
	read_b();
	for (int i=1,x,y,z;i<=p;++i)
	{
		read(x,y,z);
		if (x>=a[tota]||y>=b[totb]) continue;
		int res=upper_bound(a+1,a+tota+1,x)-a;
		int tmp=upper_bound(b+1,b+totb+1,y)-b;
		//cout<<res<<" "<<tmp<<" "<<z<<endl;
		v[res].push_back(m_p(tmp,z));
	}//cout<<"----------------\n";
}

void solve()
{
	int ans=-inf;
	build(1,1,totb);
	for (int i=1;i<=tota;++i)
	{
		for (int j=0;j<sz(v[i]);++j)
		{
			//cout<<i<<" "<<v[i][j].first<<" "<<totb<<" "<<v[i][j].second<<endl;
			update(1,1,totb,v[i][j].first,totb,v[i][j].second);
		}
		checkmax(ans,query(1,1,totb,1,totb)-ca[i]);
	}
	write(ans,'\n');
}

#undef int
int main()
{
	freopen("data.in","r",stdin);
    int t=1;
    //read(t);
    while (t--)
    {
        init();
        solve();
    }
    return 0;
}


ts = int(input())

while ts > 0:
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    a.sort()
    b.sort()

    temp1 = " ".join(map(str,a))
    temp2 = " ".join(map(str,b))

    print(temp1)
    print(temp2)

    ts-=1


ts = int(input())

while ts > 0:
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    a.sort()
    b.sort()

    temp1 = " ".join(map(str,a))
    temp2 = " ".join(map(str,b))

    print(temp1)
    print(temp2)


t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a = sorted(a)
    b = sorted(b)
    print(' '.join(map(str, a)))
    print((' '.join(map(str, b))))


t = int(input())
for i in range(t):
    n = int(input())
    a = input().split()
    b = input().split()
    a = sorted(a)
    b = sorted(b)
    print(' '.join(map(str,a)))
    print(' '.join(map(str,b)))


t=int(input())
while t:
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    li=[0]*n
    l1.sort()
    l2.sort()
    for i in range(0,n):
        li[i]=l1[i]+l2[i]
    for i in l1:
        print(i,end=' ')
    for i in l2:
        print(i,end=' ')
    t=t-1


t=int(input())
while t:
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    li=[0]*n
    q=n
    while q:
        f=0
        for i in range(0,n):
            li[i]=l1[i]+l2[i]
        f=len(set(li))==len(li)
        if f==1:
            break
        else:
            l2=l2[-1:]+l2[:-1]
            q=q-1
    for i in l1:
        print(i,end=' ')
    for i in l2:
        print(i,end=' ')
    t=t-1


n=int(input())
from math import *
for x in range(n):
    t=int(input())
    l=sorted(list(map(int,input().split())))
    m=sorted(list(map(int,input().split())))
    l=[str(xa) for xa in l]
    m=[str(ya) for ya in m]
    print(' '.join(l))
    print(' '.join(m))


n=int(input())
from math import *
for x in range(n):
    t=int(input())
    l=sorted(list(map(str,input().split())))
    m=sorted(list(map(str,input().split())))
    print(' '.join(l))
    print(' '.join(m))


from itertools import permutations

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    x = permutations(a)
    y = permutations(b)

    sum = []
    var = True

    for j in x:
        if var:
            for i in y:
                t = [r + s for r, s in zip(i, j)]
                if len(set(t)) == len(t):
                    print(*j)
                    print(*i)
                    var = False
                    break
        else:
            break


from itertools import permutations

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    x = list(permutations(a))
    y = list(permutations(b))

    sum = []
    true = True
    i = 0
    j = 0

    while(true):
        while(true):
            t = [r + s for r, s in zip(x[i], y[j])]
            if len(set(t)) == len(t):
                print(*x[i])
                print(*y[j])
                true = False
            j+=1
        i+=1


t = int(input())

def somaUnica(sum):
    if len(set(sum)) == len(sum):
        return True
    else:
        return False

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    sum = [r + s for r, s in zip(a, b)]
    if somaUnica(sum):
        print(*a)
        print(*b)
        continue

    a.sort()
    sum = [r + s for r, s in zip(a, b)]
    if somaUnica(sum):
        print(*a)
        print(*b)
        continue

    b.sort()
    sum = [r + s for r, s in zip(a, b)]
    if (somaUnica(sum)):
        print(*a)
        print(*b)
        continue

    a.sort(reverse=True)
    sum = [r + s for r, s in zip(a, b)]
    if (somaUnica(sum)):
        print(*a)
        print(*b)
        continue


from itertools import permutations

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    x = permutations(a)
    y = permutations(b)

    sum = []
    var = True

    for j in x:
        if(var):
            for i in y:
                t = [x + y for x, y in zip(j, i)]
                if len(set(t)) == len(t):
                    print(*a)
                    print(*b)
                    var = False
                    break
        else:
            break


from itertools import permutations

def f(x,y):
    for i in x:
        for j in y:
            som = [r + s for r, s in zip(i, j)]
            if len(set(som)) == len(som):
                return i,j

t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    x = list(permutations(a))
    y = list(permutations(b))

    sum = []

    z = f(x,y)
    print(*z[0])
    print(*z[1])


t = int(input())
while t>0:
    t = t-1
    n = int(input())
    x = list(map(int,input().split()))
    y = list(map(int,input().split()))
    x.sort()
    y.sort()
    for i in range(n-1):
        print(x[i],end=" ")
    print(x[n-1])
    for i in range(n-1):
        print(y[i],end=" ")
    print(y[n-1])


t = int(input())
while t>0:
    t = t-1
    n = int(input())
    x = list(map(int,input().split()))
    y = list(map(int,input().split()))
    x.sort()
    y.sort()
    s1 = ""
    s2 = ""
    for i in range(n):
        s1 += str(x[i]) + " "
        s2 += str(y[i]) + " "
    print(s1[:len(s1)-1])
    print(s2[:len(s1)-1])


t = int(input())
ans1,ans2=[],[]
for i in range(t):
    n=int(input())
    aas,bbs=[],[]
    aas=[int(a) for a in input().split()]
    bbs=[int(a) for a in input().split()]
    aas.sort
    bbs.sort
    x,y=sorted(aas),sorted(bbs)
    ans1.append(x)
    ans2.append(y)
    s1,s2=str(),str()
for i in range(t):
    for j in range(n):
        print(ans1[i][j], end=' ')
    print('', end='\n')
    for j in range(n):
        print(ans2[i][j], end=' ')
    print('\n')


t = int(input())
ans1,ans2,ns=[],[],[]
for i in range(t):
    n=int(input())
    ns.append(n)
    x,y,aas,bbs=[],[],[],[]
    aas=[int(a) for a in input().split()]
    bbs=[int(a) for a in input().split()]
    aas.sort
    bbs.sort
    x,y=sorted(aas),sorted(bbs)
    ans1.append(x)
    ans2.append(y)
    s1,s2=str(),str()
for i in range(t):
    for j in range(ns[i]):
        print(ans1[i][j], end=' ')
    print('', end='\n')
    for j in range(ns[i]):
        print(ans2[i][j], end=' ')
    print('\n')


t = int(input())
ans1,ans2,ns=[],[],[]
for i in range(t):
    n=int(input())
    ns.append(n)
    x,y,aas,bbs=[],[],[],[]
    aas=[int(a) for a in input().split()]
    bbs=[int(a) for a in input().split()]
    aas.sort
    bbs.sort
    x,y=sorted(aas),sorted(bbs)
    ans1.append(x)
    ans2.append(y)
    s1,s2=str(),str()
for i in range(t):
    for j in range(ns[i]):
        print(ans1[i][j], end=' ')
    print('', end='\n')
    for j in range(n):
        print(ans2[i][j], end=' ')
    print('\n')


t=int(input())
q=[]
for i in range(0,t):
    n=int(input())
    a = list(map(int,input().strip().split()))[:n]
    b = list(map(int,input().strip().split()))[:n]
    z=[]
    for j in range(0,n):
        k=a[j]+b[j]
        if(k in z):
            for f in range(0,len(z)):
                if(k==z[f]):
                    e=f
            t=a[f]
            a[f]=a[j]
            a[j]=t
            t=b[f]
            b[f]=b[j]
            b[j]=t
            z[f]=a[f]+b[f]
            k=a[j]+b[j]
        z.append(k)
    q.append(a)
    q.append(b)
for i in q:
    for j in i:
        print(j,end=" ")
    print()


t=int(input())
q=[]
for i in range(0,t):
    n=int(input())
    a = list(map(int,input().strip().split()))[:n]
    b = list(map(int,input().strip().split()))[:n]
    z=[]
    a.sort()
    b.sort()
    for j in range(0,n):
        k=a[j]+b[j]
        if(k in z):
            for f in range(0,len(z)):
                if(k==z[f]):
                    e=f
            t=a[f]
            a[f]=a[j]
            a[j]=t
            z[f]=a[f]+b[f]
            k=a[j]+b[j]

        z.append(k)
    q.append(a)
    q.append(b)
for i in q:
    for j in i:
        print(j,end=" ")
    print()


t=int(input())
q=[]
for i in range(0,t):
    n=int(input())
    a = list(map(int,input().strip().split()))[:n]
    b = list(map(int,input().strip().split()))[:n]
    z=[]
    for j in range(0,n):
        k=a[j]+b[j]
        if(k in z):
            for f in range(0,len(z)):
                if(k==z[f]):
                    e=f
            t=a[f]
            a[f]=a[j]
            a[j]=t
            z[f]=a[f]+b[f]
            k=a[j]+b[j]
        z.append(k)
    q.append(a)
    q.append(b)
for i in q:
    for j in i:
        print(j,end=" ")
    print()


t=int(input())
q=[]
for i in range(0,t):
    n=int(input())
    a = list(map(int,input().strip().split()))[:n]
    b = list(map(int,input().strip().split()))[:n]
    z=[]
    for j in range(0,n):
        k=a[j]+b[j]
        if(k in z):
          while(k in z):
            for f in range(0,len(z)):
                if(k==z[f]):
                    e=f
            t=a[f]
            a[f]=a[j]
            a[j]=t
            z[f]=a[f]+b[f]
            k=a[j]+b[j]

        z.append(k)
    q.append(a)
    q.append(b)
for i in q:
    for j in i:
        print(j,end=" ")
    print()


for i in range(int(input())):
    input()
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    x.sort()
    y.sort()
    print(*x, sep=" ")
    print(*y, sep=" ")


for i in range(int(input())):
    input()
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    a = []
    b = []
    p = x + y
    p.sort()
    for j in range(len(p)):
        if j % 2 == 0:
            a.append(p[j])
        else:
            b.append(p[j])
    print(*a, sep=" ")
    print(*b, sep=" ")


for t in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    print(sorted(l1))
    print(sorted(l2))


for t in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    l1.sort()
    l2.sort()
    print(*l1)
    print(*l2)


for t in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    l1.sort()
    l2.sort()
    print(*l1)
    print(*l1)


neck = []
brace = []
t = int(input())
for z in range(t):
    neck = []
    brace = []
    n = int(input())
    a = input().split()
    b = input().split()
    neck = [int(i) for i in a]
    brace = [int(i) for i in b]
    lneck = len(neck)
    lbrace = len(brace)
    for i in neck:
        if i < 0:
            lneck=-1
    for i in brace:
        if i < 0:
            lbrace=-1
    if lneck !=n or lbrace!=n:
        break
    neck.sort()
    brace.sort()
    add = [neck[i]+brace[i] for i in range(len(neck))]
    if len(add) == len(set(add)):
        print(neck)
        print(brace)


neck = []
brace = []
t = int(input())
for z in range(t):
    neck = []
    brace = []
    n = int(input())
    a = input().split()
    b = input().split()
    neck = [int(i) for i in a]
    brace = [int(i) for i in b]
    lneck = len(neck)
    lbrace = len(brace)
    for i in neck:
        if i < 0:
            lneck=-1
    for i in brace:
        if i < 0:
            lbrace=-1
    if lneck !=n or lbrace!=n:
        break
    neck.sort()
    brace.sort()
    add = [neck[i]+brace[i] for i in range(len(neck))]
    if len(add) == len(set(add)):
        print(*neck)
        print(*brace)


arr = []
t = int(input())
for i in range(t):
    arr.append([0, 0])

def quicksort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0]
    less = [i for i in array[1:] if i <= pivot]
    greater = [i for i in array[1:] if i > pivot]
    return quicksort(less) + [pivot] + quicksort(greater)

for j in range(t):
    n = input()
    arr[j][0] = []
    arr[j][1] = []
    a = input()
    for i in a.split():
        arr[j][0].append(int(i))
    b = input()
    for i in b.split():
        arr[j][1].append(int(i))
    arr[j][0] = quicksort(arr[j][0])
    arr[j][1] = quicksort(arr[j][1])

for i in arr:
    for j in i:
        for k in j:
            print(k, end=" ")
        print()


t = int(input())

def quicksort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0]
    less = [i for i in array[1:] if i <= pivot]
    greater = [i for i in array[1:] if i > pivot]
    return quicksort(less) + [pivot] + quicksort(greater)

for i in range(t):
    arr1 = []
    arr2 = []
    n = input()
    a = input()
    for i in a.split():
        arr1.append(int(i))
    b = input()
    for i in b.split():
        arr2.append(int(i))
    arr1 = quicksort(arr1)
    arr2 = quicksort(arr2)
    print()
    print(arr1)
    print(arr2)


T = int(input())
for _ in range(T):
    n = int(input())
    A = sorted(list(map(int, input().split())))
    B = sorted(list(map(int, input().split())))

    print(*A)
    print(*B)


arr = []
t = int(input())
for i in range(t):
    arr.append([0, 0])

for j in range(t):
    n = input()
    arr[j][0] = []
    arr[j][1] = []
    a = input()
    for i in a.split():
        arr[j][0].append(int(i))
    b = input()
    for i in b.split():
        arr[j][1].append(int(i))
    arr[j][0] = sorted(arr[j][0])
    arr[j][1] = sorted(arr[j][1])
    print(*arr[j][0])
    print(*arr[j][1])


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    c=[]
    for i in range(n):
        c.append(a[i]+b[i])
    d=len(list(dict.fromkeys(c)))
    if(d<n):
        a.sort()
        b.sort()
    print(a)
    print(b)


t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    c=[]
    for i in range(n):
        c.append(a[i]+b[i])
    d=len(list(dict.fromkeys(c)))
    if(d<n):
        a.sort()
        b.sort()
    for i in range(n-1):
        print(a[i],end=' ')
    print(a[n-1])
    for i in range(n):
        print(b[i],end=' ')


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    #b=b[::-1]
    for i in a:
        print(i,end=' ')
    print()
    for i in b:
        print(i,end=' ')
    print()


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    #b=b[::-1]
    for i in range(n):
        print(a[i]+b[i],end=' ')
    print()


from itertools import permutations

t = int(input())

for i in range(t):

    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))

    perm1 = list(permutations(ai,n))
    perm2 = list(permutations(bi, n))
    flag=False

    for k in range(n):
        if flag==True:
            break
        for i in range(n):
                a = list(perm1[k])
                b = list(perm2[i])
                c = list( map(sum, zip(a, b)))
                soma = []
                memo = []

                flag=True
                if len(c) == len(set(c)):
                    a = list(map(str,a))
                    b = list(map(str,b))

                    print(' '.join(list(a)))
                    print(' '.join(list(b)))
                    break


t = int(input())

for i in range(t):
    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))
    ai.sort()
    bi.sort(reverse=True)
    aiS = sum(zip(reversed(ai), ai), ())[:len(ai)]
    print(list(aiS))
    print(bi)


from itertools import permutations

t = int(input())

for i in range(t):

    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))

    perm1 = list(permutations(ai,n))
    perm2 = list(permutations(bi, n))
    flag=False
    for k in range(n):
        if flag==True:
            break
        for i in range(len(list(perm1))):
                a = list(perm1[k])
                b = list(perm2[i])
                c = list( map(sum, zip(a, b)))
                soma = []
                memo = []

                flag=True
                for j in range(len(c)):
                    if c[j] not in memo:
                        memo.append(c[j])
                    else:
                        flag=False

                        break
                if flag==True:
                    a = list(map(str,a))
                    b = list(map(str,b))

                    print(' '.join(list(a)))
                    print(' '.join(list(b)))
                    break


from itertools import permutations

t = int(input())

for i in range(t):

    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))

    c = list(map(sum, zip(ai, bi)))

    if len(c) == len(set(c)):
        ai = list(map(str,ai))
        bi = list(map(str,bi))

        print(' '.join(ai))
        print(' '.join(bi))
    else:
        ai.sort()
        c = list(map(sum, zip(ai, bi)))
        if len(c) == len(set(c)):
            ai = list(map(str,ai))
            bi = list(map(str,bi))

            print(' '.join(ai))
            print(' '.join(bi))
        else:
            bi.sort()
            c = list(map(sum, zip(ai, bi)))
            if len(c) == len(set(c)):
                ai = list(map(str,ai))
                bi = list(map(str,bi))

                print(' '.join(ai))
                print(' '.join(bi))
            else:
                ai.sort(reversed=True)
                ai = list(map(str,ai))
                bi = list(map(str,bi))

                print(' '.join(ai))
                print(' '.join(bi))


t = int(input())

for i in range(t):
    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))
    ai.sort()
    bi.sort(reverse=True)
    aiS = list(sum(zip(reversed(ai), ai), ())[:len(ai)])
    out1 = ''
    out2 = ''
    for i in range(n):
        out1+=str(aiS[i])+' '
        out2+=str(bi[i])+' '

    print(out1[:-1])
    print(out2[:-1])


from itertools import permutations

t = int(input())

for i in range(t):

    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))

    c = list(map(sum, zip(ai, bi)))

    if len(c) == len(set(c)):
        print(ai)
        print(bi)
    else:
        ai.sort()
        c = list(map(sum, zip(ai, bi)))
        if len(c) == len(set(c)):
            print(ai)
            print(bi)
        else:
            bi.sort()
            c = list(map(sum, zip(ai, bi)))
            if len(c) == len(set(c)):
                print(ai)
                print(bi)
            else:
                ai.sort(reversed=True)
                print(ai)
                print(bi)


from itertools import permutations

t = int(input())

for i in range(t):

    n = int(input())
    ai = list(map(int,input().split()))
    bi = list(map(int,input().split()))

    perm1 = list(permutations(ai,n))
    perm2 = list(permutations(bi, n))
    flag=False

    for k in range(n):
        if flag==True:
            break
        for i in range(n):
                a = list(perm1[k])
                b = list(perm2[i])
                c = list( map(sum, zip(a, b)))

                soma = []
                memo = []

                if len(c) == len(set(c)):
                    a = list(map(str,a))
                    b = list(map(str,b))

                    print(' '.join(list(a)))
                    print(' '.join(list(b)))
                    flag=True
                    break


n = int(input())
for i in range(n):
    res_1 = []
    res_2 = []
    k = int(input())
    oj = sorted(list(map(int, input().split())))
    br = sorted(list(map(int, input().split())))
    for i in range(k):
        res_1.append(oj[i])
        res_2.append(br[k-i-1])
    for i in range(k-1):
        print(res_1[i], end=' ')
    print(res_1[k-1])
    for i in range(k-1):
        print(res_2[i], end=' ')
    print(res_2[k-1])


n = int(input())
for i in range(n):
    res_1 = []
    res_2 = []
    k = int(input())
    oj = sorted(list(map(int, input().split())))
    br = sorted(list(map(int, input().split())))
    for i in range(k):
        res_1.append(oj[i])
        res_2.append(br[k-i-1])
    print(res_1)
    print(res_2)


def println(arr):
    for j in range(len(arr)):
        print(arr[j], end=" ")

def func(arr_1, arr_2):
    result_1 = []
    result_2 = []
    arr_1, arr_2 = sorted(arr_1), sorted(arr_2)
    for q in range(len(arr_1)):
        result_1.append(arr_1[q])
        result_2.append(arr_2[q])
    println(result_1)
    print()
    println(result_2)
    print()
    return

n = int(input())
for i in range(n):
    k = int(input())
    oj = list(map(int, input().split()))
    br = list(map(int, input().split()))
    func(oj, br)


kl = int(input())
for l in range(kl):

    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    a.sort()
    b.sort()
    for i in range(n):
        print(a[i], '', end='')
    print()
    for i in range(n):
        print(b[i], '', end='')
    print()


kl = int(input())
for l in range(kl):

    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    a.sort()
    b.sort()
    for i in range(n):
        print(a[i] + b[i], '', end='')
    print()


cases = int(input().strip())
for case in range(cases):
  n = int(input().strip())
  a = list(map(int, input().strip().split(" ")))
  b = list(map(int, input().strip().split(" ")))

  while True:
    found = False
    for i in range(n):
      for j in range(i + 1, n):
        if a[i] + b[i] == a[j] + b[j]:
          found = True
          b[i], b[j] = b[j], b[i]
          break
      if found: break
    if not found:
      break
  print(' '.join(map(str, a)))
  print(' '.join(map(str, b)))


cases = int(input().strip())
for case in range(cases):
  n = int(input().strip())
  a = sorted(list(map(int, input().strip().split(" "))))
  b = sorted(list(map(int, input().strip().split(" "))))
  print(' '.join(map(str, a)))
  print(' '.join(map(str, b)))


t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a.sort()
    b.sort()
    a = [str(i) for i in a]
    b = [str(i) for i in b]
    print(' '.join(a))
    print(' '.join(b))


t = int(input())
for i in range(t):
    n = int(input())
    a = list(input().split())
    b = list(input().split())
    a.sort()
    b.sort()
    print(' '.join(a))
    print(' '.join(b))


t=int(input())
for i in range(t):
    check=[]
    a1=[]
    b1=[]
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    for j in range(len(a)):
        for k in range(len(b)):
            if a[j]+b[k] not in check:
                a1.append(a[j])
                b1.append(b[k])
                check.append(a[j]+b[k])
                b.remove(b[k])
                break
    for j in range(len(a1)):
        print(a1[j],end=" ")
    print()
    for j in range(len(b1)):
        print(b1[j],end=" ")
    print()


t=int(input())
for i in range(t):
    check=[]
    a1=[]
    b1=[]
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    b.reverse()
    for i in range(n):
        if len(b)!=0:
            for j in range(n):
                if a[i]+b[j] not in check and b[j] in b:
                    a1.append(a[i])
                    b1.append(b[j])
                    check.append(a[i]+b[j])
                    b.remove(b[j])
                    break
    for i in range(len(a1)):
        print(a1[i],end=" ")
    print()
    for j in range(len(b1)):
        print(b1[j],end=" ")
    print()


t=int(input())
for i in range(t):
    check=[]
    a1=[]
    b1=[]
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    b.reverse()
    for i in range(n):
        if len(b)!=0:
            for j in range(n):
                if a[i]+b[j] not in check and b[j] in b:
                    a1.append(a[i])
                    b1.append(b[j])
                    check.append(a[i]+b[j])
                    b.remove(b[j])
                    break
        print(a1)
        print(b1)


n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a.sort()
b.sort()
a1=[]
b1=[]
for i in range(n):
    a1.append(a[i])
    b1.append(b[i])
print(*a1,sep=' ')
print(*b1,sep=' ')


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort(reverse=True)
    print(*a,sep=' ')
    print(*b,sep=' ')


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    print(*a,sep=' ')
    print(*b,sep=' ')


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort(reversed=True)
    print(*a,sep=' ')
    print(*b,sep=' ')


for i in range(int(input())):
    input()
    lis1 = list(map(int,input().split()))
    lis2 = list(map(int,input().split()))
    lis1.sort(reverse=True)
    lis2.sort()
    print(*lis1,sep=" ")
    print(*lis2,sep=" ")


for i in range(int(input())):
    input()
    lis1 = list(map(int,input().split()))
    lis2 = list(map(int,input().split()))
    lis1.sort(reverse=True)
    lis2.sort(reverse=True)
    print(*lis1,sep=" ")
    print(*lis2,sep=" ")


from sys  import stdin,stdout

import bisect

import math

def st():
    return list(stdin.readline().strip())

def inp():
    return int(stdin.readline())

def li():
    return list(map(int,stdin.readline().split()))

def mp():
    return map(int,stdin.readline().split())

def pr(n):
    stdout.write(str(n)+"\n")

def soe(limit):
    l=[1]*(limit+1)
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0

    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
        a*=a
        n=n>>1
    return r

def solve():
    n=inp()
    l=li()
    k=li()
    l.sort()
    k.sort()
    print(*l)
    print(*k)
for _ in range(inp()):
    solve()


from sys  import stdin,stdout

import bisect

import math

def st():
    return list(stdin.readline().strip())

def inp():
    return int(stdin.readline())

def li():
    return list(map(int,stdin.readline().split()))

def mp():
    return map(int,stdin.readline().split())

def pr(n):
    stdout.write(str(n)+"\n")

def soe(limit):
    l=[1]*(limit+1)
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0

    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
        a*=a
        n=n>>1
    return r

def solve():
    n=inp()
    l=li()
    k=li()
    l.sort()
    k.sort()
    for i in range(n):
        print(l[i]+k[i],end=" ")
    print()

for _ in range(inp()):
    solve()


for nt in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	print (*sorted(a))
	print (*sorted(b))


for nt in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	a.sort()
	b.sort()
	for i in range(n):
		print (a[i]+b[i],end=" ")
	print ()


for nt in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	print (sorted(a))
	print (sorted(b))


def sol():
    t=int(input())
    for tt in range(t):
        n=int(input())
        A=[int(item) for item in input().split()]
        B=[int(item) for item in input().split()]
        A.sort()
        B.sort()
        print(*A)
        print(*B)
if(__name__=='__main__'):
    sol()


def sol():
    t=int(input())
    for tt in range(t):
        n=int(input())
        A=[int(item) for item in input().split()]
        B=[int(item) for item in input().split()]
        A.sort()
        B.sort()
        C=[A[item]+B[item] for item in range(n)]
        print(*C)
if(__name__=='__main__'):
    sol()


def sol():
    t=int(input())
    for tt in range(t):
        n=int(input())
        A=[int(item) for item in input().split()]
        B=[int(item) for item in input().split()]
        A.sort()
        B.sort()
        C=[A[item],B[item] for item in range(n)]
        print(*C)
if(__name__=='__main__'):
    sol()


from itertools import permutations
for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    perm=permutations(b)
    for i in list(perm):
        x=set()
        flag=1
        for j in range(n):
            if (a[j]+i[j]) not in x:
                x.add(a[j]+i[j])
            else:
                flag=0
                break
        if flag:
            print(*a)
            print(*list(i))
            break


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    for i in range(n):
        x=set()
        flag=1
        for j in range(n):
            if (b[j]+a[j]) not in x:
                x.add(b[j]+a[j])
            else:
                if j==0:
                    b[j],b[j+1]=b[j+1],b[j]
                else:
                    b[j],b[j-1]=b[j-1],b[j]
                flag=0
                break
        if flag:
            print(*a)
            print(*b)
            break


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    print(*a)
    print(*b)


t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.sort()
    b.sort()
    print(*a)
    print(*b)


t = int(input())
for i in range(t):
    n = int(input())
    a = input().split()
    b = input().split()
    c = sorted(a, key=lambda x: ord(x))
    d = sorted(b, key=lambda x: ord(x))
    print(*c)
    print(*d)
    a = list()
    b = list()


t = int(input())
while t>0:
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a.sort()
    b.sort()
    for val in a:
        print(val,end=' ')
    print()
    for val in b:
        print(val,end=' ')
    print()
    t-=1


t = int(input())
while t>0:
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a.sort()
    b.sort()
    for val in a:
        print(val,end=' ')
    print()
    for val in b:
        print(val,end=' ')
    print()


import sys
input = sys.stdin.readline
for ti in range(int(input())):
    n = int(input())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    a.sort()
    b.sort()
    print(*a)
    print(*b)


import math
n = int(input())
r = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
d = 0
c = 0
for i in range(len(r)):
    t = 0
    if(r[i] == 0):
        t = 1
        if(b[i] == 1):
            d += 1
    elif b[i] == 0:
        c += 1

#print(d, c)
if(not c):
    print('-1')
else:
    print(math.ceil((d+1)/c))


for _ in range(int(input())):
    n = int(input())
    a = sorted(map(int, input().split()))
    b = sorted(map(int, input().split()))
    print(*a)
    print(*b)


for _ in range(int(input())):
    n = int(input())
    a = sorted(map(int, input().split()))
    b = sorted(map(int, input().split()))
    for i in range(n):
        print(a[i] + b[i], end=" ")
    print()


T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i).sort
  b = gets.split.map(&:to_i).sort

  puts a.join(' ')
  puts b.join(' ')
end


def main
  t = gets.chomp.to_i
  t.times { solve }
end

def solve
  n = gets.chomp.to_i # not used
  a = gets.chomp.split.map(&:to_i)
  b = gets.chomp.split.map(&:to_i)
  # S = gets.chomp.chars
  a.sort!
  b.sort!
  print a.join(" ") + "\n"
  print b.join(" ") + "\n"
end

main


t = gets.to_i
while t > 0
  t -= 1
  n = gets.to_i
  a = gets.split.map(&:to_i).sort!
  b = gets.split.map(&:to_i).sort!
  puts a.join(' ')
  puts b.join(' ')
end


'''

'''
from sys import stdin, stdout

def main():
	n = int(stdin.readline())
	for i in range(n):
		tc = int(stdin.readline())
		arr = [int(x) for x in stdin.readline().split()]
		arr2 = [int(x) for x in stdin.readline().split()]
		arr.sort()
		arr2.sort()
		r = ' '.join(map(str,arr))
		r2 = ' '.join(map(str,arr2))
		stdout.write(r+'\n')
		stdout.write(r2+'\n')
if __name__ == "__main__":
	main()


from collections import defaultdict as dd, Counter, OrderedDict as od, deque
from itertools import combinations as comb, permutations as perm
from functools import reduce
from math import log, ceil, floor
from fractions import gcd

import os, sys

#sys.stdin = open("pb.txt", "r")
raw_input = sys.stdin.readline

int_input = lambda : int(raw_input())
list_input = lambda : list(raw_input().strip())
explode_input = lambda sep :raw_input().split(sep)
format_input = lambda object : map(object, raw_input().split())

t = int_input()
for t_itr in xrange(t):
	n = int_input()
	a = sorted(format_input(int))
	b = sorted(format_input(int))
	d = dd(int)
	is_used = {x:False for x in b}
	sa, sb = '', ''
	for x in a:
		for y in b:
			if is_used[y] ==  False and d.get(x + y, 0) == 0:
				d[x + y] = 1
				is_used[y] = 1
				sa = sa + str(x) + ' '
				sb = sb + str(y) + ' '
				break

	print sa + '\n' +sb


T = int(raw_input())
for _ in xrange(T):
    n = int(raw_input())
    a = map(int, raw_input().split())
    b = map(int, raw_input().split())
    a.sort()
    b.sort()
    for x in a:
        print x,
    print
    for x in b:
        print x,
    print


def merge(arr, start, mid, end):
	i,j,k,temp1,temp2 = 0,0,start, [],[]
	for m in range(start, mid+1):
		temp1.append(arr[m])
	for m in range(mid+1, end+1):
		temp2.append(arr[m])
	while i<len(temp1) and j<len(temp2):
		if(temp1[i]< temp2[j]):
			arr[k] = temp1[i]
			i+=1
		else:
			arr[k] = temp2[j]
			j+=1
		k+=1
	while i<len(temp1):
		arr[k]=temp1[i]
		i+=1
		k+=1
	while j<len(temp2):
		arr[k] = temp2[j]
		j+=1
		k+=1

def mergeSort(arr, start ,end):
	if start<end:
		mid = (start+end)/2
		mergeSort(arr, start, mid)
		mergeSort(arr, mid+1, end)
		merge(arr, start, mid, end)

arr=[]
arr1 = []
test = int(raw_input())
for i in range(test):
	a=[]
	b=[]
	no = raw_input().split()
	arr = raw_input().split()
	arr1 = raw_input().split()

	for i in arr:
		a.append(int(i))

	for i in arr1:
		b.append(int(i))
	mergeSort(a, 0, len(a)-1)
	mergeSort(b, 0, len(b)-1)
	ans1 = ''
	for i in a:
		ans1+=str(i)+" "
	ans1 =ans1[:-1]
	print(ans1)
	ans2=''
	for i in b:
		ans2+=str(i)+" "
	print(ans2)


t = input()
for i in range(t):
	n = input()
	a = [int(ai) for ai in raw_input().split(" ")]
	b = [int(bi) for bi in raw_input().split(" ")]
	a.sort()
	for ai in a:
		print ai,
	print
	b.sort()
	for bi in b:
		print bi,
	print


for _ in range(input()):
    n=input()
    a=[int(i) for i in raw_input().split()]
    b=[int(i) for i in raw_input().split()]
    a.sort()
    b.sort()
    for i in a:
        print i,
    print
    for i in b:
        print i,
    print


t = int(raw_input())
for i in xrange(t):
    n = int(raw_input())
    A = map(int, raw_input().split())
    B = map(int, raw_input().split())
    A.sort()
    B.sort()
    C, D = [],[]
    for j in xrange(n):
        C.append(A[j])
        D.append(B[j])
    for u in C:
        print u,
    print ''
    for k in D:
        print k,
    print ''


for i in range(input()):
    input()
    a = sorted(map(int, raw_input().split()))
    b = sorted(map(int, raw_input().split()))
    print ' '.join(map(str, a))
    print ' '.join(map(str, b))


t=input()
while t>0:
	n=input()
	a=[int(x) for x in raw_input().split()]
	b=[int(x) for x in raw_input().split()]
	a_sort=sorted(a)
	b_sort=sorted(b)
	for i in range(n):
		print(a_sort[i]),
	print('')
	for i in range(n):
		print(b_sort[i]),
	t-=1


t=input()
while t>0:
	n=input()
	a=[int(x) for x in raw_input().split()]
	b=[int(x) for x in raw_input().split()]
	a_sort=sorted(a)
	b_sort=sorted(b)
	for i in range(n):
		print(a_sort[i]),
	print('')
	for i in range(n):
		print(b_sort[i]),


def read_test():
	n = raw_input()
	line1 = raw_input()
	line2 = raw_input()
	return {
		"n": int(n),
		"a": [int(token) for token in line1.split(" ")],
		"b": [int(token) for token in line2.split(" ")]
	}

def sort(a):
	for i in range(len(a) - 1):
		for j in range(i + 1, len(a)):
			if a[j] < a[i]:
				a[i], a[j] = a[j], a[i]

def solve(a, b, n):
	sort(a)
	sort(b)

	return [a, b]

n = int(raw_input())
for i in range(0, n):
	test = read_test()
	result = solve(test.get("a"), test.get("b"), test.get("n"))
	print(" ".join([str(item) for item in result[0]]))
	print(" ".join([str(item) for item in result[1]]))


a = input()
b = int(input())
l=[]
for _ in range(b):
    l.append(input())
c=0
for i in l:
    if a not in i:
        c+=1
    else:
        c+=0

lo=[]
if c == len(l):
    print(a)
else:
    for i in l:
        if a in i:
            g=i.replace(a,"")
            lo.append(g)
lo.sort()
# print(lo)
print(a+lo[0])


a = input()
b = int(input())
l=[]
for _ in range(b):
    l.append(input())
c=0
for i in l:
    if a not in i[:len(a)]:
        c+=1
    else:
        c+=0
# print(c)
lo=[]
if c == len(l):
    print(a)
else:
    for i in l:
        if a in i:
            g=i.replace(a,"")
            lo.append(g)
    lo.sort()
# print(lo)
    print(a+lo[0])


s=input()
n=int(input())
last=[input() for i in range(n)]
# print(last)
last=sorted(list(filter(lambda x:s==x[:len(s)],last)),key=len)
# print(last)
if len(last)>0:
	print(last[0])
else:print(s)


s=input()
n=int(input())
last=[input() for i in range(n)]
# print(last)
def do(st):
	dic={chr(i):i for i in range(97,123)}
	out=0
	i=0
	while i>len(st):
		out+=out[st[i]]
		i+=1
	return out
last=sorted(list(filter(lambda x:s==x[:len(s)],last)),key=len)
to_work=[]
i=0
while i<len(last) and len(last[i])==len(last[0]):
	to_work.append(last[i])
	i+=1
last=sorted(to_work,key=do)
# print(last)
if len(last)>0:
	print(last[0])
else:print(s)


s=input()
n=int(input())
last=[input() for i in range(n)]
# print(last)
def do(st):
	dic={chr(i):i for i in range(97,123)}
	out=0
	i=0
	while i>len(st):
		out+=out[st[i]]
		i+=1
	return out
last=sorted(list(filter(lambda x:s==x[:len(s)],last)))
# to_work=[]
# i=0
# while i<len(last) and len(last[i])==len(last[0]):
# 	to_work.append(last[i])
# 	i+=1
# last=sorted(to_work,key=do)
# # print(last)
if len(last)>0:
	print(last[0])
else:print(s)


a='f'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if len(x)<len(a):
            a=x
print([z,a][z==x[0:len(z)]])


a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if  sum(map(ord,x))<sum(map(ord,a)):
            a=x
print([z,a][z==a[0:len(z)]])


v=[]
z=input()
for i in range(int(input())):
    x=input()
    if x[0:len(z)]==z:
        v.append(x)
if len(v)==0:
     print(z)
else:
  print(min(v))


a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if len(x)<=len(a) and sum(map(ord,x))<sum(map(ord,a)):
            a=x
print([z,a][z==a[0:len(z)]])


import sys
a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if len(x)>len(z) and len(a)>len(z):
             if ord(x[len(z)])<ord(a[len(z)]):
                  a=x
        if z==x :
             print(z)
             sys.exit(0)
print(a)


a='f'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if len(x)<len(a):
            a=x
print([z,a][z==a[0:len(z)]])


a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x[0:len(z)]:
        if len(x)>len(z) and len(a)>len(z):
             if ord(x[len(z)])<ord(a[len(z)]):
                  a=x
        if z==x :
             a=x
print([z,a][z==a[0:len(z)]])


import sys
a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x :
         print(z)
         sys.exit(0)
    if z==x[0:len(z)]:
        if len(x)>len(z) and len(a)>len(z):
             if ord(x[len(z)])<ord(a[len(z)]):
                  a=x
print([a,z][a=='z'*101])


import sys
a='z'*101
z=input()
for i in range(int(input())):
    x=input()
    if z==x :
         print(z)
         sys.exit(0)
    if z==x[0:len(z)]:
        if len(x)>len(z) and len(a)>len(z):
             if ord(x[len(z)])<ord(a[len(z)]):
                  a=x
print([a,x][a=='z'*101])


s = input()
P = set()

for _ in range(int(input())):
    p = input()
    if p.startswith(s):
        P.add(p)

print(min(P) if P else s)


import math

def ans(l,s,x):
    f=l
    y=l[0:x]
    if(s==y):
        return True
    else:
        return False

s=str(input())
x=len(s)
t=int(input())
m=100000
f=0
k=''
l=[]
while(t>0):
    a=str(input())
    l.append(a)
    t-=1

l.sort()

for i in l:
    if(ans(i,s,x)==True):
        k=i
        break
if(k):
    print(k)
else:
    print(s)


s = input()
n = int(input())
a = sorted([input() for _ in range(n)])
try:
    t = next(filter(lambda x: x.startswith(s), a))
    print(t)
except StopIteration:
    print(s)


s, n = input()
l, p = len(s), '~'
for i in range(int(input())):
    t = input()
    if len(t) < l: continue
    if s == t[: l] and p > t: p = t
print(p)


s = input()
l, p = len(s), '~'
for i in range(int(input())):
    t = input()
    if len(t) < l: continue
    if s == t[: l] and p > t: p = t
print(p if p < '~' else s)


s = input()
l, p = len(s), '~'
for i in range(int(input())):
    t = input()
    if len(t) < l: continue
    if s == t[: l] and p > t: p = t
print(p)


s, n = input()
l, p = len(s), '~'
for i in range(int(input())):
    t = input()
    if len(t) < l: continue
    if s == t[: l] and p > t: p = t
print(p if p < '~' else s)


s, v = input(), None

for i in range(int(input())):

    c = input()

    if c.startswith(s) and (not v or c < v):

        v = c

print(v if v else s)


s = input()
n = int(input())
used = []
for i in range(n):
    word = input()
    used.append(word)
used = sorted(used)

def main(s, used):
    length = len(s)
    for used_word in used:
        if used_word[0:length] == s:
            return used_word
    return s

print(main(s, used))


def main():
    str_ = input()
    n = int(input())
    sz = len(str_)
    yoo = "#"
    for cc in range (n) :
        a = input()
        flg = 0
        if(len(a)>=sz) :
            for i in range (0,sz) :
                if(str_[i] !=a[i]) :
                    flg = 1
                    break
            if(flg==0) :
                if(yoo=="#" or (yoo>=a)) : yoo = a

    if(yoo=="#") : yoo = str_
    print(yoo)

main()


def main():
    str_ = input()
    n = int(input())
    sz = len(str_)
    ans = 100000
    yoo = "#"
    for cc in range (n) :
        a = input()
        flg = 0
        for i in range (sz) :
            if(str_[i] !=a[i]) :
                flg = 1
                break
        if(flg==0) :
            if(len(a)<ans) :
                ans = len(a)
                yoo = a
            elif(len(a)==ans) :
                if(yoo > a) : yoo = a

    if(yoo=="#") : yoo = str_
    print(yoo)

main()


def main():
    str_ = input()
    n = int(input())
    sz = len(str_)
    ans = 10000000000000000
    yoo = "#"
    for cc in range (n) :
        a = input()
        flg = 0
        if(len(a)>=sz) :
            for i in range (0,sz) :
                if(str_[i] !=a[i]) :
                    flg = 1
                    break
            if(flg==0) :
                if(len(a)<ans) :
                    ans = len(a)
                    yoo = a
                elif(len(a)==ans) :
                    if(yoo > a) : yoo = a

    if(yoo=="#") : yoo = str_
    print(yoo)

main()


def isAuto(s,x):
    y=""
    if len(x)<len(s):
        return False
    for i in range(len(s)):
        y+=x[i]
    if y==s:
        return True
    else:
        return False

def findSLex(L,s):
    if len(L)==0:
        return s
    minimum=L[0]
    for i in range(len(L)):
        if L[i]<minimum:
            minimum=L[i]
    return minimum

s=input()
n=int(input())
i=1
L=[]

while i!=n+1:
    x=input()
    if isAuto(s,x):
        L.append(x)
    i+=1
print(findSLex(L,s))


def isAuto(s,x):
    lengthS=len(s)
    y=""
    if len(x)<len(s):
        return False
    for i in range(lengthS):
        y+=x[i]
    if y==s:
        return True
    return False

def findSLex(L,s):
    if len(L)==0:
        return s
    minimum=L[0]
    for i in range(len(L)):
        if len(L[i])<len(L[0]):
            minimum=L[i]
    return minimum

s=input()
n=int(input())
i=1
L=[]

while i!=n+1:
    x=input()
    if isAuto(s,x):
        L.append(x)
    i+=1
print(findSLex(L,s))


def isAuto(s,x):
    lengthS=len(s)
    y=""
    if len(x)<len(s):
        return False
    for i in range(lengthS):
        y+=x[i]
    if y==s:
        return True
    return False

def findSLex(L,s):
    if len(L)==0:
        return s
    minimum=L[0]
    for i in range(len(L)):
        if L[i]<L[0]:
            minimum=L[i]
    return minimum

s=input()
n=int(input())
i=1
L=[]

while i!=n+1:
    x=input()
    if isAuto(s,x):
        L.append(x)
    i+=1
print(findSLex(L,s))


def isAuto(s,x):
    lengthS=len(s)
    y=""
    if len(x)<len(s):
        return False
    for i in range(lengthS):
        y+=x[i]
    if y==s:
        return True
    return False

def findSLex(L,s):
    if len(L)==0:
        return s
    minimum=L[0]
    for i in range(len(L)):
        if L[i]<L[0]:
            minimum=L[i]
    return minimum

s=input()
n=int(input())
i=1
L=[]

while i!=n:
    x=input()
    if isAuto(s,x):
        L.append(x)
print(findSLex(L,s))


def isAuto(s,x):
    y=""
    if len(x)<len(s):
        return False
    for i in range(len(s)):
        y+=x[i]
    if y==s:
        return True
    else:
        return False

def findSLex(L,s):
    if len(L)==0:
        return s
    minimum=L[0]
    for i in range(len(L)):
        if L[i]<L[0]:
            minimum=L[i]
    return minimum

s=input()
n=int(input())
i=1
L=[]

while i!=n+1:
    x=input()
    if isAuto(s,x):
        L.append(x)
    i+=1
print(findSLex(L,s))


s = input()
l = len(s)
n = int(input())
p = []
for i in range(n):
    p.append(input())
    for j in range(l):
        if p[len(p)-1][j] != s[j]:
            p.pop()
            break
p.sort()
if len(p) == 0:
    print(s)
else:
    print(p[0])


s = input()
n = int(input())
current = 'z' * 101
for p in range(n):
  page = input()
  if page.startswith(s) and current > page:
    current = page
if current == 'a' * 101:
  current = s
print(current)


s = input()
n = int(input())
current = 'z' * 101
for p in range(n):
  page = input()
  if page.startswith(s) and current > page:
    current = page
if current == 'z' * 101:
  current = s
print(current)


s = input()
n = int(input())
poss = []
for i in range(n):
	x = input()
	if x[:len(s)] == s:
		poss.append(x)

poss.sort()
if poss:
	print(poss[0])
else:
	print(s)


query = input()
query = query.lower()
n = int(input())
a = []
for i in range(n):
	n = input()
	n = n.lower()
	if query == n[:len(query)]:
		a.append(n)
if not a:
	print(query)
else:
	a = sorted(a)
	print(a[0])


s = input()
n = int(input())
t = s
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        lx = len(x)
        if lx < lt:
            t = x
            lt = lx
        elif lx == lt:
            if ord(x[-1]) < ord(t[-1]):
                t = x
                lt = lx
print(t)


s = input()
n = int(input())
t = ""
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        if t == "":
            t = x
        else:
            if x < t:
                t = x
print(t)


s = input()
n = int(input())
t = s
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        lx = len(x)
        if lx < lt:
            lt = lx
            t = x
        else:
            tx = x[len(s):]
            tt = t[len(s):]
            ltt = len(tt)
            for i in range(len(tx)):
                if i < ltt:
                    break
                if ord(tx[i]) < ord(tt[i]):
                    lt = lx
                    t = x
                    break

print(t)


s = input()
n = int(input())
t = ""
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        if t == "":
            t = x
        else:
            if x < t:
                t = x
if t == "":
    print(s)
else:
    print(t)


s = input()
n = int(input())
t = s
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        lx = len(x)
        if lx < lt:
            t = x
            lt = lx
print(t)


s = input()
n = int(input())
t = s
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        if x < t:
            t = x
print(t)


s = input()
n = int(input())
t = s
lt = 10**10
for a in range(n):
    x = input()
    if x.startswith(s):
        lx = len(x)
        if lx < lt:
            lt = lx
            t = x
        else:
            tx = x[len(s):]
            tt = t[len(s):]
            ltt = len(tt)
            i = 0
            while i < ltt:
                if ord(tx[i]) < ord(tt[i]):
                    lt = lx
                    t = x
                    i = ltt

print(t)


a=input()
b=int(input())
c=[]
for i in range(b):
	d=input()
	if d[:len(a)]==a:
		c.append(d)
if len(c)>0:
	print(min(c))
else:
	print(a)


# Getting Problem-Data from Codeforces.
source,count = input(),int(input())
history = []
for i in range(count):
    history.append(input())
# Algorithm.
found,index,target = False,0,""
history.sort();
while not found and index<count:
    if history[index].find(source) == 0:
        target = history[index]
        found = not found
    else: index = index + 1
# Solution.
print(target) if found else print(source)


# Getting Problem-Data from Codeforces.
source,count = input(),int(input())
history = []
for i in range(count):
    history.append(input())
# Algorithm.
found,index,target = False,0,""
history.sort();
while not found and index<count:
    if history[index].find(source) == 0:
        target = history[index]
        found = not found
    else: index = index + 1
# Solution.
print(target)


###################
# @ June-22th-2019.
###################

######################################################
# Getting Problem-Data from Codeforces.
source,count = input(),int(input())
history = []
for i in range(count):
    history.append(input())
# Algorithm.
found,index,target = False,0,""
history.sort();
while not found and index<count:
    if history[index].find(source) == 0:
        target = history[index]
        found = not found
    else: index = index + 1
# Solution.
print(target) if found else print(source)
######################################################

             #########################################
             # Programming-Credits atifcppprogrammer.
             #########################################


# 53A
# O(n*max{|w|}) time
# O(max{|w|}) space

__author__ = 'artyom'

# SOLUTION

def main():
    q = read(0)
    r = '~'
    for _ in range(read(1)):
        next = read(0)
        if next.startswith(q) and next < r:
            r = next
    return q if r == '~' else r

# HELPERS

def read(mode=1, size=None):
    # 0: String
    # 1: Integer
    # 2: List of strings
    # 3: List of integers
    # 4: Matrix of integers
    if mode == 0: return input().strip()
    if mode == 1: return int(input().strip())
    if mode == 2: return input().strip().split()
    if mode == 3: return list(map(int, input().strip().split()))
    a = []
    for _ in range(size):
        a.append(read(3))
    return a

def write(s="\n"):
    if s is None: s = ''
    if isinstance(s, tuple) or isinstance(s, list): s = ' '.join(map(str, s))
    s = str(s)
    print(s, end="\n")

write(main())


import re
inp = input()
ran = int(input())
lista = list()
w = list()
w = list()

for i in range(ran):
    lista.append(input())

regexs = "^" + inp + ".+"

for item in lista:
    z = re.findall(regexs, item)
    if len(z) > 0:
        w.append(z)

if inp in lista:
    print(inp)
else:
    if len(w) > 0:
        print(min(w)[0])
    else:
        print(inp)


import re
inp = input()
ran = int(input())
lista = list()
y = list()

for i in range(ran):
    lista.append(input())

regexs = "^" + inp + ".*"

for item in lista:
    z = re.findall(regexs, item)
    y.append(z)

if len(min(y)) > 0:
    print(min(y)[0])
else:
    print(inp)


s = input()
l = len(s)
n = int(input())
p = []
for i in range(n):
    p.append(input())
    for j in range(l):
        if p[len(p)-1][j] != s[j]:
            p.pop()
            break
p.sort()
if len(p) == 0:
    print(s)
else:
    print(p[0])


found_list = []
s = input()
n = int(input())

for i in range(n):
    new_s = input()
    if new_s.startswith(s):
        found_list.append(new_s)

if len(found_list) == 0:
    print(s)
else:
    print(min(found_list))


s = input()

n = int(input())

found_list = []

for i in range(n):
    new_s = input()
    if new_s.startswith(s):
        found_list.append(new_s)

found_length = len(found_list)

if found_length != 0:
    result = found_list[1]
    for i in range(found_length-1):
        if len(found_list[i + 1]) < len(result):
            result = found_list[i + 1]
    print(result)
else:
    print(s)


s = input()

n = int(input())

found_list = []

for i in range(n):
    new_s = input()
    if new_s.startswith(s):
        found_list.append(new_s)

found_length = len(found_list)

if found_length != 0:
    result = found_list[0]
    if found_length >= 2:
        for i in range(found_length - 1):
            if len(found_list[i + 1]) < len(result):
                result = found_list[i + 1]
    print(result)
else:
    print(s)


s = input()

n = int(input())

found_list = []

for i in range(n):
    new_s = input()
    if new_s.startswith(s):
        found_list.append(new_s)

found_length = len(found_list)
found_list = sorted(found_list)

if found_length != 0:
    result = found_list[0]
    if found_length >= 2:
        for i in range(found_length - 1):
            if len(found_list[i + 1]) < len(result):
                result = found_list[i + 1]
    print(result)
else:
    print(s)


s=str(input())
n=int(input())
arr=[]
for i in range(n):
    arr.append(str(input()))
arr=sorted(arr)
l=len(s)
c=0
for i in range(n):
    if arr[i][:l]==s:
        print(arr[i])
        c=1
        break
    else:
        continue
if c==0:
    print(s)


s=input()
r=''
n=int(input())
while n:
  a=input()
  if a[:len(s)]==s:
    if r=='' or a<r:
      r=a
  n-=1
print(r=='' and s or r)


a=input().strip()
n=int(input())
k=[input() for i in range(n)]
k=[i for i in k if a in i]
if k:
	print(min(k))
else:
	print(a)


a=input().strip()
n=int(input())
k=[input() for i in range(n)]
p=[]
for i in k:
	if a in i and i.index(a)==0:
		p.append(i)
if p:
	print(min(p))
else:
	print(a)


a=input().strip()
n=int(input())
k=[input() for i in range(n)]
p=[]
for i in k:
	if a in i and i.index(a)==0:
		p.append(i)
if p:
	print(min(k))
else:
	print(a)


a=input().strip()
n=int(input())
k=[input() for i in range(n)]
k=min(k,key=len)
if len(k)<=a:
	print(a)
else:
	print(k)


a=input().strip()
n=int(input())
k=[input() for i in range(n)]
k=min(k,key=len)
if len(k)<=len(a):
	print(a)
else:
	print(k)


palavra = input()
qtd = int(input())
history = [input() for i in range(qtd)]
history.sort()

new = []

for p in history:
	if p >= palavra:
		new.append(p)

if len(new) == 0:
	print(palavra)
else:
	first = new[0]
	if first == palavra:
		print(palavra)
	else:
		if len(first) >= len(palavra):
			if first[:len(palavra)] == palavra:
				print(first)
			else:
				print(palavra)
		else:
			print(palavra)


inp= input()
a=[]

for _ in range(int(input())):
  a.append(input())

ans=max(a)
for i in a:
  if i.startswith(inp) and i<ans:
    ans=i
if(ans != max(a)):
  print(ans)
else:
  print(inp)


inp= input()
a=[]

for _ in range(int(input())):
  a.append(input())

ans=max(a)
c=False
for i in a:
  if i.startswith(inp) and i<=ans:
    ans=i
    c=True
if(c):
  print(ans)
else:
  print(inp)


s = gets.chomp!
n = gets.to_i
visited = $stdin.readlines.map(&:chomp!).sort!
t = visited.bsearch{|x| x >= s}
if t.nil? or (/#{s}/ =~ t) != 0
  puts s
else
  puts t
end


word = gets.strip
n    = gets.to_i

a = []
o = []

for i in 0..(n-1)
  a << gets.strip
end

a.each {|x| o << x if x.start_with?(word) }

o.length == 0 ? (puts word) : (puts o.min)


str = STDIN.gets.chomp
n   = STDIN.gets.to_i
lines = []
n.times { lines << STDIN.gets.chomp }

lines.sort.each do |line|
  if line.rindex(str) == 0
    puts line
    exit
  end
end
puts str


s, n = gets.chomp, gets.to_i
lines = []
n.times { lines << gets.chomp }

lines.sort.each do |line|
  if line.start_with?(s)
    puts line; exit
  end
end
puts s


=begin 59A - Word
str = gets.chomp
lower = 0
upper = 0
str.each_char { |w| /[[:lower:]]/.match(w) ? lower+= 1 : upper+= 1 }
puts upper > lower ? str.upcase : str.downcase
=end
str = gets.chomp
n = gets.to_i
max = 101
out = str
while n > 0
	tmp = gets.chomp
  if /\A#{str}[[:lower:]]*/.match(tmp)
    if tmp.size < max
      max = tmp.size
      out = tmp
    end
  end
  n-=1
end
puts out


=begin 59A - Word
str = gets.chomp
lower = 0
upper = 0
str.each_char { |w| /[[:lower:]]/.match(w) ? lower+= 1 : upper+= 1 }
puts upper > lower ? str.upcase : str.downcase
=end
str = gets.chomp
n = gets.to_i
max = 101
out = str
while n > 0
	tmp = gets.chomp
  if /\A#{str}[[:lower:]]*/.match(tmp)
    if tmp.size < max
      max = tmp.size
      out = tmp
    elsif tmp.size == max
      out = tmp < out ? tmp : out
    end
  end
  n-=1
end
puts out