//written by raj_0jaiswal
#include <bits/stdc++.h>
using namespace std;
#define crt()  int t; cin>>t; while(t--)
#define ll long long
#define mod 1000000007
#define mxx 100005
#define sc(n) scanf("%d",&n)
#define out(x) #x << " = " << x << "   "
#define DBG(vari) cerr<<#vari<<" ==> "<<(vari)<<endl;
#define pb                      push_back
#define vt                      vector<int>
#define pi                      pair<int,int>
#define mp                      make_pair
#define ff                      first
#define ss                      second
#define maX(a,b)                ((a)>(b)?(a):(b))
#define miN(a,b)                ((a)<(b)?(a):(b))
#define INF                     (int)1e9
#define SORT(_a,_b)             sort(_a,_b,greater<int>())
#define dbg(n)                  cout<<n<<" i'm here "<<endl;
#define DPdbg(n)                for(int i=0;i<=n;i++){for(int j=0;j<=n;j++)cout<<dp[i][j]<<" ";cout<<endl;}
/*inline int scan(){ register int t=0;char c=getchar_unlocked();while(c<'0' || c>'9')c=getchar_unlocked();
while(c>='0' && c<='9'){t=(t<<3)+(t<<1)+c-'0';c=getchar_unlocked();} return(t);}*/
ll power(ll aa,ll pp){ ll xx=1,yy=aa%mod; while(pp>0){ if(pp&1) xx=(xx*yy)%mod; yy=(yy*yy)%mod; pp=pp>>1;} return xx%mod;}
bool cmp(pi a,pi b){if(a.ff!=b.ff)return a.ff<b.ff; else return a.ss<b.ss;}
pair<int,int> A[200005];
int main()
{
    ios_base::sync_with_stdio(false);
    int n,i,k,q1,q2,h1,h2,id;
    ll h,q,ans=0;
    cin>>n>>k;
    for(i=0;i<n;i++){
        cin>>A[i].ff;
        A[i].ss=i;
    }
    sort(A,A+n);
    //for(i=0;i<n;i++)
        //cout<<i<<" "<<A[i].ff<<" "<<A[i].ss<<endl;
    for(i=1;i<=n;i++){
        if(abs(A[i].ff)%k==0 && abs(A[i].ff*k)<1e9+7){
            id=A[i].ss;
            int pre=(A[i].ff)/k;
            int next=(A[i].ff)*k;
            q1=lower_bound(A,A+n,mp(pre,0))-A;
            q2=lower_bound(A,A+n,mp(pre,id))-A;
            //cout<<q1<<" "<<q2<<endl;
            q=q2-q1;
            h1=lower_bound(A,A+n,mp(next,id+1))-A;
            h2=lower_bound(A,A+n,mp(next,n))-A;
            h=h2-h1;
            ans+=q*h;
        }
    }
    cout<<ans<<endl;
    return 0;
}

//12 3
//1 2 6 2 3 6 9 18 3 9 2 3


//written by raj_0jaiswal
#include <bits/stdc++.h>
using namespace std;
#define crt()  int t; cin>>t; while(t--)
#define ll long long
#define mod 1000000007
#define mxx 100005
#define sc(n) scanf("%d",&n)
#define out(x) #x << " = " << x << "   "
#define DBG(vari) cerr<<#vari<<" ==> "<<(vari)<<endl;
#define pb                      push_back
#define vt                      vector<int>
#define pi                      pair<int,int>
#define mp                      make_pair
#define ff                      first
#define ss                      second
#define maX(a,b)                ((a)>(b)?(a):(b))
#define miN(a,b)                ((a)<(b)?(a):(b))
#define INF                     (int)1e9
#define SORT(_a,_b)             sort(_a,_b,greater<int>())
#define dbg(n)                  cout<<n<<" i'm here "<<endl;
#define DPdbg(n)                for(int i=0;i<=n;i++){for(int j=0;j<=n;j++)cout<<dp[i][j]<<" ";cout<<endl;}
/*inline int scan(){ register int t=0;char c=getchar_unlocked();while(c<'0' || c>'9')c=getchar_unlocked();
while(c>='0' && c<='9'){t=(t<<3)+(t<<1)+c-'0';c=getchar_unlocked();} return(t);}*/
ll power(ll aa,ll pp){ ll xx=1,yy=aa%mod; while(pp>0){ if(pp&1) xx=(xx*yy)%mod; yy=(yy*yy)%mod; pp=pp>>1;} return xx%mod;}
bool cmp(pi a,pi b){if(a.ff!=b.ff)return a.ff<b.ff; else return a.ss<b.ss;}
pair<int,int> A[200005];
int main()
{
    ios_base::sync_with_stdio(false);
    int n,i,k,q1,q2,h1,h2,h,q,id,ans=0;
    cin>>n>>k;
    for(i=0;i<n;i++){
        cin>>A[i].ff;
        A[i].ss=i;
    }
    sort(A,A+n);
    //for(i=0;i<n;i++)
        //cout<<i<<" "<<A[i].ff<<" "<<A[i].ss<<endl;
    for(i=1;i<=n;i++){
        if(abs(A[i].ff)%k==0 && abs(A[i].ff*k)<1e9+7){
            id=A[i].ss;
            int pre=(A[i].ff)/k;
            int next=(A[i].ff)*k;
            q1=lower_bound(A,A+n,mp(pre,0))-A;
            q2=lower_bound(A,A+n,mp(pre,id))-A;
            //cout<<q1<<" "<<q2<<endl;
            q=q2-q1;
            h1=lower_bound(A,A+n,mp(next,id+1))-A;
            h2=lower_bound(A,A+n,mp(next,n))-A;
            h=h2-h1;
            ans+=q*h;
        }
    }
    cout<<ans<<endl;
    return 0;
}

