You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.

For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.

Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.

The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.

Input

The first line contains a single integer n_A (1 ≤ n_A ≤ 100).

The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.

The next line contains a single integer n_B (1 ≤ n_B ≤ 100).

The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.

Note that segments in both sets may intersect.

Output

Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.

Examples

Input


2
3 5
5 8
3
1 2
1 9
2 9


Output


112


Input


1
1 9
2
2 4
2 10


Output


120

Note

In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
long long n, m, l, r;
long long mod = 998244353, inv2 = (mod + 1) / 2;
vector<pair<long long, long long> > v1, v2;
vector<pair<long long, long long> > v1f[64], v2f[64];
vector<pair<long long, long long> > fin;
int main() {
  read(n);
  for (int i = 0; i < n; i++) {
    read(l);
    read(r);
    r += 1;
    while ((l + (l & (-l))) <= r) {
      v1.push_back(make_pair(l, (l + (l & (-l)) - 1)));
      l += l & (-l);
    }
    while ((r - (r & (-r))) >= l) {
      v1.push_back(make_pair(r - (r & (-r)), r - 1));
      r -= r & (-r);
    }
  }
  read(m);
  for (int i = 0; i < m; i++) {
    read(l);
    read(r);
    r += 1;
    while ((l + (l & (-l))) <= r) {
      v2.push_back(make_pair(l, (l + (l & (-l)) - 1)));
      l += l & (-l);
    }
    while ((r - (r & (-r))) >= l) {
      v2.push_back(make_pair(r - (r & (-r)), r - 1));
      r -= r & (-r);
    }
  }
  for (auto ct1 : v1) {
    pair<long long, long long> cc = ct1;
    for (int i = 0; i < 60; i++) {
      if (((1ll) << i) - 1 == cc.second - cc.first) {
        v1f[i].push_back(cc);
        long long cn = ((1ll) << (i + 1)) - 1;
        if ((cc.second & cn) == cn) {
          cc.first -= ((1ll) << i);
        } else {
          cc.second += ((1ll) << i);
        }
      }
    }
  }
  for (auto ct1 : v2) {
    pair<long long, long long> cc = ct1;
    for (int i = 0; i < 60; i++) {
      if (((1ll) << i) - 1 == cc.second - cc.first) {
        v2f[i].push_back(cc);
        long long cn = ((1ll) << (i + 1)) - 1;
        if ((cc.second & cn) == cn) {
          cc.first -= ((1ll) << i);
        } else {
          cc.second += ((1ll) << i);
        }
      }
    }
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  v1.erase(unique(v1.begin(), v1.end()), v1.end());
  v2.erase(unique(v2.begin(), v2.end()), v2.end());
  for (int i = 0; i < 60; i++) {
    sort(v1f[i].begin(), v1f[i].end());
    sort(v2f[i].begin(), v2f[i].end());
    v1f[i].erase(unique(v1f[i].begin(), v1f[i].end()), v1f[i].end());
    v2f[i].erase(unique(v2f[i].begin(), v2f[i].end()), v2f[i].end());
  }
  for (int i = 0; i < 60; i++) {
    long long cs = ((1ll) << i) - 1;
    for (auto ct1 : v1) {
      if (ct1.second - ct1.first != cs) continue;
      for (auto ct2 : v2f[i]) {
        fin.push_back(
            make_pair(ct1.first ^ ct2.first, (ct1.first ^ ct2.first) + cs));
      }
    }
    for (auto ct1 : v2) {
      if (ct1.second - ct1.first != cs) continue;
      for (auto ct2 : v1f[i]) {
        fin.push_back(
            make_pair(ct1.first ^ ct2.first, (ct1.first ^ ct2.first) + cs));
      }
    }
  }
  sort(fin.begin(), fin.end());
  long long ans = 0, st = fin[0].first, ed = fin[0].second;
  for (auto ct : fin) {
    if (ct.first <= ed) {
      ed = max(ed, (long long)ct.second);
    } else {
      ans += (((st + ed) % mod) * ((ed - st + 1) % mod) % mod) * inv2 % mod;
      st = ct.first;
      ed = ct.second;
    }
  }
  ans += (((st + ed) % mod) * ((ed - st + 1) % mod) % mod) * inv2 % mod;
  ans %= mod;
  cout << ans << endl;
  return 0;
}
