#include <bits/stdc++.h>
using namespace std;
inline int RR() {
  int v = 0, k = 1;
  char c = 0;
  while ('0' > c || c > '9') {
    if (c == '-') k = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') v = (v << 3) + (v << 1) + c - 48, c = getchar();
  return v * k;
}
inline void W(long long x) {
  if (x > 9) W(x / 10);
  putchar(x % 10 + 48);
}
const int N = 3e4 + 5;
struct cs {
  long long v;
  int p;
} a[N], b[N];
long long f[N], g[N][3];
int pa[N], pb[N], c[N];
int n, m, x, y;
bool operator<(cs a, cs b) { return a.v < b.v; }
inline void jdb(int i) {
  g[i][0] = g[i][1] = g[i][2] = -1e9;
  if (c[i] != i) g[i][0] = a[i].v * b[i].v;
  if (i > 1 && c[i] != i - 1 && c[i - 1] != i)
    g[i][1] = a[i - 1].v * b[i].v + a[i].v * b[i - 1].v;
  if (i > 2 && c[i] != i - 2 && c[i - 1] != i && c[i - 2] != i - 1)
    g[i][2] =
        a[i].v * b[i - 2].v + a[i - 1].v * b[i].v + a[i - 2].v * b[i - 1].v;
  if (i > 2 && c[i] != i - 1 && c[i - 1] != i - 2 && c[i - 2] != i)
    g[i][2] = max(g[i][2], a[i].v * b[i - 1].v + a[i - 1].v * b[i - 2].v +
                               a[i - 2].v * b[i].v);
}
int main() {
  n = RR();
  m = RR();
  for (int i = 1; i <= n; i++) a[i].v = RR(), a[i].p = i;
  for (int i = 1; i <= n; i++) b[i].v = RR(), b[i].p = i;
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) pa[a[i].p] = pb[b[i].p] = i;
  for (int i = 1; i <= n; i++) c[i] = pb[a[i].p];
  for (int i = 1; i <= n; i++) jdb(i);
  while (m--) {
    x = pa[RR()];
    y = pa[RR()];
    swap(c[x], c[y]);
    for (int i = x; i <= min(n, x + 2); i++) jdb(i);
    for (int i = y; i <= min(n, y + 2); i++) jdb(i);
    for (int i = 1; i <= n; i++) {
      f[i] = f[i - 1] + g[i][0];
      if (i > 1) f[i] = max(f[i], f[i - 2] + g[i][1]);
      if (i > 2) f[i] = max(f[i], f[i - 3] + g[i][2]);
    }
    W(f[n]);
    puts("");
  }
}