//12 3
//1 2 6 2 3 6 9 18 3 9 2 3


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, int> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if((x%k) == 0) cnt1[x] += cnt[x/k];
		if((x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			int tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, int> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if(abs(k) != 1 && (x%k) == 0) cnt1[x] += cnt[x/k];
		if((abs(k) != 1 && x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}
	if(cnt[1] >= 3) {
		ll tmp = cnt[1];
		ans += (tmp*(tmp-1)*(tmp-2))/6;
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, ll> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if(x && (x%k) == 0) cnt1[x] += cnt[x/k];
		if(x && (x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}
	ll tmp = cnt[0];
	if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, ll>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, int> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if((x%k) == 0) cnt1[x] += cnt[x/k];
		if((x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, ll> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if((x%k) == 0) cnt1[x] += cnt[x/k];
		if((x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, ll>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, ll> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if((x%k) == 0) cnt1[x] += cnt[x/k];
		if((x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, int> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if((x%k) == 0) cnt1[x] += cnt[x/k];
		if((x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


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

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define eps 1e-9

//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fi(i, n) for(int i = 0; i <= n; ++i)

//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)

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

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

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

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

#define dbg(args...) do {cerr << #args << ": "; DBG, args; cerr << endl; } while(0)
#define deb(a, n) cerr << #a << ": "; for(int i = 0; i < n; ++i) cerr << a[i] << " "; cerr << endl;

struct debugger {
	template <typename T> debugger &operator , (const T &v) {
		cerr << v << ' ';
		return *this;
	}
} DBG;

//Direction Array
//int fx[]={1, -1, 0, 0};
//int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1};
//int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
//end of template

ll ans;
map <int, int> cnt, cnt1;

int main () {
	int n, k, x;

	sii(n, k);
	rep(i, n) {
		si(x);

		if(abs(k) != 1 && (x%k) == 0) cnt1[x] += cnt[x/k];
		if((abs(k) != 1 && x%(k*k)) == 0) ans += cnt1[x/k];

		++cnt[x];
	}
	if(cnt[1] >= 3) {
		ll tmp = cnt[1];
		ans += (tmp*(tmp-1)*(tmp-2))/6;
	}
	if(cnt[-1] >= 3) {
		ll tmp = cnt[1];
		ans += (tmp*(tmp-1)*(tmp-2))/6;
	}

	if(k > 1) printf("%lld\n", ans);
	else {
		ans = 0;
		map<int, int>::iterator it;
		for(it = cnt.begin(); it != cnt.end(); ++it) {
			ll tmp = (*it).S;
			if(tmp >= 3) ans += (tmp*(tmp-1)*(tmp-2))/6;//(((tmp*(tmp-1))/2)*(tmp-2))/3;
		}
		printf("%lld\n", ans);
	}

	return 0;
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int main()
{
    int n,k;
    long long counter = 0;
    cin >> n >> k;
    int a[200005], ahead[200005]={}, behind[200005] = {};
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        ahead[a[i]]++;
    }
    for(int i=0;i<n;i++)
    {
        ahead[a[i]]--;
        float kw = (float)a[i] / (float)k;
        //cout << kw << " " << floor(kw) << endl;
        if (kw != floor(kw))
        {
            behind[a[i]]++;
            continue;
        }
        int kx = a[i] * k;
        //cout << ahead[kx] << " " << behind[(int)kw] << endl;
        counter += ahead[(int)kx] * behind[(int)kw];
        behind[a[i]]++;
    }
    cout << counter;
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int main()
{
    int n,k;
    map< ll , ll >ma;
    map< ll , ll >mb;
    map<ll , ll>::iterator it;
    long long counter = 0;
    cin >> n >> k;
    long long a[200005], ahead[200005]={}, behind[200005] = {};
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        it = ma.find(a[i]);
        if(it != ma.end())
        {

            (it->second)++;
        }
        else
            ma[a[i]] = 1;
        mb[a[i]] = 0;
    }
    for(int i=0;i<n;i++)
    {
        (ma.find(a[i])->second)--;
        float kw = (float)a[i] / (float)k;
        //cout << kw << " " << floor(kw) << endl;
        if (kw != floor(kw))
        {
            (mb.find(a[i])->second)++;
            continue;
        }
        int kx = a[i] * k;
        //cout << ahead[kx] << " " << behind[(int)kw] << endl;
        counter += (ma.find((ll)kx)->second) * ((mb.find((ll)kw))->second);
        (mb.find(a[i])->second)++;
    }
    cout << counter;
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

map< ll , ll >ma;
map< ll , ll >mb;
map<ll , ll>::iterator it;
long long a[200005];

int main()
{
    int n,k;
    long long counter = 0;
    cin >> n >> k;

    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        it = ma.find(a[i]);
        if(it != ma.end())
        {

            (it->second)++;
        }
        else
            ma.insert ( pair<ll , ll>(a[i],1) );

        mb.insert ( pair<ll , ll>(a[i],0) );
    }
    for(int i=0;i<n;i++)
    {
        (ma.find(a[i])->second)--;
        float kw = (float)a[i] / (float)k;
        if (kw != floor(kw))
        {
            (mb.find(a[i])->second)++;
            continue;
        }
        int kx = a[i] * k;
        //cout << ahead[kx] << " " << behind[(int)kw] << endl;
        counter += (ma.find((ll)kx)->second) * ((mb.find((ll)kw))->second);
        (mb.find(a[i])->second)++;
    }
    cout << counter;
}


#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

int main()
{
    int n,k;
    long long counter = 0;
    cin >> n >> k;
    long long a[200005], ahead[200005]={}, behind[200005] = {};
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        //if(a[i] > 0)
        ahead[a[i]]++;
    }
    for(int i=0;i<n;i++)
    {
        ahead[a[i]]--;
        float kw = (float)a[i] / (float)k;
        //cout << kw << " " << floor(kw) << endl;
        if (kw != floor(kw))
        {
            behind[a[i]]++;
            continue;
        }
        int kx = a[i] * k;
        //cout << ahead[kx] << " " << behind[(int)kw] << endl;
        //if (ahead[(int)kx] * behind[(int)kw] > 0) cout << a[i] << " "  << i << " " << ahead[(int)kx] * behind[(int)kw] << endl;
        counter += ahead[(int)kx] * behind[(int)kw];
        behind[a[i]]++;
    }
    cout << counter;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;

const int maxn = 2e6+5;
const int inf = 0x3f3f3f3f;

int n,m,p;
pair<int,int>a[maxn],b[maxn];
pair<pair<int,int>,int>c[maxn];
LL lazy[maxn],ma[maxn];
int ub(int x){
    int l=1,r=m,ans=m+1;
    while(l<=r){
        int mid=l+r>>1;
        if(b[mid].fi<=x){
            l=mid+1;
        } else {
            ans=min(ans,mid);
            r=mid-1;
        }
    }
    return ans;
}
void push_up(int rt,int l,int r){
     ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
}
void push_down(int rt,int l,int r){
     if(lazy[rt]){
        lazy[rt<<1]+=lazy[rt];
        ma[rt<<1]+=lazy[rt];

        lazy[rt<<1|1]+=lazy[rt];
        ma[rt<<1|1]+=lazy[rt<<1|1];
        lazy[rt]=0;
     }
}

void update(int rt,int l,int r,int x,int y,LL val){
      if(x<=l&&r<=y){
         lazy[rt]+=val;ma[rt]+=val;
         return ;
      }
      push_down(rt,l,r);
      int mid=l+r>>1;
      if(x<=mid) update(rt<<1,l,mid,x,y,val);
      if(y>mid) update(rt<<1|1,mid+1,r,x,y,val);
      push_up(rt,l,r);
}
int main(){
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].fi,&a[i].se);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&b[i].fi,&b[i].se);
    }
    for(int i=1;i<=p;i++){
        scanf("%d%d%d",&c[i].fi.se,&c[i].fi.fi,&c[i].se);
    }

    sort(a+1,a+1+n);
    sort(b+1,b+1+m);
    sort(c+1,c+1+p);
    for(int i=1;i<=m;i++){
        update(1,1,m,i,i,-b[i].se);
    }
//    for(int i=1;i<=n;i++) cout<<a[i].fi<<',';cout<<'\n';
//    for(int i=1;i<=m;i++) cout<<b[i].fi<<',';cout<<'\n';
//    for(int i=1;i<=p;i++) cout<<c[i].fi.se<<',';cout<<'\n';

    //cout<<ma[1]<<endl;
    LL ans=-inf,res=0,t=1;
    for(int i=1;i<=n;i++){
        while(t<=p&&c[t].fi.fi<a[i].fi){
            int pos=ub(c[t].fi.se);
            //cout<<i<<','<<pos<<','<<c[t].fi.se<<endl;
            if(pos<=m)
            update(1,1,m,pos,m,c[t].se);
            t++;
        }
        res=ma[1]-a[i].se;
        ans=max(ans,res);
        //printf("%lld %lld\n",ma[1],ans);
    }
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;

const int maxn = 2e6+5;
const LL inf = 0x3f3f3f3f3f3f3f3f;

int n,m,p,vis[maxn];
pair<int,int>a[maxn],b[maxn];
pair<pair<int,int>,int>c[maxn];
LL lazy[maxn],ma[maxn];
int ub(int x){
    int l=1,r=m,ans=m+1;
    while(l<=r){
        int mid=l+r>>1;
        if(b[mid].fi<=x){
            l=mid+1;
        } else {
            ans=min(ans,mid);
            r=mid-1;
        }
    }
    return ans;
}
void push_up(int rt,int l,int r){
     ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);
}
void push_down(int rt,int l,int r){
     if(lazy[rt]){
        lazy[rt<<1]+=lazy[rt];
        ma[rt<<1]+=lazy[rt];

        lazy[rt<<1|1]+=lazy[rt];
        ma[rt<<1|1]+=lazy[rt];
        lazy[rt]=0;
     }
}

void update(int rt,int l,int r,int x,int y,LL val){
      if(x<=l&&r<=y){
         lazy[rt]+=val;ma[rt]+=val;
         return ;
      }
      push_down(rt,l,r);
      int mid=l+r>>1;
      if(x<=mid) update(rt<<1,l,mid,x,y,val);
      if(y>mid) update(rt<<1|1,mid+1,r,x,y,val);
      push_up(rt,l,r);
}
int main(){
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].fi,&a[i].se);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&b[i].fi,&b[i].se);
    }
    for(int i=1;i<=p;i++){
        scanf("%d%d%d",&c[i].fi.fi,&c[i].fi.se,&c[i].se);
    }

    sort(a+1,a+1+n);
    sort(b+1,b+1+m);
    sort(c+1,c+1+p);
   // init();
    //cout<<n<<','<<m<<'\n';
    for(int i=1;i<=m;i++){
        update(1,1,m,i,i,-b[i].se);
    }
    //cout<<ma[1]<<endl;
    LL ans=-inf,res=0,t=1;
    for(int i=1;i<=n;i++){
        while(t<=p&&c[t].fi.fi<a[i].fi){
            int pos=ub(c[t].fi.se);
            //cout<<i<<','<<pos<<','<<c[t].fi.se<<endl;
            if(pos<=m)
            update(1,1,m,pos,m,c[t].se);
            t++;
        }
        res=ma[1]-a[i].se;
        ans=max(ans,res);
        //printf("%lld %lld\n",ma[1],ans);
    }
    printf("%lld\n",ans);
}
/*
3 3 3
6 1010
9 1153
9 1159
9 1150
7 1058
9 1159
7 3 172
10 3 167
6 6 160
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,int v){
	if(l>r) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
int a[1000100],d[1000100];
struct node{
	int y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	memset(a,0x3f,sizeof(a));
	memset(d,0x3f,sizeof(d));
	for(int i=1;i<=n;i++){
		int id,v;
		scanf("%d%d",&id,&v);
		if(a[id]==0) a[id]=v;
		else a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		int id,v;
		scanf("%d%d",&id,&v);
		if(d[id]==0) d[id]=v;
		else d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		int id,y,z;
		scanf("%d%d%d",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	int ans=0;int c=0;int pt=1;
	for(int i=1;i<=1000000;i++){
		c=a[i];
		for(;pt<i;pt++){
			for(node x:mon[pt]){
				modi(1,1,1000000,min(x.y+1,1000000),1000000,x.z);
			}
		}
		ans=max(ans,seg[1]-c);
		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
	//	d[i]=min(d[i+1],d[i]);
	//	a[i]=min(a[i+1],a[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000000;i++){
		ans=max(ans,seg[1]-a[i]);
			for(node x:mon[i-1]){
				modi(1,1,1000000,x.y+1,1000000,x.z);
			}

		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,int v){
	if(l>r || tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
int a[1000100],d[1000100];
struct node{
	int y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	memset(a,0x3f,sizeof(a));
	memset(d,0x3f,sizeof(d));
	for(int i=1;i<=n;i++){
		int id,v;
		scanf("%d%d",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		int id,v;
		scanf("%d%d",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		int id,y,z;
		scanf("%d%d%d",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	int ans=0;int c=0;int pt=1;
	for(int i=1;i<=1000000;i++){
		c=a[i];
		for(;pt<i;pt++){
			for(node x:mon[pt]){
				modi(1,1,1000000,x.y+1,1000000,x.z);
			}
		}
		ans=max(ans,seg[1]-c);
		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	if(l>r || tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=1;i<1000100;i++){
		a[i]=1e15;
		d[i]=1e15;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=0;ll c=0;int pt=1;
	for(int i=1;i<=1000000;i++){
		c=a[i];
		for(;pt<i;pt++){
			for(node x:mon[pt]){
				modi(1,1,1000000,x.y+1,1000000,x.z);
			}
		}
		ans=max(ans,seg[1]-c);
		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	//if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000010;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		a[i]=min(a[i+1],a[i]);
		modi(1,1,1000010,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000010;i++){
		ans=max(ans,seg[1]-a[i]);
			for(node x:mon[i-1]){
				if(x.y+1<=1000010) modi(1,1,1000010,x.y+1,1000010,x.z);
			}

		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	//if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000010;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		a[i]=min(a[i+1],a[i]);
		modi(1,1,1000010,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000010;i++){

		for(node x:mon[i-1]){
			if(x.y+1<=1000010) modi(1,1,1000010,x.y+1,1000010,x.z);
		}
		ans=max(ans,seg[1]-a[i]);
		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}

	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000010;i>=1;i--){
	//	d[i]=min(d[i+1],d[i]);
	//	a[i]=min(a[i+1],a[i]);
		modi(1,1,1000010,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000010;i++){
		ans=max(ans,seg[1]-a[i]);
			for(node x:mon[i-1]){
				if(x.y+1<=1000010) modi(1,1,1000010,x.y+1,1000010,x.z);
			}

		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	if(l>r || tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
		d[i]=min(d[i+1],d[i]);
		a[i]=min(a[i+1],a[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=0;ll c=0;int pt=1;
	for(int i=1;i<=1000000;i++){
		c=a[i];
		for(;pt<i;pt++){
			for(node x:mon[pt]){
				modi(1,1,1000000,x.y+1,1000000,x.z);
			}
		}
		ans=max(ans,seg[1]-c);
		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	//if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000010;i>=1;i--){
	//	d[i]=min(d[i+1],d[i]);
	//	a[i]=min(a[i+1],a[i]);
		modi(1,1,1000010,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000010;i++){
		ans=max(ans,seg[1]-a[i]);
			for(node x:mon[i-1]){
				if(x.y+1<=1000010) modi(1,1,1000010,x.y+1,1000010,x.z);
			}

		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll seg[1000100<<2],fg[1000100<<2];
void push(int pos){
	if(fg[pos]){
		seg[pos<<1]+=fg[pos];
		fg[pos<<1]+=fg[pos];
		seg[pos<<1|1]+=fg[pos];
		fg[pos<<1|1]+=fg[pos];
		fg[pos]=0;
	}
	return;
}
void modi(int pos,int l,int r,int tl,int tr,ll v){
	if(tl>tr) return;
	if(l==tl && r==tr){
		seg[pos]+=v;
		fg[pos]+=v;
		return;
	}
	push(pos);
	int md=(l+r)>>1;
	if(md>=tr){
		modi(pos<<1,l,md,tl,tr,v);
	}
	else if(md<tl){
		modi(pos<<1|1,md+1,r,tl,tr,v);
	}
	else{
		modi(pos<<1,l,md,tl,md,v);
		modi(pos<<1|1,md+1,r,md+1,tr,v);
	}
	seg[pos]=max(seg[pos<<1],seg[pos<<1|1]);
	return;
}
int n,m,p;
ll a[1000100],d[1000100];
struct node{
	ll y,z;
};
vector<node> mon[1000100];
int main(){
	scanf("%d%d%d",&n,&m,&p);
	for(int i=0;i<1000100;i++){
		a[i]=1e18;
		d[i]=1e18;
	}
	for(int i=1;i<=n;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		a[id]=min(a[id],v);
	}
	for(int i=1;i<=m;i++){
		ll id,v;
		scanf("%lld%lld",&id,&v);
		d[id]=min(d[id],v);
	}
	for(int i=1000000;i>=1;i--){
	//	d[i]=min(d[i+1],d[i]);
	//	a[i]=min(a[i+1],a[i]);
		modi(1,1,1000000,i,i,-d[i]);
	}
	for(int i=1;i<=p;i++){
		ll id,y,z;
		scanf("%lld%lld%lld",&id,&y,&z);
		mon[id].push_back((node){y,z});
	}
	ll ans=-1e18;int pt=1;
	for(int i=1;i<=1000000;i++){
		ans=max(ans,seg[1]-a[i]);
			for(node x:mon[i-1]){
				if(x.y+1<=1000000) modi(1,1,1000000,x.y+1,1000000,x.z);
			}

		//if(i<=10){
		//	printf("%d: %d\n",i,ans);
		//}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        scanf("%d %d", &A[i].a, &A[i].ca);
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        scanf("%d %d", &B[i].b, &B[i].cb);
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].z);
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    //cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
        //cout<<t[1]<<'\n';
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    //cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    //cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
        //cout<<t[1]<<'\n';
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0}, maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);
    /*
    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }
    */
    int last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    int ans = -A[0].ca - B[0].cb;

    int j = 0;
    for (int i = 0; i < n; i++){
        int best = -A[i].ca;
        while ((j<p) && (C[j].x<A[i].a)){
            upd(1,1,maxb,C[j].y+1,maxb,C[j].z);
            j++;
        }
        add(best,t[1]);
        ans = max(ans,best);
    }

    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0}, maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    int ans = -A[0].ca - B[0].cb;

    int j = 0;
    for (int i = 0; i < n; i++){
        int best = -A[i].ca;
        while ((j<p) && (C[j].x<A[i].a)){
            upd(1,1,maxb,C[j].y+1,maxb,C[j].z);
            j++;
        }
        add(best,t[1]);
        ans = max(ans,best);
    }

    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0};
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int  r, int val){
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        scanf("%d %d", &A[i].a, &A[i].ca);
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        scanf("%d %d", &B[i].b, &B[i].cb);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].z);
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }

    int maxb = 1000000;
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);

    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        scanf("%d %d", &A[i].a, &A[i].ca);
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        scanf("%d %d", &B[i].b, &B[i].cb);
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].z);
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    //cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
        cout<<t[1]<<'\n';
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        scanf("%d %d", &A[i].a, &A[i].ca);
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        scanf("%d %d", &B[i].b, &B[i].cb);
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].z);
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    //cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        scanf("%d %d", &A[i].a, &A[i].ca);
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        scanf("%d %d", &B[i].b, &B[i].cb);
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].z);
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    //cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    //cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
        //cout<<t[1]<<'\n';
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0},maxb = 0;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;

    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
        maxb = max(maxb, B[i].b);
    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);

    for (int i = n - 2; i >= 0; i--){
        A[i].ca = min(A[i].ca, A[i+1].ca);
    }
    for (int i = m - 2; i >= 0; i--){
        B[i].cb = min(B[i].cb, B[i+1].cb);
    }

    int last = 0;
    for (int i = 0; i < n; i++){
        for (int j = A[i].a; j > last; j--){
            besta[j] = -A[i].ca;
        }
        last = A[i].a;
    }
    for (int i = A[n-1].a+1; i < MAXVAL; i++){
        besta[i] = INT_MIN;
    }
    cout<<maxb<<" here**\n";
    last = 0;
    for (int i = 0; i < m; i++){
        upd(1,1,maxb,last+1,B[i].b,-B[i].cb);
        last = B[i].b;
    }
    upd(1,1,maxb,last+1,maxb,INT_MIN);
    //cout<<t[1]<<'\n';
    int ans = -A[0].ca - B[0].cb;
    for (int i = 0; i < p; i++){
        int best = besta[C[i].x+1];
        upd(1,1,maxb,C[i].y+1,maxb,C[i].z);
        add(best,t[1]);
        ans = max(ans, best);
        //cout<<t[1]<<'\n';
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0}, maxb = 1000000;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

