#include <bits/stdc++.h>
using namespace std;
const int kN = 30005, kQ = 10005;
const long long kInfl = 1e18;
int n, q, rkw[kN], rkh[kN];
pair<int, int> w[kN], h[kN];
long long f[kN], c2[kN], c3[kN];
inline int Read() {
  int res = 0, flag = 1;
  char ch;
  for (ch = getchar(); isspace(ch); ch = getchar())
    ;
  if (ch == '-') {
    ch = getchar();
    flag = -1;
  }
  for (; isdigit(ch); ch = getchar()) res = res * 10 + ch - '0';
  return res * flag;
}
long long Calc(int x, int y) {
  if (x <= 0 || y <= 0 || x > n || y > n) return -kInfl;
  return 1LL * w[x].first * h[y].first;
}
void Update(int x) {
  c2[x] = Calc(x, x + 1) + Calc(x + 1, x);
  c3[x] = max(Calc(x, x + 1) + Calc(x + 1, x + 2) + Calc(x + 2, x),
              Calc(x, x + 2) + Calc(x + 1, x) + Calc(x + 2, x + 1));
}
int main() {
  n = Read(), q = Read();
  for (int i = 1; i <= n; ++i) w[i] = (pair<int, int>){Read(), i};
  sort(w + 1, w + n + 1);
  for (int i = 1; i <= n; ++i) h[i] = (pair<int, int>){Read(), i};
  sort(h + 1, h + n + 1);
  for (int i = 1; i <= n; ++i) {
    rkw[w[i].second] = rkh[h[i].second] = i;
    Update(i);
  }
  for (int i = 1; i <= q; ++i) {
    int a(Read()), b(Read());
    swap(h[rkh[a]].second, h[rkh[b]].second);
    swap(rkh[a], rkh[b]);
    for (int j = -2; j <= 0; ++j) {
      Update(rkw[a] + j);
      Update(rkh[a] + j);
      Update(rkw[b] + j);
      Update(rkh[b] + j);
    }
    for (int j = n; j > 0; --j) {
      f[j] = max(c2[j] + f[j + 2], c3[j] + f[j + 3]);
      if (w[j].second != h[j].second)
        f[j] = max(f[j], (long long)w[j].first * h[j].first + f[j + 1]);
    }
    printf("%lld\n", f[1]);
  }
  return 0;
}