void set_val(int v, int tl, int tr, int pos, int val){
    if (tl==tr){
        t[v] = val;
        return;
    }
    int tm = (tl+tr)/2;
    if (pos<=tm){
        set_val(v*2,tl,tm,pos,val);
    }
    else{
        set_val(v*2+1,tm+1,tr,pos,val);
    }
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;
    for (int i = 0; i < N; ++i){
        t[i] = INT_MIN;
    }
    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
        set_val(1,1,maxb,B[i].b,-B[i].cb);

    }
    sort(B,B+n,comp2);

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);

    int ans = -A[0].ca - B[0].cb;

    int j = 0;
    for (int i = 0; i < n; i++){
        int best = -A[i].ca;
        while ((j<p) && (C[j].x<A[i].a)){
            upd(1,1,maxb,C[j].y+1,maxb,C[j].z);
            j++;
        }
        add(best,t[1]);
        ans = max(ans,best);
    }

    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits.h>
#include <climits>
#include <map>
#include <unordered_map>
#include <cmath>
#include <set>
#include <unordered_set>
#include <iterator>
#include <deque>
#include <assert.h>
#include <cstdlib>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ull min(ull x, ull y){ if (x<y) return x; return y;}
ull max(ull x, ull y){ if (x<y) return y; return x;}
ll min(ll x, ll y){ if (x<y) return x; return y;}
ll max(ll x, ll y){ if (x<y) return y; return x;}
double min(double x, double y){ if (x<y) return x; return y;}
double max(double x, double y){ if (x<y) return y; return x;}
ull gcd(ull x, ull y){
    if (!x) return y;
    if (!y) return x;
    if (x>y) swap(x,y);
    return gcd(x, y%x);
}
ull inv(ull a, ull c){ // a,c are coprime and a<c
    if (a==1){
        return 1;
    }
    return ((c-(c/a))*inv(c%a,c))%c;
}

#define maxn 200005
#define N 2097152
#define MAXVAL 1000005
typedef struct{
    int a, ca;
} weapon;
typedef struct{
    int b, cb;
} armor;
typedef struct{
    int x, y, z;
} monst;

weapon A[maxn];
armor B[maxn];
monst C[maxn];
int n, m, p, besta[MAXVAL] = {0}, t[N] = {0}, lazy[N] = {0}, maxb = 1000000;
int comp1(weapon i, weapon j){
    return i.a<j.a;
}
int comp2(armor i, armor j){
    if (i.b==j.b) return i.cb<j.cb;
    return i.b<j.b;
}
int comp3(monst i, monst j){
    return i.x<j.x;
}
void add(int &a, int b){
    if ((a==INT_MAX) || (a==INT_MIN)) return;
    if ((b==INT_MAX) || (b==INT_MIN)){
        a=b;
        return;
    }
    a+=b;
}
void push(int v){
    add(t[v*2],lazy[v]);
    add(lazy[v*2],lazy[v]);
    add(t[v*2+1],lazy[v]);
    add(lazy[v*2+1],lazy[v]);
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    //cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<val<<"\n";
    if (l>r) return;
    if ((l==tl) && (r==tr)){
        add(t[v],val);
        add(lazy[v],val);
        return;
    }
    push(v);
    int tm = (tl+tr)/2;
    upd(v*2,tl,tm,l,min(tm,r),val);
    upd(v*2+1,tm+1,tr,max(tm+1,l),r,val);
    t[v] = max(t[v*2],t[v*2+1]);
}

void set_val(int v, int tl, int tr, int pos, int val){
    if (tl==tr){
        t[v] = val;
        return;
    }
    int tm = (tl+tr)/2;
    if (pos<=tm){
        set_val(v*2,tl,tm,pos,val);
    }
    else{
        set_val(v*2+1,tm+1,tr,pos,val);
    }
    t[v] = max(t[v*2],t[v*2+1]);
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>n>>m>>p;
    for (int i = 0; i < N; ++i){
        t[i] = INT_MIN;
    }
    for (int i = 0; i < n; i++){
        cin>>A[i].a>>A[i].ca;
    }
    sort(A,A+n,comp1);

    for (int i = 0; i < m; i++){
        cin>>B[i].b>>B[i].cb;
    }
    sort(B,B+n,comp2);
    for (int i = 0; i < m; i++){
        if ((i==0) || (B[i].b!=B[i-1].b)) set_val(1,1,maxb,B[i].b,-B[i].cb);
    }

    for (int i = 0; i < p; i++){
        cin>>C[i].x>>C[i].y>>C[i].z;
    }
    sort(C,C+p,comp3);

    int ans = -A[0].ca - B[0].cb;

    int j = 0;
    for (int i = 0; i < n; i++){
        int best = -A[i].ca;
        while ((j<p) && (C[j].x<A[i].a)){
            upd(1,1,maxb,C[j].y+1,maxb,C[j].z);
            j++;
        }
        add(best,t[1]);
        ans = max(ans,best);
    }

    cout<<ans<<"\n";
    return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M,P;
pair<int,int> A[202020];
ll BC[1<<20];
int X[202020],Y[202020],Z[202020];

static ll const def=-1LL<<60;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val, ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2,0);
	};

	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return ma[k];
		return val[k]+max(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}

	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r) return;
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k]+=v;
		}
		else if(l < y && x < r) {
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			ma[k]=val[k]+max(ma[k*2],ma[k*2+1]);
		}
	}
};
SegTree_3<ll,1<<20> st;
vector<pair<int,int>> ev[1010101];

void solve() {
	int i,j,k,l,r,x,y; string s;

	scanf("%d%d%d",&N,&M,&P);

	FOR(i,N) scanf("%d%d",&A[i].first,&A[i].second);
	FOR(i,1<<20) BC[i]=-1LL<<60;
	FOR(i,M) {
		scanf("%d%d",&x,&y);
		BC[x]=max(BC[x],(ll)-y);
	}

	for(i=(1<<20)-2;i>=0;i--) BC[i]=max(BC[i],BC[i+1]);
	FOR(i,1<<20) st.update(i,i+1,BC[i]);

	FOR(i,P) {
		scanf("%d%d%d",&X[i],&Y[i],&Z[i]);
		ev[X[i]].push_back({Y[i],Z[i]});
	}

	ll ma=-1LL<<60;
	sort(A,A+N);
	int pre=0;
	FOR(i,N) {
		while(pre<A[i].first) {
			FORR(e,ev[pre]) {
				st.update(e.first+1,1<<20,e.second);
			}
			pre++;
		}
		ll sc=-A[i].second+st.getval(0,1<<20);
		ma=max(ma,sc);
	}
	cout<<ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M,P;
pair<int,int> A[202020];
int BC[1<<20];
int X[202020],Y[202020],Z[202020];

static ll const def=-1LL<<60;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val, ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2,0);
	};

	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return ma[k];
		return val[k]+max(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}

	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r) return;
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k]+=v;
		}
		else if(l < y && x < r) {
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			ma[k]=val[k]+max(ma[k*2],ma[k*2+1]);
		}
	}
};
SegTree_3<ll,1<<20> st;
vector<pair<int,int>> ev[1010101];

void solve() {
	int i,j,k,l,r,x,y; string s;

	scanf("%d%d%d",&N,&M,&P);

	FOR(i,N) scanf("%d%d",&A[i].first,&A[i].second);
	FOR(i,1<<20) BC[i]=-1<<30;
	FOR(i,M) {
		scanf("%d%d",&x,&y);
		BC[x]=max(BC[x],-y);
	}

	for(i=(1<<20)-2;i>=0;i--) BC[i]=max(BC[i],BC[i+1]);
	FOR(i,1<<20) st.update(i,i+1,BC[i]);

	FOR(i,P) {
		scanf("%d%d%d",&X[i],&Y[i],&Z[i]);
		ev[X[i]].push_back({Y[i],Z[i]});
	}

	ll ma=-1LL<<60;
	sort(A,A+N);
	int pre=0;
	FOR(i,N) {
		while(pre<A[i].first) {
			FORR(e,ev[pre]) {
				st.update(e.first+1,1<<20,e.second);
			}
			pre++;
		}
		ll sc=-A[i].second+st.getval(0,1<<20);
		ma=max(ma,sc);
		ma=max(ma,(ll)(-A[i].second-BC[0]));
	}
	cout<<ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M,P;
pair<int,int> A[202020];
int BC[1<<20];
int X[202020],Y[202020],Z[202020];

static ll const def=-1LL<<60;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val, ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2,0);
	};

	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return ma[k];
		return val[k]+max(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}

	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r) return;
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k]+=v;
		}
		else if(l < y && x < r) {
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			ma[k]=val[k]+max(ma[k*2],ma[k*2+1]);
		}
	}
};
SegTree_3<ll,1<<20> st;
vector<pair<int,int>> ev[1010101];

void solve() {
	int i,j,k,l,r,x,y; string s;

	scanf("%d%d%d",&N,&M,&P);

	FOR(i,N) scanf("%d%d",&A[i].first,&A[i].second);
	FOR(i,1<<20) BC[i]=-1<<30;
	FOR(i,M) {
		scanf("%d%d",&x,&y);
		BC[x]=max(BC[x],-y);
	}

	for(i=(1<<20)-2;i>=0;i--) BC[i]=max(BC[i],BC[i+1]);
	FOR(i,1<<20) st.update(i,i+1,BC[i]);

	FOR(i,P) {
		scanf("%d%d%d",&X[i],&Y[i],&Z[i]);
		ev[X[i]].push_back({Y[i],Z[i]});
	}

	ll ma=-1LL<<60;
	sort(A,A+N);
	int pre=0;
	FOR(i,N) {
		while(pre<A[i].first) {
			FORR(e,ev[pre]) {
				st.update(e.first+1,1<<20,e.second);
			}
			pre++;
		}
		ll sc=-A[i].second+st.getval(0,1<<20);
		ma=max(ma,sc);
		ma=max(ma,-A[i].second-BC[0]);
	}
	cout<<ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M,P;
pair<int,int> A[202020];
int BC[1<<20];
int X[202020],Y[202020],Z[202020];

static ll const def=-1LL<<60;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val, ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2,0);
	};

	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return ma[k];
		return val[k]+max(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}

	void update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r) return;
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k]+=v;
		}
		else if(l < y && x < r) {
			update(x,y,v,l,(l+r)/2,k*2);
			update(x,y,v,(l+r)/2,r,k*2+1);
			ma[k]=val[k]+max(ma[k*2],ma[k*2+1]);
		}
	}
};
SegTree_3<ll,1<<20> st;
vector<pair<int,int>> ev[1010101];

void solve() {
	int i,j,k,l,r,x,y; string s;

	scanf("%d%d%d",&N,&M,&P);
	FOR(i,N) scanf("%d%d",&A[i].first,&A[i].second);
	FOR(i,1<<20) BC[i]=-1<<30;
	FOR(i,M) {
		scanf("%d%d",&x,&y);
		BC[x]=max(BC[x],-y);
	}

	for(i=(1<<20)-2;i>=0;i--) BC[i]=max(BC[i],BC[i+1]);
	FOR(i,1<<20) st.update(i,i+1,BC[i]);

	FOR(i,P) {
		scanf("%d%d%d",&X[i],&Y[i],&Z[i]);
		ev[X[i]].push_back({Y[i],Z[i]});
	}

	ll ma=-1LL<<60;
	sort(A,A+N);
	int pre=0;
	FOR(i,N) {
		while(pre<A[i].first) {
			FORR(e,ev[pre]) {
				st.update(e.first+1,1<<20,e.second);
			}
			pre++;
		}
		ll sc=-A[i].second+st.getval(0,1<<20);
		ma=max(ma,sc);
	}
	cout<<ma<<endl;

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}